Database Support in Apache Karaf

From summer 2015, Roboconf is based on Karaf 4.x.
For the moment, it does not use any database, although such a support has been studied.

For the record, here are the required steps to embed, define and use a database in Roboconf’s DM.
We here assume we want to use a H2 database.

Complete the Karaf Distribution

Database support goes through PAX-JDBC (which is the new standard way for Karaf 4.x, unlike Blueprint which was the main option in Karaf 3.x). So, update the pom.xml file of the Karaf distribution. Make sure it installs the following boot features


Create a Datasource

Make sure the following file will be added in the etc directory of the Karaf distribution.
This file should be named org.ops4j.datasource-roboconf-dm.cfg. What is important is its name begins with org.ops4j.datasource-. = H2
databaseName = roboconf-dm
url = jdbc:h2:${}/roboconf-dm-database
user = roboconf
password = roboconf
dataSourceName = roboconf-dm-db

Using it in the DM

First, define a new injected field in the Manager class.

// A javax.sql.DataSource object that will be injected with iPojo.
protected DataSource dataSource;

Then, make sure to import the javax.sql packages.
In the POM file, you should see…


Eventually, make the DataSource service (created by PAX-JDBC) injected in the DM by iPojo.
To achieve it, update the metadata.xml file.

<component classname="" name="roboconf-dm" public="false">
		<requires field="dataSource" optional="false" filter="(dataSourceName=roboconf-dm-db)" />

That’s it.
PAX-JDBC will create the DataSource and register it as a service. It will also handle reconfiguration.
And iPojo will inject the service and make it available in the DM. The DM only has to use it to interact with the database.