ferisj
ferisj

Reputation: 31

How to publish a database driver as an OSGi bundle

I have two or more non-specific JDBC drivers. I wish to use them in an OSGi container. Some of the drivers are from the same third party having only version differences and one or more conflicting classes, including the driver class and connection URL.

Explain how to make an OSGi bundle from each of these bundles.

I am using JBoss Fuse 6.2.1.redhat-117, Red Hat JBoss Developer Studio 10.4.0.GA (Eclipse with Fuse integration), Maven 3.3.9, Eclipse Maven integration, Windows 7.

I tried producing my own bundle for each JDBC driver using a two step process.

  1. Deploy the driver into Maven repository.
  2. Use osgi:install -s wrap:mvn:group/artifact/version to load driver into Fuse.

This does not work well enough. A JDBC driver works only if it is the only one of conflicting drivers installed into OSGi.

The best I can tell, the problem I have described is one that OSGi was created to solve. Because of this I believe there is a solution.

I am willing to go to great length to solve this problem (short of hard coding, diverging from generic implementation) because of the investment so far paid and the needed service that will come with a solution.

Upvotes: 3

Views: 894

Answers (1)

Tim Ward
Tim Ward

Reputation: 1199

The correct way to make a JDBC driver available in OSGi is to implement the JDBC service. This is very easy to do as the code is almost entirely generic.

The DataSourceFactory interface has four methods for producing

  • Driver
  • DataSource
  • ConnectionPoolDataSource
  • XADataSource

It's perfectly reasonable to throw SQLException if your factory can't provide one or more of these types.

Your DataSourceFactory implementation bundle should then either wrap the JDBC driver jar (for example if it's not already an OSGi bundle), or Import the necessary package(s) from the JDBC driver so that it can implement the DataSourceFactory methods (for example if the driver already has OSGi metadata, but doesn't provide the necessary service). The DataSourceFactory should then be registered as a service, which can easily be achieved using a bundle activator (H2 does this), or using a container like Declarative Services. This service advertises the driver class that it provides, alongside version and vendor information, using service properties.

The same template project can be used to produce multiple different bundles for different versions of the same driver, or by changing the referenced implementation. Using a tool such as the bnd-maven-plugin will make sure that your OSGi metadata is correct.

There are already some projects, such as PAX-JDBC that have attempted to provide these sorts of wrappers for JDBC drivers.

Upvotes: 5

Related Questions