Fixes issue 22923: Cursors not closed
authorMartin Taal <martin.taal@openbravo.com>
Wed, 30 Jan 2013 12:36:06 +0100
changeset 19522 d0dbfd0f4d02
parent 19521 d0a11fe5293e
child 19523 b34d87f25d55
Fixes issue 22923: Cursors not closed
Add explicit statement close
src-core/src/org/openbravo/utils/OBRebuildAppender.java
src/org/openbravo/base/OBSchedulerInitializerListener.java
src/org/openbravo/base/model/ModelProvider.java
src/org/openbravo/erpCommon/ad_process/ApplyModulesCallServlet.java
src/org/openbravo/service/db/CallStoredProcedure.java
src/org/openbravo/service/system/SystemService.java
--- a/src-core/src/org/openbravo/utils/OBRebuildAppender.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src-core/src/org/openbravo/utils/OBRebuildAppender.java	Wed Jan 30 12:36:06 2013 +0100
@@ -63,27 +63,41 @@
         if (connection == null || connection.isClosed()) {
           connection = cp.getConnection();
         }
-        PreparedStatement ln = connection
-            .prepareStatement("SELECT coalesce(max(line_number)+1,1) FROM AD_ERROR_LOG");
-        ResultSet rs = ln.executeQuery();
-        String line_number;
-        if (rs.next()) {
-          line_number = rs.getString(1);
-        } else {
-          line_number = "1";
+        PreparedStatement ln = null;
+        String line_number = "1";
+        try {
+          ln = connection
+              .prepareStatement("SELECT coalesce(max(line_number)+1,1) FROM AD_ERROR_LOG");
+          ResultSet rs = ln.executeQuery();
+          if (rs.next()) {
+            line_number = rs.getString(1);
+          } else {
+            line_number = "1";
+          }
+        } finally {
+          if (ln != null && !ln.isClosed()) {
+            ln.close();
+          }
         }
 
         String message = arg0.getMessage().toString();
         if (message.length() > 3000)
           message = message.substring(0, 2997) + "...";
-        PreparedStatement ps = connection
-            .prepareStatement("INSERT INTO ad_error_log (ad_error_log_id, ad_client_id, ad_org_id, isactive, created, createdby, updated, updatedby, system_status, error_level, message, line_number) SELECT get_uuid(), '0', '0', 'Y', now(), '0', now(), '0', system_status, ?,?, to_number(?) FROM ad_system_info");
-        String level = arg0.getLevel().toString();
+        PreparedStatement ps = null;
+        try {
+          ps = connection
+              .prepareStatement("INSERT INTO ad_error_log (ad_error_log_id, ad_client_id, ad_org_id, isactive, created, createdby, updated, updatedby, system_status, error_level, message, line_number) SELECT get_uuid(), '0', '0', 'Y', now(), '0', now(), '0', system_status, ?,?, to_number(?) FROM ad_system_info");
+          String level = arg0.getLevel().toString();
 
-        ps.setString(1, level);
-        ps.setString(2, message);
-        ps.setString(3, line_number);
-        ps.executeUpdate();
+          ps.setString(1, level);
+          ps.setString(2, message);
+          ps.setString(3, line_number);
+          ps.executeUpdate();
+        } finally {
+          if (ps != null && !ps.isClosed()) {
+            ps.close();
+          }
+        }
       } catch (Exception e) {
         // We will not log an error if the insertion in the log table
         // goes wrong for two different reasons:
--- a/src/org/openbravo/base/OBSchedulerInitializerListener.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src/org/openbravo/base/OBSchedulerInitializerListener.java	Wed Jan 30 12:36:06 2013 +0100
@@ -120,9 +120,16 @@
       try {
         Connection connection = OBDal.getInstance().getConnection();
         if (connection != null) {
-          Statement s = connection.createStatement();
-          String query = "UPDATE AD_PROCESS_RUN SET END_TIME=NOW(),STATUS='SYR' WHERE END_TIME IS NULL";
-          s.executeUpdate(query);
+          Statement s = null;
+          try {
+            s = connection.createStatement();
+            String query = "UPDATE AD_PROCESS_RUN SET END_TIME=NOW(),STATUS='SYR' WHERE END_TIME IS NULL";
+            s.executeUpdate(query);
+          } finally {
+            if (s != null && !s.isClosed()) {
+              s.close();
+            }
+          }
           OBDal.getInstance().flush();
           OBDal.getInstance().getConnection().commit();
         } else {
--- a/src/org/openbravo/base/model/ModelProvider.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src/org/openbravo/base/model/ModelProvider.java	Wed Jan 30 12:36:06 2013 +0100
@@ -320,18 +320,25 @@
     try {
       con = new ConnectionProviderImpl(OBPropertiesProvider.getInstance().getOpenbravoProperties());
       connection = con.getConnection();
-      PreparedStatement ps = connection
-          .prepareStatement("select distinct model_impl from ad_reference where model_impl is not null");
-      ResultSet rs = ps.executeQuery();
-      while (rs.next()) {
-        String classname = rs.getString(1);
-        Class<?> myClass = Class.forName(classname);
-        if (org.openbravo.base.model.domaintype.BaseDomainType.class.isAssignableFrom(myClass)) {
-          BaseDomainType classInstance = (BaseDomainType) myClass.newInstance();
-          for (Class<?> aClass : classInstance.getClasses()) {
-            sessionFactoryController.addAdditionalClasses(aClass);
+      PreparedStatement ps = null;
+      try {
+        ps = connection
+            .prepareStatement("select distinct model_impl from ad_reference where model_impl is not null");
+        ResultSet rs = ps.executeQuery();
+        while (rs.next()) {
+          String classname = rs.getString(1);
+          Class<?> myClass = Class.forName(classname);
+          if (org.openbravo.base.model.domaintype.BaseDomainType.class.isAssignableFrom(myClass)) {
+            BaseDomainType classInstance = (BaseDomainType) myClass.newInstance();
+            for (Class<?> aClass : classInstance.getClasses()) {
+              sessionFactoryController.addAdditionalClasses(aClass);
+            }
           }
         }
+      } finally {
+        if (ps != null && !ps.isClosed()) {
+          ps.close();
+        }
       }
     } catch (Exception e) {
       throw new OBException("Failed to load reference classes", e);
--- a/src/org/openbravo/erpCommon/ad_process/ApplyModulesCallServlet.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_process/ApplyModulesCallServlet.java	Wed Jan 30 12:36:06 2013 +0100
@@ -146,12 +146,9 @@
         resp.setStatusofstate(defaultState);
     } catch (Exception e) {
     } finally {
-      try {
-        releasePreparedStatement(ps3);
-        releasePreparedStatement(ps2);
-        releasePreparedStatement(ps);
-      } catch (SQLException e2) {
-      }
+      releasePreparedStatement(ps);
+      releasePreparedStatement(ps2);
+      releasePreparedStatement(ps3);
     }
     return resp;
   }
@@ -201,11 +198,8 @@
       // We need to use printStackTrace here because if not, the log will not be shown
       e.printStackTrace();
     } finally {
-      try {
-        releasePreparedStatement(ps3);
-        releasePreparedStatement(ps2);
-      } catch (SQLException e2) {
-      }
+      releasePreparedStatement(ps2);
+      releasePreparedStatement(ps3);
     }
     return resp;
   }
@@ -239,11 +233,7 @@
       // We need to use printStackTrace here because if not, the log will not be shown
       e.printStackTrace();
     } finally {
-      if (ps != null)
-        try {
-          releasePreparedStatement(ps);
-        } catch (SQLException e) {
-        }
+      releasePreparedStatement(ps);
     }
   }
 
@@ -292,8 +282,13 @@
     }
     String finalMessageType = "";
     OBError error = new OBError();
-    PreparedStatement ps;
-    PreparedStatement ps2;
+    PreparedStatement ps = null;
+    PreparedStatement ps2 = null;
+    PreparedStatement ps3 = null;
+    PreparedStatement ps4 = null;
+    PreparedStatement ps5 = null;
+    PreparedStatement ps6 = null;
+    PreparedStatement ps7 = null;
     try {
       ps = getPreparedStatement("SELECT MESSAGE FROM AD_ERROR_LOG WHERE ERROR_LEVEL='ERROR' ORDER BY CREATED DESC");
       ps.executeQuery();
@@ -324,7 +319,7 @@
 
       BuildMainStep finalStep;
       if (finalMessageType.equals("Error")) {
-        PreparedStatement ps3 = getPreparedStatement("SELECT SYSTEM_STATUS FROM AD_SYSTEM_INFO");
+        ps3 = getPreparedStatement("SELECT SYSTEM_STATUS FROM AD_SYSTEM_INFO");
         ps3.executeQuery();
         ResultSet rs3 = ps3.getResultSet();
         rs3.next();
@@ -382,27 +377,42 @@
       if (!rsErr.next()) {
         String successCode = build.getMainSteps().get(build.getMainSteps().size() - 1)
             .getSuccessCode();
-        PreparedStatement ps3 = getPreparedStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='"
-            + successCode + "'");
-        ps3.executeUpdate();
-        PreparedStatement ps4 = getPreparedStatement("UPDATE AD_MODULE SET STATUS='A' WHERE STATUS='P'");
+        ps4 = getPreparedStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='" + successCode + "'");
         ps4.executeUpdate();
+        ps5 = getPreparedStatement("UPDATE AD_MODULE SET STATUS='A' WHERE STATUS='P'");
+        ps5.executeUpdate();
       } else {
-        ps = getPreparedStatement("SELECT SYSTEM_STATUS FROM AD_SYSTEM_INFO");
-        ps.executeQuery();
-        ResultSet rs1 = ps.getResultSet();
+        ps6 = getPreparedStatement("SELECT SYSTEM_STATUS FROM AD_SYSTEM_INFO");
+        ps6.executeQuery();
+        ResultSet rs1 = ps6.getResultSet();
         rs1.next();
         String state = rs1.getString(1);
         BuildMainStep finalMainStep = build.mainStepOfCode(state);
         String errorCode = finalMainStep.getErrorCode();
-        PreparedStatement ps3 = getPreparedStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='"
-            + errorCode + "'");
-        ps3.executeUpdate();
+        ps7 = getPreparedStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='" + errorCode + "'");
+        ps7.executeUpdate();
       }
     } catch (Exception e) {
       // We need to use printStackTrace here because if not, the log will not be shown
       e.printStackTrace();
+    } finally {
+      releasePreparedStatement(ps);
+      releasePreparedStatement(ps2);
+      releasePreparedStatement(ps3);
+      releasePreparedStatement(ps4);
+      releasePreparedStatement(ps5);
+      releasePreparedStatement(ps6);
+      releasePreparedStatement(ps7);
     }
   }
 
+  public void releasePreparedStatement(PreparedStatement ps) {
+    try {
+      if (ps != null) {
+        super.releasePreparedStatement(ps);
+      }
+    } catch (SQLException e) {
+      // ignore
+    }
+  }
 }
--- a/src/org/openbravo/service/db/CallStoredProcedure.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src/org/openbravo/service/db/CallStoredProcedure.java	Wed Jan 30 12:36:06 2013 +0100
@@ -22,6 +22,7 @@
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.sql.Types;
 import java.util.Date;
@@ -95,9 +96,10 @@
       sb.append(" AS RESULT FROM DUAL");
     }
     final Connection conn = OBDal.getInstance().getConnection(doFlush);
+    PreparedStatement ps = null;
     try {
-      final PreparedStatement ps = conn.prepareStatement(sb.toString(),
-          ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
+      ps = conn.prepareStatement(sb.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
+          ResultSet.CONCUR_READ_ONLY);
       int index = 0;
 
       for (Object parameter : parameters) {
@@ -132,10 +134,17 @@
         }
       }
       resultSet.close();
-      ps.close();
       return resultValue;
     } catch (Exception e) {
       throw new IllegalStateException(e);
+    } finally {
+      try {
+        if (ps != null && !ps.isClosed()) {
+          ps.close();
+        }
+      } catch (SQLException e) {
+        // ignore
+      }
     }
   }
 
--- a/src/org/openbravo/service/system/SystemService.java	Tue Jan 29 19:27:44 2013 +0100
+++ b/src/org/openbravo/service/system/SystemService.java	Wed Jan 30 12:36:06 2013 +0100
@@ -327,18 +327,38 @@
         }
       }
       for (String command : sqlCommands) {
-        PreparedStatement ps = con.prepareStatement(command);
-        ps.setString(1, clientId);
-        ps.executeUpdate();
+        PreparedStatement ps = null;
+        try {
+          ps = con.prepareStatement(command);
+          ps.setString(1, clientId);
+          ps.executeUpdate();
+        } finally {
+          if (ps != null && !ps.isClosed()) {
+            ps.close();
+          }
+        }
       }
-      PreparedStatement stpref = con
-          .prepareStatement("DELETE FROM ad_preference p where visibleat_client_id=?");
-      stpref.setString(1, clientId);
-      stpref.executeUpdate();
-      PreparedStatement stpers = con
-          .prepareStatement("DELETE FROM obuiapp_uipersonalization p where visibleat_client_id=?");
-      stpers.setString(1, clientId);
-      stpers.executeUpdate();
+      PreparedStatement stpref = null;
+      try {
+        stpref = con.prepareStatement("DELETE FROM ad_preference p where visibleat_client_id=?");
+        stpref.setString(1, clientId);
+        stpref.executeUpdate();
+      } finally {
+        if (stpref != null && !stpref.isClosed()) {
+          stpref.close();
+        }
+      }
+      PreparedStatement stpers = null;
+      try {
+        stpers = con
+            .prepareStatement("DELETE FROM obuiapp_uipersonalization p where visibleat_client_id=?");
+        stpers.setString(1, clientId);
+        stpers.executeUpdate();
+      } finally {
+        if (stpers != null && !stpers.isClosed()) {
+          stpers.close();
+        }
+      }
       con.commit();
       OBDal.getInstance().commitAndClose();
       enableConstraints(platform);
@@ -368,9 +388,15 @@
   private void resetSafeMode(Connection con) {
 
     try {
-      PreparedStatement ps2 = con
-          .prepareStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='RB70'");
-      ps2.executeUpdate();
+      PreparedStatement ps2 = null;
+      try {
+        ps2 = con.prepareStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='RB70'");
+        ps2.executeUpdate();
+      } finally {
+        if (ps2 != null && !ps2.isClosed()) {
+          ps2.close();
+        }
+      }
     } catch (Exception e) {
       throw new RuntimeException("Couldn't reset the safe mode", e);
     }
@@ -378,13 +404,26 @@
 
   private void killConnectionsAndSafeMode(Connection con) {
     try {
-      PreparedStatement updateSession = con
-          .prepareStatement("UPDATE AD_SESSION SET SESSION_ACTIVE='N' WHERE CREATEDBY<>?");
-      updateSession.setString(1, OBContext.getOBContext().getUser().getId());
-      updateSession.executeUpdate();
-      PreparedStatement ps2 = con
-          .prepareStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='RB80'");
-      ps2.executeUpdate();
+      PreparedStatement updateSession = null;
+      try {
+        updateSession = con
+            .prepareStatement("UPDATE AD_SESSION SET SESSION_ACTIVE='N' WHERE CREATEDBY<>?");
+        updateSession.setString(1, OBContext.getOBContext().getUser().getId());
+        updateSession.executeUpdate();
+      } finally {
+        if (updateSession != null && !updateSession.isClosed()) {
+          updateSession.close();
+        }
+      }
+      PreparedStatement ps2 = null;
+      try {
+        ps2 = con.prepareStatement("UPDATE AD_SYSTEM_INFO SET SYSTEM_STATUS='RB80'");
+        ps2.executeUpdate();
+      } finally {
+        if (ps2 != null && !ps2.isClosed()) {
+          ps2.close();
+        }
+      }
     } catch (Exception e) {
       throw new RuntimeException("Couldn't destroy concurrent sessions", e);
     }