H2 Driver Adapter

Current H2 versions

For current H2 versions the adapter is not necessary anymore. The H2 driver already registers a DataSourceFactory service.

This service has the properties:

  • osgi.jdbc.driver.class = org.h2.Driver

For 1.3.172

  • osgi.jdbc.driver.name = H2

For 1.3.174

So to make sure best check what properties the DataSourceFactory service actually has. In karaf you can do this using:

service:list DataSourceFactory

Usage

The Pax JDBC H2 Driver Adapter registers a DataSourceFactory in the OSGi service registry, adapting implementation classes from the H2 Driver, which is an OSGi bundle and does not need to be wrapped.

The DataSourceFactory methods of this adapter produces objects with the following implementation classes:

  • org.h2.Driver
  • org.h2.jdbcx.JdbcDataSource

Service Properties

The H2 Driver Adapter registers its DataSourceFactory with the following service properties:

  • osgi.jdbc.driver.class = org.h2.Driver
  • osgi.jdbc.driver.name = h2

Method Arguments

The argument passed to createDataSource(), createConnectionPoolDataSource(), createXADataSource() supports the following properties:

  • DataSourceFactory.JDBC_DATABASE_NAME (mandatory)
  • DataSourceFactory.JDBC_USER
  • DataSourceFactory.JDBC_PASSWORD

An SQLException is thrown if any other properties are set or if a mandatory property is missing.

createDriver()

This method returns an instance of org.h2.Driver. Any properties passed to this method are ignored.

Obtaining a DataSourceFactory

To obtain a DataSourceFactory, use any OSGi service lookup or injection method (Declarative Services, Blueprint) of your choice and specify the following properties:

  • objectClass = org.osgi.service.jdbc.DataSourceFactory

and at least one of

  • osgi.jdbc.driver.class = org.h2.Driver
  • osgi.jdbc.driver.name = h2

Required Maven Artifacts

  • org.ops4j.pax.jdbc:pax-jdbc-h2
  • com.h2database:h2
  • org.osgi:org.osgi.enterprise:4.2.0
  • An OSGi 4.2 framework