Can not find Driver when using generic database bundle

1.4k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

2
On

"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.