How to Create an External Connection Pool

From InfiniteERP Wiki
Revision as of 09:17, 16 December 2021 by Wikiadmin (talk | contribs) (Created page with "{{(!)| The use of external connection pools is available from '''3.0MP32'''}} == Introduction == Openbravo uses connection pools to reuse existing connections/prepared state...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Bulbgraph.png   The use of external connection pools is available from 3.0MP32

Introduction

Openbravo uses connection pools to reuse existing connections/prepared statements, avoiding the cost of initiating a connection, parsing SQL etc. By default Openbravo uses two different connection pools:

  • Hibernate default connection pool for DAL-related queries
  • Apache DBCP for the connections provided by the ConnectionProviderImpl.

From 3.0MP32 it is possible to specify an external connection provider to replace these two connection pools. Openbravo provides a free commercial module that implements the Tomcat JDBC Connection Pool, but it is possible for developers to publish a module implement their own connection pool.

ExternalConnectionPool abstract class

In order to implement an external connection pool, developers must subclass the ExternalConnectionPool abstract class. This class has three methods:

<source lang="java">/**

  * 
  * @param externalConnectionPoolClassName
  *          The full class name of the external connection pool
  * @return An instance of the external connection pool
  */

public final synchronized static ExternalConnectionPool getInstance(

     String externalConnectionPoolClassName) throws InstantiationException,
     IllegalAccessException, ClassNotFoundException;

</source>

The getInstance method is in charge of instantiating the external connection pool. The name of the class that implements the connection pool is passed as a parameter, its value is taken from the db.externalPoolClassName property of Openbravo.properties.

<source lang="java">/**

  * @return A Connection from the external connection pool
  */

public abstract Connection getConnection() </source>

This is the main method that should be overwritten by all external connection providers. It takes no arguments, and it should return a connection that has been borrowed from the pool.

<source lang="java">/**

  * If the external connection pool supports interceptors this method should be overwritten
  * 
  * @param interceptors
  *          List of PoolInterceptorProvider comprised of all the interceptors injected with Weld
  */

public void loadInterceptors(List<PoolInterceptorProvider> interceptors) </source>

This method should be overwritten if the connection pool supports interceptors and allows custom interceptors to be instantiated using dependency injection. A list of PoolInterceptorProvider will be passed as an arguments, containing the full class name of the interceptors.

Design Considerations

  • Include a configuration file so that users can customize the external connection pool to meet their needs.
  • If the pool needs to execute some initialization code, place it in the getConnection method and make sure that is only executed the first time that method is invoked.
  • For all connection returned by the pool:
    • SessionInfo.setDBSessionInfo(Connection conn, String rdbms) must be executed
    • The sql query included in the bbdd.sessionConfig property must be executed using that connection.
  • Take into account that these two operations must be executed on the returned connection only when the connection is actually created, not every time it is returned from the pool.