Reputation: 11
I want to work in java OSGI bundle (Felix) with PostgreSQL database. I have this dependency in the pom:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1207</version>
</dependency>
And this class is in PostgreSQL bundle.
`public class PostgreSQLDBDAOFactory extends DAOFactory {
Connection conn = null;
private static final Logger LOG = Logger.getLogger(PostgreSQLDBDAOFactory.class.getName());
public PostgreSQLDBDAOFactory() throws ClassNotFoundException {
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/CarsAPJ";
String user = "petr";
String password = "pass";
conn = DriverManager.getConnection(url, user, password);
...}
...
}
...
}`
Building of OSGI bundle is OK, but after starting app in Felix, I have this error. I have this bundle in modules and in felix configuration script.
Jan 18, 2016 1:13:02 AM org.lib.postgresqldb.PostgreSQLDBDAOFactory <init>
SEVERE: java.lang.ClassNotFoundException: org.postgresql.Driver not found by org.lib.postgresqlDB [4]
Do you know anybody, how to resolve it, please?
Upvotes: 1
Views: 136
Reputation: 19626
When you load the DriverManager class in your bundle then this is delegated to the classloader of the system bundle (felix framework). So the DriverManager will only see the packages that are visible to the system bundle. The DB drivers are not visible there of course.
To make it short. You should not rely on the DriverManager to make a DB connection. Instead use a DataSource.
The best way to get a DataBase connection is to use pax-jdbc. It provides a way to create and publish DataSources as OSGi services.
So you can just refer to the service from your bundle and create a Connection from it. This makes sure your bundle does not depend on the specific database and allows the admin to easily change the DataSource config.
I also got some tutorials for Apache Karaf that show how to cleanly build database based applications on top of OSGi.
Upvotes: 0