How to connect to JNDI bound datasource from WSO2 Data Services?

Data Services allow you to define a JDBC connection pool. But what if you already have a connection pool defined somewhere else & want to use it?

All you have to do is expose that pool (Datasource) as a JNDI resource. WSO2 Data Services allows you to connect to Datasources exposed as JNDI resources.

In the following example, I explain how  to create a Datasource on a Jboss-4.0.2 instance & connect to that from a data service. (I am going to use 'default' configuration of jboss-4.0.2, hence all the changes should be done inside jboss-4.0.2/server/default folder).

Step 1: Sample Database

For this example, I use the database created during Step 1 of 'How to RESTify your data with WSO2 Data Services?'.

Step 2: Creating Datasource in Jboss

Create a xml file (name - 'mysql-ds.xml') containing following & drop it to $JBOSS_HOME/server/default/deploy folder.

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <use-java-context>false</use-java-context>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/DATASERVICE_SAMPLE</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>dsuser</user-name>
    <password>user123</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

More details on creating a Datasource on Jboss can be found here & here.

Step 3: MySql JDBC driver

Add MySQL JDBC driver to jboss-4.0.2's classpath. You can simply do this by dropping (mysql-connector-java-VERSION-bin.jar) to jboss-4.0.2/server/default/lib folder.

Step 4: Start jboss-4.0.2

execute

$JBOSS_HOME/bin/run.sh

During start up you will see a message similar to following, which tells you that Datasource is configured correctly.

06:03:24,802 INFO  [WrapperDataSourceService] Bound connection factory for resource 
adapter for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=MySqlDS 
to JNDI name 'java:MySqlDS'

Step 5: Jboss JNP client libraries

WSO2 Data Services need to have Jboss's jnp client libraries within it's classpath. In order to do this, copy jboss-4.0.2/client/jbossall-client.jar to $wso2-dataservices-VERSION-home/lib/extensions.

Step 6: Sample Dataservice configuration file

Listed bellow is a sample Dataservice configuration file that connects to the Jboss Datasource we defined in previous steps. As you can see, following properties are being used to create the bridge between Jboss & WSO2 Data Services.

<property name="jndi_context_class">org.jnp.interfaces.NamingContextFactory</property>
<property name="jndi_provider_url">jnp://localhost:1099</property>
<property name="jndi_resource_name">MySqlDS</property>

Note : 1099 is the default jnp port of jboss. If you have changed this, use the new one.

<data name="TestJNDI">
    <config id="conA">
        <property name="jndi_context_class">org.jnp.interfaces.NamingContextFactory</property>
        <property name="jndi_provider_url">jnp://localhost:1099</property>
        <property name="jndi_resource_name">MySqlDS</property>
        <property name="jndi_username"/>
        <property name="jndi_password"/>
    </config>
    <query id="query1" useConfig="conA">
        <sql>select productCode,buyPrice from Products</sql>
        <result element="Products" rowName="Product">
            <element name="Code" column="productCode"/>
            <element name="Price" column="buyPrice"/>
        </result>
    </query>
    <operation name="operation1">
        <call-query href="query1"/>
    </operation>
</data>
sumedha's picture

Data Services and HA-JDBC

I deleted a valuable comment from 'johnwang'while deleting a spam comment. Adding it back. ========================================================================================== Submitted by johnwang on July 9, 2009 - 11:18. It would be interesting if one could get WSO2 DS to work with HA-JDBC. A HA-JDBC cluster can wrap one or more datasources and the cluster itself can be made available to JNDI context. It just seems there is no straightforward way to configure WSO2 DS for this. HA-JDBC works fine on Tomcat as long as the following are set: <ha-jdbc> <!-- ... --> <cluster balancer="..." dialect="PostgreSQL" default-sync="..." transaction-mode="..."> <database id="database1"> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql://server1/database</url> <user>postgres</user> <password>password</password> </datasource> <database id="database2"> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql://server2/database</url> <user>postgres</user> <password>password</password> </datasource> </cluster> </ha-jdbc> server.xml <Context> <!-- ... --> <Resource name="jdbc/cluster" type="javax.sql.DataSource" username="postgres" password="password" driverClassName="net.sf.hajdbc.sql.Driver" url="jdbc:ha-jdbc:cluster1"/> <!-- ... --> </Context> web.xml <web-app> <!-- ... --> <resource-env-ref> <resource-env-ref-name>jdbc/cluster</resource-env-ref-name> <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type> </resource-env-ref> <!-- ... --> </web-app> I'd appreciate it if you could provide any leads on this.
daggett's picture

Use ESB Datasource from WSAS

Is it possible to use WSO2ESB Datasource from WSAS? We have quite large set of data-services in our WSAS and if database connection parameters has beenn changed, we need to redeploy everything... It would be nicer to integrate datasources into WSAS and use it to manage connection to the databases.
sumedha's picture

Hi Dmitry, We have had

Hi Dmitry, We have had several requests in this area & this is a task item we have planned. We are trying to push this into forth coming release (end of November). But due to other work items, we sometimes might not be able to do this before November release. If this is the case, we will surely include this for the next release. I will keep you posted of the status. Thanks, /sumedha
andrewm.graticule.com's picture

Documentation?

Hi Sumedha, I am very interested in your comment about being able to share a datasource between the ESB and a WSAS service. Can you please link to some form of documentation that describes the configuration of such a scenario? Thank you, - Andrew
library project main code
Learn Cloud
Learn
Cloud

The WSO2 Application Server is a reliable application server that can host your enterprise web applications. The WSO2 Application Server as a Service is offered in StratosLive, the WSO2 Platform as a Service. This article explains how a simple web application can be developed and deployed from Carbon Studio to the WSO2 Application Server...

Latest Webinar
Different groups within an organization need to monitor different Key Performance Indicators (KPIs) - An operations team will be interested in the response times of business services and loads of each service,..
Thursday, February 9th 2012, 09.00 AM (PST)

Thursday, February 9th 2012, 10.00 AM (GMT)