Creating and Consuming Database Connections using XPages

XPages uses a connection pool to make the most efficient use of database connections. This method allows for a pluggable architecture that can be used for different implementations.

As mentioned, XPages uses a connection pool to make the most efficient use of connections. This method provides a plugin architecture that can be used for different implementations. A simple connection pool is provided that supports Apache DBCP. Connections can be defined globally (through a server) or locally (via an NSF).

With the server method, the server would contain resources files. For example, <filename>.jdbc, which would be found in /WebContent/WEB-INF/jdbc. This file would define the connection.

The following provides an example of a <filename>.jdbc file that you might find on a server:
<jdbc type="simple">
	<driver>com.ibm.db2.jcc.DB2Driver</driver>
	<url>jdbc:db2://<host>:<port>/<DB></url>
	<user><usr></user>
	<password><pwd></password>
	<simple>
		<maxPoolSize>10</maxPoolSize>
		<maxConnectionSize>5</maxConnectionSize>
	</simple>
</jdbc>

An NSF contains a new file design element identical to the global resources files for defining database specific connections.

Connections can also be contributed programmatically via an extension point. The XPages runtime ensures that a connection is properly closed when an XPages request finishes.

More details on the process of enabling Apache DBCP support

To make DBCP support easier, the driver needs to visible from the DBCP connection pool classes. The goal is also to follow the OSGI recommendations and avoid the usage of workarounds like using a fragment, for example.

To do this, the code instantiating the connections pool was changed in order to make better usage of the JDBCDriverLoader class. This class is used by the internal pool to provide the driver and manually create all the factories and pools needed to use a PoolingDataSource instance. This code provides an example of how to do this:

driver = JDBCDriverLoader.loadDriver(driverClass);

ConnectionFactory connectionFactory = new DriverConnectionFactory(driver, url, 
properties);

// create the pool
pool = new GenericObjectPool();
// create the pool object factory
PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, 
pool...);
// finally create the datasource
PoolingDataSource bds = new PoolingDataSource(pool);

Another change was also needed to export the pools from the DBCP plugin so a change in the manifest of that plugin was done in order to export the packages. With those changes, the OSGI ready drivers provided in the source to access Apache Derby, DB2, etc. can be used by the DBCP pool.