Fixed issue 33441: Queries to check ad_context_info have been reduced
authorNaroa Iriarte <naroa.iriarte@openbravo.com>
Wed, 10 Aug 2016 15:51:33 +0200
changeset 29839 88ea14de1a84
parent 29838 eec30a5fcdf4
child 29853 541e897fe626
Fixed issue 33441: Queries to check ad_context_info have been reduced

Before, the SessionInfo.initDB was invoqued when a connection was borrowed instead when a connection was created.
The correct behavior should be to execute the intDB method when creating a connection.
A new call to intDB has been added in ConnectionInitializerInterceptor.reset, if the connection is not initialized, it invoques the initDB method. This improves the number of times the initDB method is invoqued when the openbravo pool is used.
modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
src-core/src/org/openbravo/database/SessionInfo.java
src/org/openbravo/dal/core/DalSessionFactory.java
src/org/openbravo/dal/core/SessionHandler.java
--- a/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java	Wed Aug 10 14:24:36 2016 +0200
+++ b/modules/org.openbravo.apachejdbcconnectionpool/src/org/openbravo/apachejdbcconnectionpool/ConnectionInitializerInterceptor.java	Wed Aug 10 15:51:33 2016 +0200
@@ -51,7 +51,7 @@
       HashMap<Object, Object> attributes = con.getAttributes();
       Boolean connectionInitialized = (Boolean) attributes.get("OB_INITIALIZED");
       if (connectionInitialized == null || connectionInitialized == false) {
-        SessionInfo.setDBSessionInfo(con.getConnection(), rbdms);
+        SessionInfo.initDB(con.getConnection(), rbdms);
         PreparedStatement pstmt = null;
         try {
           final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Wed Aug 10 14:24:36 2016 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Wed Aug 10 15:51:33 2016 +0200
@@ -56,7 +56,6 @@
 import org.openbravo.base.model.Property;
 import org.openbravo.base.model.domaintype.EnumerateDomainType;
 import org.openbravo.base.secureApp.VariablesSecureApp;
-import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.base.weld.WeldUtils;
 import org.openbravo.client.application.Parameter;
 import org.openbravo.client.application.Process;
@@ -914,8 +913,7 @@
     // setting session info
     // Reset Session Info in DB manually as it was set in the service but actual information is not
     // available till now.
-    SessionInfo.setDBSessionInfo(OBDal.getInstance().getConnection(), OBPropertiesProvider
-        .getInstance().getOpenbravoProperties().getProperty("bbdd.rdbms"));
+    SessionInfo.setDBSessionInfo(OBDal.getInstance().getConnection());
   }
 
   private boolean checkSetParameters(HttpServletRequest request, HttpServletResponse response,
--- a/src-core/src/org/openbravo/database/SessionInfo.java	Wed Aug 10 14:24:36 2016 +0200
+++ b/src-core/src/org/openbravo/database/SessionInfo.java	Wed Aug 10 15:51:33 2016 +0200
@@ -199,7 +199,9 @@
    *          Connection where the session information will be stored in
    * @param rdbms
    *          Database type
+   * @deprecated
    */
+  @Deprecated
   public static void setDBSessionInfo(Connection conn, String rdbms) {
     if (!isAuditActive) {
       return;
--- a/src/org/openbravo/dal/core/DalSessionFactory.java	Wed Aug 10 14:24:36 2016 +0200
+++ b/src/org/openbravo/dal/core/DalSessionFactory.java	Wed Aug 10 15:51:33 2016 +0200
@@ -162,6 +162,7 @@
   /**
    * Note method sets user session information in the database and opens a connection for this.
    */
+  @Override
   public Session openSession() throws HibernateException {
     // NOTE: workaround for this issue:
     // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
@@ -171,6 +172,7 @@
       Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
       final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();
       Connection conn = ((SessionImplementor) session).connection();
+      // When a connection is obtained using the DAL pool it is necessary to call the initDB method.
       SessionInfo.initDB(conn, props.getProperty("bbdd.rdbms"));
       SessionInfo.setDBSessionInfo(conn);
       PreparedStatement pstmt = null;
@@ -198,16 +200,28 @@
   /**
    * Note method sets user session information in the database and opens a connection for this.
    */
+  @Override
   public Session openSession(Connection connection, Interceptor interceptor) {
     // NOTE: workaround for this issue:
     // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
     final Session session = delegateSessionFactory.openSession(connection, interceptor);
+    Connection conn = ((SessionImplementor) session).connection();
+    initializeDBSessionInfo(conn);
+    return session;
+  }
+
+  /**
+   * Note method sets user session information in the database and opens a connection for this.
+   */
+  @Override
+  public Session openSession(Connection connection) {
+    // NOTE: workaround for this issue:
+    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
+    final Session session = delegateSessionFactory.openSession(connection);
     final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
     try {
       Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
       Connection conn = ((SessionImplementor) session).connection();
-      SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
-          .getProperty("bbdd.rdbms"));
       SessionInfo.setDBSessionInfo(conn);
     } finally {
       Thread.currentThread().setContextClassLoader(currentLoader);
@@ -218,81 +232,52 @@
   /**
    * Note method sets user session information in the database and opens a connection for this.
    */
-  public Session openSession(Connection connection) {
+  @Override
+  public Session openSession(Interceptor interceptor) throws HibernateException {
     // NOTE: workaround for this issue:
     // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
-    final Session session = delegateSessionFactory.openSession(connection);
+    final Session session = delegateSessionFactory.openSession(interceptor);
+    Connection conn = ((SessionImplementor) session).connection();
+    initializeDBSessionInfo(conn);
+    return session;
+  }
+
+  /**
+   * Note method sets user session information in the database and opens a connection for this.
+   */
+  @Override
+  public StatelessSession openStatelessSession() {
+    // NOTE: workaround for this issue:
+    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
+    final StatelessSession session = delegateSessionFactory.openStatelessSession();
+    Connection conn = ((SessionImplementor) session).connection();
+    initializeDBSessionInfo(conn);
+    return session;
+  }
+
+  /**
+   * Note method sets user session information in the database and opens a connection for this.
+   */
+  @Override
+  public StatelessSession openStatelessSession(Connection connection) {
+    // NOTE: workaround for this issue:
+    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
+    final StatelessSession session = delegateSessionFactory.openStatelessSession(connection);
+    Connection conn = ((SessionImplementor) session).connection();
+    initializeDBSessionInfo(conn);
+    return session;
+  }
+
+  private void initializeDBSessionInfo(Connection conn) {
     final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
     try {
       Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
-      Connection conn = ((SessionImplementor) session).connection();
       SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
           .getProperty("bbdd.rdbms"));
       SessionInfo.setDBSessionInfo(conn);
     } finally {
       Thread.currentThread().setContextClassLoader(currentLoader);
     }
-    return session;
-  }
-
-  /**
-   * Note method sets user session information in the database and opens a connection for this.
-   */
-  public Session openSession(Interceptor interceptor) throws HibernateException {
-    // NOTE: workaround for this issue:
-    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
-    final Session session = delegateSessionFactory.openSession(interceptor);
-    final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
-    try {
-      Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
-      Connection conn = ((SessionImplementor) session).connection();
-      SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
-          .getProperty("bbdd.rdbms"));
-      SessionInfo.setDBSessionInfo(conn);
-    } finally {
-      Thread.currentThread().setContextClassLoader(currentLoader);
-    }
-    return session;
-  }
-
-  /**
-   * Note method sets user session information in the database and opens a connection for this.
-   */
-  public StatelessSession openStatelessSession() {
-    // NOTE: workaround for this issue:
-    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
-    final StatelessSession session = delegateSessionFactory.openStatelessSession();
-    final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
-    try {
-      Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
-      Connection conn = ((SessionImplementor) session).connection();
-      SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
-          .getProperty("bbdd.rdbms"));
-      SessionInfo.setDBSessionInfo(conn);
-    } finally {
-      Thread.currentThread().setContextClassLoader(currentLoader);
-    }
-    return session;
-  }
-
-  /**
-   * Note method sets user session information in the database and opens a connection for this.
-   */
-  public StatelessSession openStatelessSession(Connection connection) {
-    // NOTE: workaround for this issue:
-    // http://opensource.atlassian.com/projects/hibernate/browse/HHH-3529
-    final StatelessSession session = delegateSessionFactory.openStatelessSession(connection);
-    final ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
-    try {
-      Thread.currentThread().setContextClassLoader(BorrowedConnectionProxy.class.getClassLoader());
-      Connection conn = ((SessionImplementor) session).connection();
-      SessionInfo.initDB(conn, OBPropertiesProvider.getInstance().getOpenbravoProperties()
-          .getProperty("bbdd.rdbms"));
-      SessionInfo.setDBSessionInfo(conn);
-    } finally {
-      Thread.currentThread().setContextClassLoader(currentLoader);
-    }
-    return session;
   }
 
   public Cache getCache() {
--- a/src/org/openbravo/dal/core/SessionHandler.java	Wed Aug 10 14:24:36 2016 +0200
+++ b/src/org/openbravo/dal/core/SessionHandler.java	Wed Aug 10 15:51:33 2016 +0200
@@ -40,6 +40,7 @@
 import org.openbravo.base.util.Check;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.database.ExternalConnectionPool;
+import org.openbravo.database.SessionInfo;
 import org.openbravo.service.db.DbUtility;
 
 /**
@@ -55,6 +56,7 @@
   private static final Logger log = Logger.getLogger(SessionHandler.class);
 
   private static ExternalConnectionPool externalConnectionPool;
+  private static String rbdms;
 
   {
     String poolClassName = OBPropertiesProvider.getInstance().getOpenbravoProperties()
@@ -67,6 +69,7 @@
         log.warn("External connection pool class not found: " + poolClassName, e);
       }
     }
+    rbdms = (String) OBPropertiesProvider.getInstance().getOpenbravoProperties().get("bbdd.rdbms");
   }
 
   // The threadlocal which handles the session
@@ -178,6 +181,7 @@
       // getting connection from Hibernate pool
       newConnection = ((DalSessionFactory) SessionFactoryController.getInstance()
           .getSessionFactory()).getConnectionProvider().getConnection();
+      SessionInfo.initDB(newConnection, rbdms);
     }
     return newConnection;
   }