If I understand correctly then you are trying to use the plain java approach of creating a data source using the Driver class. This does not work well in OSGi as the Driver approach expects a flat classloader that sees all classes. You can try to work around this but
it will suck.

Instead I recommend to use the DataSourceFactory approach like used by pax-jdbc. In OSGi a compliant database driver bundle will offer a DataSourceFactory as an OSGi service.
You can use this factory to create a DataSource.

In case of H2 this is really  easy you just need two bundles:
install -s mvn:org.osgi/org.osgi.service.jdbc/1.0.0
install -s mvn:com.h2database/h2/1.3.172

This is already enough to use the DataSourceFactory.

Pax jdbc makes this even easier by providing the feature pax-jdbc-h2 which can be directly installed in karaf.
It also provides wrappers for databases that do not yet support this.

On top pax-jdbc can create pooling and XA ready DataSources. You can even create such Datasources from a plain config without any coding.

See:
https://ops4j1.jira.com/wiki/display/PAXJDBC/Documentation
https://ops4j1.jira.com/wiki/display/PAXJDBC/H2+Driver+Adapter
https://ops4j1.jira.com/wiki/display/PAXJDBC/Pooling+and+XA+support+for+DataSourceFactory
https://ops4j1.jira.com/wiki/display/PAXJDBC/Pooling+and+XA+support+in+1.0.0

Christian

On 04.07.2017 23:35, smunro wrote:
Hello,

I've got a question regarding DBCP2 & Karaf.   When using DBCP2, I get a
driver not found error. If I use a straight Class.forName("org.h2.Driver")
it works as expected. I'm not looking to use fragments at the moment as I
need to get a working example quickly, but before I bin all the DBCP2 code I
have, does anyone know of a quick way to get the above working.

I've tried the DynamicImport-Package entry (which doesn't appear to appear
in intellisense as an option when adding it to the maven plugin). And while
the bundle does book up, none of the breakpoints are hit when running in
debug mode (when I take it out, the break points are hit), so I'm guessing
this isn't supported.

Can anyone suggest a quick way to get the DBCP2 BasicDataSource to work
correctly in an osgi bundle without it throwing an exception that it cannot
locate the driver. I know it's a classpath issue with the current thread,
I'm just looking for a fast way to get around it before moving onto a more
long term solution.

Stephen



--
View this message in context: 
http://karaf.922171.n3.nabble.com/DBCP2-Karaf-tp4050942.html
Sent from the Karaf - User mailing list archive at Nabble.com.


--
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
http://www.talend.com

Reply via email to