related with bug 39601: minor code cleanup
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 08 Nov 2018 10:47:32 +0100
changeset 34948 2ef2c5233cab
parent 34947 e834e74a3c1a
child 34949 f55cbe0219f9
related with bug 39601: minor code cleanup
modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java
--- a/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java	Thu Nov 08 10:19:51 2018 +0100
+++ b/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java	Thu Nov 08 10:47:32 2018 +0100
@@ -21,13 +21,12 @@
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
-import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import org.apache.tomcat.jdbc.pool.ConnectionPool;
 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
 import org.apache.tomcat.jdbc.pool.PooledConnection;
-import org.openbravo.base.exception.OBException;
 import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.database.PoolInterceptorProvider;
 import org.openbravo.database.SessionInfo;
@@ -52,51 +51,41 @@
    */
   @Override
   public void reset(ConnectionPool parent, PooledConnection con) {
-    if (con != null) {
-      boolean physicalConnectionChanged = hasPhysicalConnectionChanged(con);
+    if (con == null) {
+      return;
+    }
 
-      HashMap<Object, Object> attributes = con.getAttributes();
-      Boolean sessionInfoApplied = (Boolean) attributes.get(SESSION_CONFIG_APPLIED);
-      if (physicalConnectionChanged || sessionInfoApplied == null || !sessionInfoApplied) {
-        PreparedStatement pstmt = null;
-        try {
-          final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();
-          final String dbSessionConfig = props.getProperty("bbdd.sessionConfig");
-          pstmt = con.getConnection().prepareStatement(dbSessionConfig);
-          pstmt.executeQuery();
-        } catch (Exception e) {
-          throw new IllegalStateException(e);
-        } finally {
-          try {
-            if (pstmt != null && !pstmt.isClosed()) {
-              pstmt.close();
-            }
-          } catch (SQLException e) {
-            throw new OBException(e);
-          }
-        }
-        attributes.put(SESSION_CONFIG_APPLIED, true);
+    boolean physicalConnectionChanged = hasPhysicalConnectionChanged(con);
+
+    Map<Object, Object> attributes = con.getAttributes();
+    Boolean sessionInfoApplied = (Boolean) attributes.get(SESSION_CONFIG_APPLIED);
+    if (physicalConnectionChanged || sessionInfoApplied == null || !sessionInfoApplied) {
+      final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();
+      final String dbSessionConfig = props.getProperty("bbdd.sessionConfig");
+      try (PreparedStatement pstmt = con.getConnection().prepareStatement(dbSessionConfig)) {
+        pstmt.executeQuery();
+      } catch (SQLException e) {
+        throw new IllegalStateException(e);
       }
+      attributes.put(SESSION_CONFIG_APPLIED, true);
+    }
 
-      Boolean sessionInfoInitialized = (Boolean) attributes.get(SESSION_INFO_APPLIED);
-      if (physicalConnectionChanged || sessionInfoInitialized == null || !sessionInfoInitialized) {
-        boolean initialized = false;
-        if (isReadOnlyPool(parent)) {
-          initialized = true;
-        } else {
-          // SessionInfo will be initialized when the SessionListener ServletContextListener is
-          // invoked. That listener will check if there are audited tables and notify SessionInfo,
-          // from then on SessionInfo will know whether the ad_context_info table should be created
-          if (SessionInfo.isInitialized()) {
-            SessionInfo.initDB(con.getConnection(), rbdms);
-            initialized = true;
-          }
-        }
-        attributes.put(SESSION_INFO_APPLIED, initialized);
+    Boolean sessionInfoInitialized = (Boolean) attributes.get(SESSION_INFO_APPLIED);
+    if (physicalConnectionChanged || sessionInfoInitialized == null || !sessionInfoInitialized) {
+      boolean initialized = false;
+      if (isReadOnlyPool(parent)) {
+        initialized = true;
+      } else if (SessionInfo.isInitialized()) {
+        // SessionInfo will be initialized when the SessionListener ServletContextListener is
+        // invoked. That listener will check if there are audited tables and notify SessionInfo,
+        // from then on SessionInfo will know whether the ad_context_info table should be created
+        SessionInfo.initDB(con.getConnection(), rbdms);
+        initialized = true;
       }
+      attributes.put(SESSION_INFO_APPLIED, initialized);
+    }
 
-      cachePhysicalConnection(con);
-    }
+    cachePhysicalConnection(con);
   }
 
   /**