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 http://servicemix.apache.org/

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

image

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:

osgi.jdbc.driver.class=com.microsoft.sqlserver.jdbc.SQLServerDriver-pool
osgi.jdbc.driver.name = sqljdbc4-pool

databaseName=Calamp_CMS
url=jdbc:sqlserver://im1stageagl.im1.local:1433;multiSubnetFailover=true;integratedSecurity=true

dataSourceName=CALAMP_CMS

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.

http://servicemix.apache.org/docs/7.x/users-guide/wrapper.html

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.

image

type:

karaf@root> feature:install jdbc

karaf@root> feature:install jpa

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

image

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

image

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s