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
...
<jndi-name>jdbc/mysql</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password></password>
...
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\deploy5. 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 {
testDataSource();
}
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:C:\ws\nb\scrap\src\com\foo>
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):java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
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
Tags:
4 comments:
<use-java-context>false</use-java-context>
That line was the key!, thank you very much, it solved our problem.
The line
use-java-context ...
is something I should say as an opener for a drinker. Got my problem solved thanks a ton.
use-java-context/false/use-java-context
is the magic line .... thank you..
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!
Post a Comment