Access JBoss DataSource Remotely from Java Applications

Sometimes people need to access appserver datasource remotely from applications outside J2EE containers. JBoss adds this feature as of 4.0.0, according to JBoss Wiki page. Here are the steps to remotely access a mysql datasource deployed in JBoss 4.0.1:

1. start MySQL server. If a test database is not present, create it (inside mysql console, run create database test;)

2. put MySQL Connector/J jar (mysql-connector-java-5.0.3-bin.jar) in %JBOSS_HOME%\server\default\lib

3. back up and edit %JBOSS_HOME%\docs\examples\jca\mysql-ds.xml

Set use-java-context to false. This tells JBoss to open this datasource for remote access, and its JNDI name will be used for remote lookup without using java:/ prefix.

4. copy modified mysql-ds.xml to %JBOSS_HOME%\server\default\deploy

5. code java client:
package com.foo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceTest {
public static void main(String[] args) throws Exception {

private static void testDataSource()
throws NamingException, SQLException {
final String sql = "select version()";
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("jdbc/mysql");
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("Query '" + sql + "' returned " + rs.getString(1));
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
To compile it:
javac -d C:\ws\nb\scrap\build\classes DataSourceTest.java
6. create a jndi.properties and put it in classpath (C:\ws\nb\scrap\build\classes):
7. run it with JDK 6 so that I can use wildcard (*) in classpath and don't have to figure out which jboss jars to include (see my previous post on this feature):
java -cp C:\ws\nb\scrap\build\classes;%JBOSS_HOME%\lib\*;%JBOSS_HOME%\server\default\lib\* com.foo.DataSourceTest
Query 'select version()' returned 5.0.18


Jon Ander said...


That line was the key!, thank you very much, it solved our problem.

Santhoooooshhhh said...

The line

use-java-context ...

is something I should say as an opener for a drinker. Got my problem solved thanks a ton.

Anonymous said...

is the magic line .... thank you..

TF said...

You guys should understand the meaning of the different JNDI name spaces: global, local (java:/), and java:comp/env/

Using Datasources from remote is not recommended and might lead into trouble easily because the app server looses control over the connections. Be warned!

Olivier Sinnaeve said...

What TF says is absolutely correct !!! Once the maximum connectioin in the pool are reached no other connections can be obtained. For the reason that the server does not manage the connections anymore, he is not aware that you close the connection.

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Designing Company

Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India