Skip to main content

Wrapping a DataSource for XA

An XA capable database offers an XADataSource. This data source is not to be used directly by users though. The user will always bind to the plain DataSource interface. It is the duty of the application server to wrap an XADataSource with pooling and XA auto-enlistment.

In OSGi, there is also the issue that standard JDBC does not work well with OSGi classloading. So the OSGi alliance specified the DataSourceFactory interface. A DataSourceFactory is provided as an OSGi service by the database driver or by an external adapter. It allows to create DataSource and XADataSource instances in an OSGi friendly way.

The pax-jdbc project provides DataSourceFactory adapters for legacy drivers as well as pooling and XA auto-enlistment wrappers for existing DataSourceFactory services.

For H2, the installation is below. This installs the H2 database as well as adapters for pooling and XA support.

feature:repo-add pax-jdbc 0.7.0
feature:install pax-jdbc-h2 pax-jdbc-pool-dbcp2 pax-jdbc-config transaction

The installation can be checked with service:list DataSourceFactory. It should list the original H2 DataSourceFactory as well as a pooling one and a pooling/XA one. The different services can be discerned by their service properties.

To create an actual DataSource, pax-jdbc-config monitors configuration files with the naming scheme etc/org.ops4j.datasource-*.cfg. It will automatically create a DataSource for each of these configurations.

You can reference the DataSourceFactory using the special property osgi.jdbc.driver.name or osgi.jdbc.driver.class. The value should match the respective property value of the DataSourceFactory service. For more details, see the pax-jdbc documenation.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!