Search code examples
javajdbcdatasourceapache-karafpax

Bundle in Karaf using wrong pax-jdbc DataSource


We have different OSGi bundles loaded in Karaf 4.0.3 and multiple javax.sql.DataSource loaded by pax-jdbc-config. For example, there are configured two datasources to different databases:

karaf@root()> service:list DataSource
[javax.sql.DataSource]
----------------------
 dataSourceName = datasourceA
 felix.fileinstall.filename = file:/opt/karaf/etc/org.ops4j.datasource-dba.cfg
 osgi.jdbc.driver.name = mysql-pool
 osgi.jndi.service.name = datasourceA
 password = secretpassword
 service.bundleid = 207
 service.factoryPid = org.ops4j.datasource
 service.id = 276
 service.pid = org.ops4j.datasource.2ab06071-d3fa-4d7e-b508-19f36892cb7e
 service.scope = singleton
 url = jdbc:mysql://mysql.example.com/dba
 user = dbuser
Provided by : 
 OPS4J Pax JDBC Config (207)
Used by: 
 MODULE B (29)

[javax.sql.DataSource]
----------------------
 dataSourceName = datasourceB
 felix.fileinstall.filename = file:/opt/karaf/etc/org.ops4j.datasource-dbb.cfg
 osgi.jdbc.driver.name = PostgreSQL JDBC Driver-pool
 osgi.jndi.service.name = datasourceB
 password = secretpassword
 service.bundleid = 207
 service.factoryPid = org.ops4j.datasource
 service.id = 278
 service.pid = org.ops4j.datasource.a7b84423-a605-4010-9806-db7bcc0bccd4
 service.scope = singleton
 url = jdbc:postgresql://postgresql.example.com/dbb
 user = dbuser
Provided by : 
 OPS4J Pax JDBC Config (207)

My problem is that bundle MODULE B should use datasourceB. It is configured to use datasourceB in all source files and in blueprint.xml

...
<reference id="datasourceB" interface="javax.sql.DataSource"/>
...

I really don't understand, how easily can karaf bundles use different DataSource.

And when datasourceA config file is removed, then MODULE B correctly use datasourceB.

Is this issue about wrong order of config files or something?

EDIT :
I have found out that when I remove datasourceA config file and therefore Karaf removes that DataSource (not present in service:list DataSource), MODULE B correctly uses datasourceB.

But I still don't know why is this happening. Did I missed read some documentation about this? Is it pax-jdbc-config issue or Karaf issue?


Solution

  • When you reference the datasource, you can use a filter to indicate wich one you want :

    <reference id="datasourceB" interface="javax.sql.DataSource" filter="(dataSourceName=datasourceB)" />