com.mysql.jdbc.Driver not found by bundle in karaf

3.7k views Asked by At

I have a bundle which is using mySQL.jdbc.driver , I have given the dependency in pom file,import-packages and copied mysql connector in lib, deploy folder , installed mysql driver using install wrap:mvn:mysql/mysql-connector-java/5.1.17, i mean I did all possible as per me, but still I am getting error message as under, FYI I am using karaf-4.0.0.M2, please help its road block for me,

errorMessage = JDBC driver name is missing or not valid, cause = java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found by portframeworkservice [this is my bundle name]

Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found by portframeworkservice [145]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
1

There are 1 answers

4
Christian Schneider On

Database drivers in OSGi are always a bit difficult as the most common approach outside OSGi using DriverManager is not really suitable for OSGi.

The best approach in OSGi is to use a DataSourceFactory which is standardized by the OSGi alliance. Some database drivers already offer this. For others pax-jdbc provides an adapter. For mysql the second case applies.

So my advice is to use pax-jdbc like this:

feature:repo-add pax-jdbc 0.6.0
feature:install pax-jdbc-mysql

If you then do

karaf@root()> service:list DataSourceFactory
[org.osgi.service.jdbc.DataSourceFactory]
-----------------------------------------
 osgi.jdbc.driver.class = com.mysql.jdbc.Driver
 osgi.jdbc.driver.name = mysql
 service.bundleid = 52
 service.id = 110
 service.scope = singleton

So the feature offers you a ready to use DataSourceFactory. You can bind it as an OSGi service and create a DataSource from it. To make it even more convenient you can also use pax-jdbc-config and pax-jdbc-pool to create a ready to use pooled DataSource as a service.

I have a complete tutorial for accessing databases in OSGi that shows this approach.