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:
Instantiated an empty bean for that Driver in the spring context, like this:
<bean class="org.postgresql.Driver" />
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
.
Added a file META-INF\services\java.sql.Driver
with the content org.postgresql.Driver
None of these solutions seems to help.
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.