Configuring Sql Server JDBC as a JNDI ServiceMix Datasource

The following steps will outline how I have configured JNDI in  ServiceMix to successfully create a datasource for use within your OSGI packages.

Install Servicemix by unzipping the release from

Verify that your install was successful buy running Servicemix.bat in the bin folder.


This should leave you at the karaf@root> command prompt.

run the following commands:

feature:install webconsole

feature:install transaction jndi pax-jdbc-h2 pax-jdbc-pool-dbcp2 pax-jdbc-config

You will now be able to point your browser to http://localhost:8181/system/console

login with username karaf, password karaf

browse to http://yourhost:8181/system/console/features

scroll down to pax-jdbc-mssql and click on install

copy sqljdbc42.jar to the /deploy folder

create a configuration file with the database url you want to configure JNDI for.

example: = sqljdbc4-pool



save this file as yourdatasource.cfg and store file in the /etc folder.

Note on the above file. you will see that currently I have it configured to use integratedSecurity. This requires that you properly install sqljdbc_auth.dll  in your path. Another assumption is that you login as the windows authentication user when you start servicemix. If you configure servicemix as a service which is very easy to do. Just makes sure it runs under the windows account that is authenticating to your database. The following documentation shows how to configure servicemix as a service.

return to the karaf command shell.

karaf@root> service:list DataSourceFactory should show you that the sqljdbc4 driver is installed and running.

You should also have pooled and xa drivers listed if you would like to change your configuration file in etc to use those drivers.



karaf@root> feature:install jdbc

karaf@root> feature:install jpa

karaf@root> ds-list  (This should now show you the datasourceservice connecting with OK)


karaf@root> jndi:names (this should show you your configured JNDI service name datasource.


now we have this configured we can reference the JNDI service as follows in Java OSGI packages.

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceFactory {
    private static final Logger LOG = Logger.getLogger(DataSourceFactory.class.getName());

    public static DataSource createDataSourceByName(String name) {
        try {
            LOG.log(Level.FINER, “Finding DataSource:  ” + name);
            return (DataSource)new InitialContext().lookup(“osgi:service/” + name);
        catch (NamingException e) {
            LOG.log(Level.FINEST, “DataSource name ” + name + ” not found”, e);
        catch (Exception e) {
            LOG.log(Level.SEVERE, “Can’t create data source ” + name, e);
        return null;


The name variable would be your datasource name shown on the previous jdni:names list you had configured.

I hope this saves you some time in getting sql server jdbc connectivity to your OSGI projects.

This entry was posted in Sql Server. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s