Friday, July 24, 2009

Invoking Datasource Remotely for JBOSS

DataSources are not exposed remotely by default.

In JBoss4 and above, you can set the following attribute to false in the datasource
definition: in oracle-ds.xml to lookup datasource remotely ( typically from external
java class from main method as shown below) <use-java-context>false</use-
java-context> if datasource is not invoked remotely then you can
comment the above line in oracle-ds.xml


 public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs"
,"org.jboss.naming");
props.setProperty("java.naming.provider.url",
"127.0.0.1:1099");

InitialContext ctx = new InitialContext(props);
MyBeanRemote bean=(MyBeanRemote)ctx.lookup("MyBean/remote");
bean.doSomething();
DataSource ds = (DataSource)ctx.lookup("OracleDS");
Connection con = ds.getConnection();
ResultSet rs = con.createStatement()
.executeQuery("SELECT NAME FROM EMPLOYEE");
while (rs.next()) {
System.out.println("I got " + rs.getString(1));
}
rs.close();
con.close();
} catch (Exception e) {
.......
}
}

Note : if the datasource is not looked-up
remotely then the look-up might change to ctx.lookup("java:OracleDS") or
ctx.lookup("java:/OracleDS") depends on whatever DS is bound to , check the server
start up log

No comments:

Post a Comment

Create ElasticSearch cluster on single machine

I wanted to figure out how to create a multi-node ElasticSearch cluster on single machine. So i followed these instructions First i did...