Hi,
I'm having some problem trying to lookup data source from my stateless session EJB. Here is the scenario:
I wrote a class (i.e. called DSRegisterJNDI) that set up data source reference through JNDI, this class is a stand alone java class outside jboss.
I started jboss and then I execute DSRegisterJNDI to register the data source through JNDI. This is the way that I bind the data source:
=========================================================
// Set up environment for creating initial context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
env.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
Context ctx = new InitialContext(env);
// Register the data source to JNDI
// for application to use
ctx.bind("testtrk", ds);
=========================================================
Then I also wrote a test client that will try to do lookup the data source and do some SQL query. This test client is also a stand alone class, not part of jboss. Here is the way I do the lookup:
=========================================================
// Set up environment for creating InitialContext object
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
try {
// Retrieve the DataSource object that bound to the
// logical lookup JNDI name
ctx = new InitialContext(env);
ds = (DataSource) ctx.lookup("testtrk");
}
... do some SQL query ...
========================================================
The test client works fine, I got some result back from the data base.However, when I try to do the same lookup from my EJB(stateless session bean), it couldn't find the data source. It was giving me a NameNotFoundException. I assumed the EJB will be able to lookup the data source just like the test client does, but it fails.
Did I do something wrong here? Or am I misunderstanding the EJB or JNDI concept? Please help, thank you.
Andrew