Pooling and XA support for DataSourceFactory

Deprecated

Since version 1.0.0 pooling and XA works differently. See Pooling and XA support in 1.0.0


ModuleDescription
pax-jdbc-pool-dbcpPooling and JTA support using dbcp2
pax-jdbc-pool-ariesPooling and JTA support using aries transaction jdbc

Both modules work basically in the same way and just use a different library for pooling. So make sure you only install one of them.

How it works

When one of the pax-jdbc-pool bundles is installed it will start to track DataSourceFactory services. For each such service it finds it will create an additional service with a pooling and XA wrapper around the original DataSourceFactory.

The pooled service will mostly have the same properties as the original service but it will add "-pool" to the osgi.jdbc.driver.name. If a TransactionManager is available it will also add "-xa" at the end.  So it will be easy to select the new Factory instead of the old one.

Keep in mind that the pooled XA DataSource will be published using the javax.sql.DataSource interface. So from the outside it will look like a normal DataSource. Inside it will take care that the connection is added to the TransactionManager as an XA resource.

Additonal / Changed Service Properties

The new pooled DataSourceFactory OSGi service provided by pax-jdbc-pool adds the following properties while keeping the properties of the original DataSourceFactory.

KeyValueDescription
osgi.jdbc.driver.name<orig_value>-pool[-xa]adds -pool and if TransactionManager is available also -xa
pooltrue
xatrueonly if TransactionManager is available

pax-jdbc-pool-dbcp2 configuration

When calling create you should supply the pooled DataSourceFactory with the same properties as the original one. In addition you can also set pooling properties that start with "pool.". These will be forwarded to the pooling library.

See http://commons.apache.org/proper/commons-dbcp/configuration.html.

Key (pool.)TypeDescription
blockWhenExhaustedboolean

Sets whether to block when the borrowObject() method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached)

lifobooleanSets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects - always returning the most recently used object from the pool, or as a FIFO (first in, first out) queue, where the pool always returns the oldest object in the idle object pool
maxIdleintReturns the cap on the number of "idle" instances in the pool
maxTotalintSets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time
maxWaitMillislongSets the maximum amount of time (in milliseconds) the borrowObject() method should block before throwing an exception when the pool is exhausted and getBlockWhenExhausted() is true
minEvictableIdleTimeMillislongSets the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any - see setTimeBetweenEvictionRunsMillis(long))
minIdleintSets the target for the minimum number of idle objects to maintain in the pool
numTestsPerEvictionRunintSets the maximum number of objects to examine during each run (if any) of the idle object evictor thread
softMinEvictableIdleTimeMillislongSets the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any - see setTimeBetweenEvictionRunsMillis(long)), with the extra condition that at least minIdle object instances remain in the pool
testOnBorrowbooleanSets whether objects borrowed from the pool will be validated before being returned from the borrowObject() method
testOnCreatebooleanSets whether objects created for the pool will be validated before being returned from the borrowObject() method
testOnReturnbooleanSets whether objects borrowed from the pool will be validated when they are returned to the pool via the returnObject() method
testWhileIdlebooleanReturns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - see setTimeBetweenEvictionRunsMillis(long))
timeBetweenEvictionRunsMillislongSets the number of milliseconds to sleep between runs of the idle object evictor thread


The following properties will only work for pax-jdbc-pool >= 0.6.0

Key (factory.)TypeDescriptionDefault
cacheStatebooleanIf true, the pooled connection will cache the current readOnly and autoCommit settings when first read or written and on all subsequent writes. This removes the need for additional database queries for any further calls to the getter. If the underlying connection is accessed directly and the readOnly and/or autoCommit settings changed the cached values will not reflect the current state. In this case, caching should be disabled by setting this attribute to falsetrue
defaultAutoCommitbooleanThe default auto-commit state of connections created by this pool. If not set then the setAutoCommit method will not be calleddriver default
defaultCatalogStringSets the default "catalog" setting for borrowed Connectionsdriver default
defaultReadOnlybooleanThe default read-only state of connections created by this pool. If not set then the setReadOnly method will not be called. (Some drivers don't support read only mode, ex: Informix)driver default
defaultTransactionIsolationintThe default TransactionIsolation state of connections created by this pool. One of the following: (see javadoc for int values)
  • NONE
  • READ_COMMITTED
  • READ_UNCOMMITTED
  • REPEATABLE_READ
  • SERIALIZABLE
driver default


Example config for pax-jdbc-pool-dbcp2 together with pax-jdbc-config

Create config in etc/org.ops4j.datasource-test.cfg:

osgi.jdbc.driver.name=H2-pool-xa
databaseName=test
user=sa
password=
dataSourceName=test2
pool.maxTotal=8

Installation in karaf 3

Install the features

Installation
feature:repo-add mvn:org.ops4j.pax.jdbc/pax-jdbc-features/0.5.0/xml/features
feature:install transaction jndi pax-jdbc-h2 pax-jdbc-config pax-jdbc-pool-dbcp2

The automatically created DataSource service will look like this:

DataSource
karaf@root()> service:list javax.sql.DataSource
[javax.sql.DataSource]
----------------------
 dataSourceName = test2
 service.factoryPid = org.ops4j.datasource
 databaseName = test
 user = sa
 osgi.jdbc.driver.name = H2-pool-xa
 osgi.jndi.service.name = test2
 felix.fileinstall.filename = file:/.../etc/org.ops4j.datasource-test2.cfg
 service.pid = org.ops4j.datasource.b1809982-d20a-4b84-b48c-ad23bbafb9ec
 pool.maxTotal = 8
 password = 
 service.id = 743
Provided by : 
 OPS4J Pax JDBC Config (72)

It will have pooling and XA transaction support.