chatanyday
chatanyday

Reputation: 145

Get Datasources programmatically

I have a web-app in Java/Java EE deployed on any application server/web server. I would like to get all the datasources configured on this server in my application.

Does anyone have any ideas to achieve it? I can get those through WLST ant tasks. But I need to get them programatically.

Upvotes: 1

Views: 1547

Answers (2)

Krushna
Krushna

Reputation: 6060

If your datasources are configured with JNDI then you can list the context and can get all the names (more from here) with Context.list() method and from those name you can find all the datasources

the Context.list() returns an enumeration of NameClassPair. Each NameClassPair consists of the object's name and its class name. So just iterate it and check the class name for java.sql.DataSource and then get the object name to retrieve it.

Upvotes: 1

Kris
Kris

Reputation: 5792

With JBoss you can do the following (assuming JMX is available):

Context ctx = new InitialContext();
MBeanServerConnection mconn = (MBeanServerConnection)ctx.lookup("jmx/invoker/RMIAdaptor");
ObjectName name = new ObjectName("jboss.jca:service=DataSourceBinding,*");
Set s = mconn.queryMBeans(name, null);

Where s is an mbeans collection.

Upvotes: 0

Related Questions