Skip to content
gamlerhart edited this page Apr 2, 2013 · 3 revisions

We strongly recommend to use the connection pool. The connection pool avoid overhead for the initial connection handshake when reusing a connection. It also is an effective way to limit the amount of connections made to the database.

Adding Maven Dependency

The connection pool is an additional dependency:

<dependency>
	<groupId>org.adbcj</groupId>
	<artifactId>adbcj-connection-pool</artifactId>
	<version>0.6-SNAPSHOT</version>
</dependency>

Use the Pool

In order to use the connection pool you need to add the "pooled" protocol in the connection URL. A few examples:

  • MySQL Pooled: adbcj:pooled:mysql://localhost/adbcj-demo
  • MySQL direct: adbcj:mysql://localhost/adbcj-demo
  • H2 pooled: adbcj:pooled:h2://localhost/adbcj-demo
  • H2 direct: adbcj:h2://localhost/adbcj-demo

So, as an example:

    final ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(
            "adbcj:pooled:mysql://localhost/adbcj-demo",
            "adbcj",
            "adbc-pwd"
    );

#Configuring the Pool Currently the connection pool has two settings. You configure them by setting the properties for the connection:

  • pool.maxConnections: Number of maximum connections made to the database. Default: 50
  • pool.maxWaitForConnection: Maximum time the pool waits for getting a connection, in case the maximum connection is reached. In Milliseconds. Default: 500ms
  • pool.statementCacheSize: The pool tries to cache prepared statements. This way the statements are kept open for the pooled connection, why logically you create new connections. The statements are reused if the string matches. The default cache size is 64.

Example:

    Map<String,String> config = new HashMap<String,String>();
    config.put("pool.maxConnections","50");
    config.put("pool.maxWaitForConnection","500"); // in milleseconds
    final ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(
            "adbcj:pooled:mysql://localhost/adbcj-demo",
            "adbcj",
            "adbc-pwd"
    );