views:

72

answers:

2

I have a project that is build up from several OSGi bundles. One of them is a generic Database bundle that defines a DataSource that can be used throughout the project. The spring bean definition of this service is:

<osgi:service interface="javax.sql.DataSource">
    <bean class="org.postgresql.ds.PGPoolingDataSource">
        <property name="databaseName" value="xxx" />
        <property name="serverName" value="xxx" />
        <property name="user" value="xxx" />
        <property name="password" value="xxx" />
    </bean>
</osgi:service>

Now, when using this DataSource is a different bundle, we get an error:

No suitable driver found for jdbc:postgresql://localhost/xxx

I have tried the following to add the org.postgresql.Driver to the DriverManager:

  1. Instantiated an empty bean for that Driver in the spring context, like this:
    <bean class="org.postgresql.Driver" />

  2. Instantiated the Driver statically in one of the classes, like this:
    Class.forName("org.postgresql.Driver");

    a. Also tried this while adding the org.postgresql package as DynamicImport-Package.

  3. Added a file META-INF\services\java.sql.Driver with the content org.postgresql.Driver

None of these solutions seems to help.

A: 

"No suitable driver" isn't the same thing as "can't find the driver".

This suggests to me that the JDBC driver class was loaded, but the syntax of the connection URL is incorrect.

duffymo
The url is generated by the PGPoolingDataSource, which is from the same bundle as the Driver. So this seems to me to be unlikely. Furthermore, the url that is shown is correct.Also, when I do not add the Driver anywhere, I get the same error.
Marc
Can't load the driver would give you a ClassNotFoundException. Something else is going on.
duffymo
A: 

I've found the solution, or at least a workaround. In my abstract DAO I've added the following:

static {
  try {
    DriverManager.registerDriver(new org.postgresql.Driver());
  } catch(SQLException ex) {
    LogFactory.getLogger(AbstractDAO.class).error("Could not load Driver", ex);
  }
}

This does seem to work. Does anyone know why? It is not that different from the Class.forName solution.

Marc