OSGi JDBC Service

716 views
Skip to first unread message

Pelle

unread,
Jul 8, 2011, 2:57:30 AM7/8/11
to H2 Database
Hi!

I'm working in a project where we consider to use H2.

In this project we use OSGi. H2 do have the appropriate fields in the
manifest file to qualify as a valid OSGi bundle. However, H2 does not
currently implement the JDBC Service defined by the OSGi Enterprise
Specification: http://www.osgi.org/Download/Release4V42?info=nothanks

Are there any plans to add this to H2?

BR/ pelle

Thomas Mueller

unread,
Jul 8, 2011, 4:07:11 AM7/8/11
to h2-database
Hi,

As far as I know, H2 can be used within OSGi without much trouble
(unlike other databases), as the jar file has a Bundle-Activator (the
org.h2.util.DbDriverActivator). So... are you sure you *need* the OSGi
JDBC Services Specification?

Is there a database that _does_ support the OSGi JDBC Services
Specification out of the box? So far I found this:

https://issues.apache.org/jira/browse/ARIES-122

http://svn.apache.org/viewvc/incubator/aries/trunk/samples/transaction-sample/osgi-jdbc-api/src/main/java/org/osgi/service/jdbc/DataSourceFactory.java?view=markup&pathrev=906494

http://svn.apache.org/viewvc/incubator/aries/trunk/samples/transaction-sample/osgi-jdbc-derby/src/main/java/org/apache/aries/samples/osgijdbc/derby/DerbyActivator.java?view=markup&pathrev=906494

https://bugs.eclipse.org/bugs/show_bug.cgi?id=328310

http://laeubi-soft.de/artikel/osgi/jdbc-unter-osgi-nutzen.html

Please tell me if you are interested to create a "JDBC Services
Specification" patch for H2. I wonder if it would cause no problems
for OSGi frameworks that don't support this specification.

Regards,
Thomas

Pelle

unread,
Jul 8, 2011, 6:16:29 AM7/8/11
to H2 Database
Hi.

Yes, it is possible to use H2 within OSGi as is. But if H2 would
register itself as a JDBC service in OSGi things can be made cleaner.

Admittedly few, if any, existing JDBC drivers implement the spec. So
far I've implemented a simple wrapper for DB's that we have evaluated
that implements the JDBC Service. I'll see if I can make the
implementation more complete and integrated with H2, and wrap it up as
a patch. I can't think of anything would break if this was added.

BR/ pelle

On Jul 8, 10:07 am, Thomas Mueller <thomas.tom.muel...@gmail.com>
wrote:
> Hi,
>
> As far as I know, H2 can be used within OSGi without much trouble
> (unlike other databases), as the jar file has a Bundle-Activator (the
> org.h2.util.DbDriverActivator). So... are you sure you *need* the OSGi
> JDBC Services Specification?
>
> Is there a database that _does_ support the OSGi JDBC Services
> Specification out of the box? So far I found this:
>
> https://issues.apache.org/jira/browse/ARIES-122
>
> http://svn.apache.org/viewvc/incubator/aries/trunk/samples/transactio...
>
> http://svn.apache.org/viewvc/incubator/aries/trunk/samples/transactio...

Thomas Mueller

unread,
Jul 8, 2011, 6:26:37 AM7/8/11
to h2-database
Hi,

> But if H2 would register itself as a JDBC service in OSGi things can be made cleaner.

Yes, of course.

> I'll see if I can make the
> implementation more complete and integrated with H2, and wrap it up as
> a patch.

That would be great! Even if the patch isn't perfect.

> I can't think of anything would break if this was added.

That's the most important aspect currently.

Thanks a lot for your help!

Regards,
Thomas

laeubi

unread,
Aug 28, 2011, 8:47:52 AM8/28/11
to H2 Database
Hi Thomas,

> I wonder if it would cause no problems
> for OSGiframeworks that don't support this specification.

The best option for this is to integrate the JDBC Service interface
into the bundle (if you can live with the OSGi licence requirements
for this) which can be downloaded on the specifikation page under the
"Enterprise Specification" section, export the package as well as add
it as an imported package with resolution:=optional.

So two cases can apply:
1) The OSGi Framework support the JDBC Service: Your driver will
import the package from the framework
2) The OSGi Framework does -NOT- support the JDBC Service: The
fallback interface of your internal interface will be used and your
drive becomes the "implementation" of the JDBC service

(note: In fact the JDBC service does not involve any implementation,
just providing the interface, thats why i quoted the 'implementation'.
I don't know why not every OSGi framework includes the JDBC service
since there is nearly no effort for including it)

Alternativly, you can just import with resolution:=optional, and just
guard your service registration code this way that it silently fails
when the interface can't be loaded at runtime.

If you want to learn more about that I can forward you to the OSGi
core specifiaction part 3.6.3 Optional Packages.

Thomas Mueller

unread,
Aug 30, 2011, 1:51:43 PM8/30/11
to h2-database
Hi,

This all sounds quite complicated, and easy to get wrong.

> The best option for this is to integrate the JDBC Service interface
> into the bundle (if you can live with the OSGi licence requirements
> for this)

It sounds wrong to add an OSGi _interface_ directly to the H2 jar
file. It would be like adding java.sql.* to the H2 driver. It just
sounds wrong.

Regards,
Thomas

laeubi

unread,
Aug 30, 2011, 1:58:00 PM8/30/11
to H2 Database
Hi Thomas,

> It sounds wrong to add an OSGi _interface_ directly to the H2 jar

As mentioned, you always can use dynamic imports but have to live with
the fact of a class not found exception when loading the class.
As an alternative, you can always supply a seperate bundle with a
static import which just import the JDBC service as well as the h2
bundle.
Thats the way I implemented the MySQL Wrapper ans should be considered
as the best practise from the OSGi point of view, some peaple just
don't like havin another bundle where in fact is no problem with that
at all.

> This all sounds quite complicated, and easy to get wrong.
Its not that complicated at all. If you need any help with that let me
know, be the first database to support OSGi JDBC service nativly ;)

laeubi

unread,
Sep 2, 2011, 7:03:24 AM9/2/11
to H2 Database, thomas.to...@gmail.com
On Jul 8, 8:57 am, Pelle <per.otterst...@ericsson.com> wrote:
> In this project we useOSGi. H2 do have the appropriate fields in the
> However, H2 does not currently implement the JDBC Service
> defined by theOSGiEnterprise Specification

I have added a Wrapper class for H2, see http://laeubi-soft.de/downloads/osgi.html

@Thomas
Feel free to include the example into H2 and modify it for anything I
have overseen.
One note: If there is no special problem with this, the H2 jar should
be named org.h2_<version>.jar (e.g. org.h2_1.3.159.jar) to be more
confomant with common OSGi bundle naming, I think this wont add any
problems for non OSGi users.

Reply all
Reply to author
Forward
0 new messages