You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by smunro <st...@gmail.com> on 2017/07/04 21:35:44 UTC

DBCP2 & Karaf

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.

Re: DBCP2 & Karaf

Posted by smunro <st...@gmail.com>.
Hello jb,

I create the DBCP2 visit blueprint and inject them into my Dad classes. I am
not using any of the pax features at the moment, but that may change later.
It's just a case of getting this working now as a proof of concept.

Stephen



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

Re: DBCP2 & Karaf

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Stephen,

How do you use DBCP ? Via pax-jdbc, blueprint, your own bundle ?

If you use in your bundle, you probably need to set the Thread Context Class 
Loader (TCCL).

Regards
JB

On 07/04/2017 11:35 PM, 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.
> 

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: DBCP2 & Karaf

Posted by Timothy Ward <ti...@paremus.com>.
As I did say in a previous email chain when you asked this question, the DBCP BasicDataSource does things that are fundamentally not going to work in OSGi.

When you pass BasicDataSource a driver class name it is going to do something like:

    Class.forName(suppliedDbDriverClassName);


This will attempt to load the Driver class reflectively using the class loader of the DBCP bundle *not* the bundle that you’ve written. You can do as much to your own bundle manifest as you like and it won’t make a difference because DBCP is the one failing to load the driver.

When you write the code yourself I imagine that you’re directly referencing the class by calling something like:

    DriverManager.register(new org.h2.Driver());

or even just instantiating it as a blueprint bean directly. This creates a dependency from your code on the org.h2 package. Your build tool sees this and makes sure that the manifest is set up appropriately, hence it works.

As Christian and I have both said previously, if you want to do this properly in OSGi you should really be using a DataSourceFactory service to create your Driver/DataSource.

Then, once you have your Driver/DataSource you need to configure it using the DBCP *advanced* setup, not the BasicDataSource. Given that this seems to be causing you a lot of problems, have you considered using an alternative, for example HikariCP? Note that you will still need to inject a dataSource, not supply a class name.

Regards,

Tim


> On 5 Jul 2017, at 08:26, smunro <st...@gmail.com> wrote:
> 
> The problem with the data source approach is that I got it working with h2 in
> the past, but I have never managed to get it working for sqlserver/jtds. I
> tried the jtds feature but never got it to work. After various attempts I
> was told to just get it working via plain Java, which I can understand as
> I've been trying for a few weeks now with little success. I've not tried the
> fragment approach as I am out of time for adding extras. I just need the
> minimum working and can revisit it later on.
> 
> 
> 
> --
> View this message in context: http://karaf.922171.n3.nabble.com/DBCP2-Karaf-tp4050942p4050952.html
> Sent from the Karaf - User mailing list archive at Nabble.com.


Re: DBCP2 & Karaf

Posted by smunro <st...@gmail.com>.
Thanks Christian,

I'll give that a try and let you know shortly. 

I noticed that in blueprint, rather than setting DriverClassName for the
BasicDataSource, I had better success with defining a bean of type
org.h2.Driver and setting that as a property to setDriver.

This at least gets round the ClassNotFound error when calling
dataSource.getConnection(), but to be completely sure, I'll need to try
hitting it from another bundle to be sure. 






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

Re: DBCP2 & Karaf

Posted by Christian Schneider <ch...@die-schneider.net>.
I just looked into the pax-jdbc code.

There already is a pax-jdbc-jtds feature but it does not contain the 
actual db driver. I added the driver bundle and deployed a new snapshot.

You can try it like this:
feature:repo-add pax-jdbc 1.2.0-SNAPSHOT
feature:install pax-jdbc-jtds

I do not have a sqlserver installation .. so I can not actually test it. 
If you find issues I can try to fix them.

Best

Christian

On 05.07.2017 09:26, smunro wrote:
> The problem with the data source approach is that I got it working with h2 in
> the past, but I have never managed to get it working for sqlserver/jtds. I
> tried the jtds feature but never got it to work. After various attempts I
> was told to just get it working via plain Java, which I can understand as
> I've been trying for a few weeks now with little success. I've not tried the
> fragment approach as I am out of time for adding extras. I just need the
> minimum working and can revisit it later on.
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/DBCP2-Karaf-tp4050942p4050952.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


Re: DBCP2 & Karaf

Posted by smunro <st...@gmail.com>.
The problem with the data source approach is that I got it working with h2 in
the past, but I have never managed to get it working for sqlserver/jtds. I
tried the jtds feature but never got it to work. After various attempts I
was told to just get it working via plain Java, which I can understand as
I've been trying for a few weeks now with little success. I've not tried the
fragment approach as I am out of time for adding extras. I just need the
minimum working and can revisit it later on.



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

Re: DBCP2 & Karaf

Posted by Christian Schneider <ch...@die-schneider.net>.
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