Search code examples
javahibernatepostgresqlosgic3p0

PostgreSQL connector on OSGi


I'm working on an OSGi application using Karaf.

This application connects to a postgresql database using Hibernate 4.3.

My application works when I use the hibernate default connection pool but JBoss advices not to use it on a prod system. So I'm trying to use a c3p0 connection pool.

I use the following bundles:

<bundle wrap="yes">hibernate-jpa-2.1-api-1.0.0.Final</bundle>
<bundle wrap="no">hibernate-core-4.3.7.Final</bundle>       
<bundle wrap="no">hibernate-entitymanager-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-c3p0-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-envers-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-osgi-4.3.7.Final</bundle>

In order to use c3p0, I also add the requiered third party library:

<bundle wrap="yes">c3p0-0.9.2.1</bundle>

wrapoption is set to "yes" as it's a simple jar file.

All those bundles are correctly loaded by karaf.

Now my problem is to add the PostgreSQL driver: I have postgresql-9.3-1102.jdbc41.jar, which is not a bundle (no manifest.mf file).

I tried to:

  • add it to the classpath
  • wrap it using <bundle wrap="no">postgresql-9.3-1102.jdbc41.jar</bundle> in bundles.xml
  • wrap it throught eclipse using this tutorial
  • make it into a fragment, hosted by my dao-layer bundle

But nothing works. Each times c3p0 start the connexion pool and when it comes to find the right driver, it fails with this message:

java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:278)[:1.7.0_67]
    at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:240)[98:wrap_file_D_container_karaf_bundles_c3p0-0.9.2.1.jar:0]
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)[98:wrap_file_D_trunk_container_karaf_bundles_c3p0-0.9.2.1.jar:0]

Looking for this kind of problem, I found there are 2 reasons this may append:

  • the hibernate.connection.url may be wrong. But that's not my case. I checked it multiple times.
  • The postgresql driver is not loaded

Is there any magical trick to use c3p0 with hibernate on a OSGi environment?


Solution

  • Postgresql now delivers an OSGi compatible driver. It can be downloaded from maven central: mvn:org.postgresql/postgresql/9.4-1203-jdbc41 In Apache karaf it can be installed with

    feature:install pax-jdbc-postgresql

    The bundle offers a DataSourceFactory as an OSGi. This can be used to create a DataSource programmatically.

    Use service:list to see the properties of this DataSourceFactory.

    Alternatively you can use pax-jdbc-config to create a DataSource from a config using pax-jdbc-config. Use osgi.jdbc.driver.class=org.postgresql.Driver to identify the Postgresql driver in the config. If the config can be processed correctly there will be a service of type DataSource that you can refer to in your code.

    I got a complete example using derby and hibernate that should be quite easy to adapt to postgresql.