Forums Register Login

Not able to perform JNDI lookup

+Pie Number of slices to send: Send
Hi Guys,
I want to use JNDI lookup to use a datasource created in Websphere 4.0 and I'm getting a null exception. All I'm doing in my dataconnection bean is

try {
InitialContext ic = new InitialContext();
datasource = (DataSource) ic.lookup("OracleDSN");
Con = datasource.getConnection();
} catch
Can anybody help me what should I write to access this datasource "OracleDSN". I don't have any other thing in my code, do I need to define the LDAp hash table etc.
Thanks
Neha
+Pie Number of slices to send: Send
what null point exception did you get?
regards,
-aao
+Pie Number of slices to send: Send
Hi Andrew,
Now I'm trying the following, and I'm still getting a null pointer Exception. I've just created the DSN but in the websphere documentation it says you have to bind your dsn to the JNDI how do you do that ? do you perform this programmatically or is there a way to do it in websphere.
java.util.Properties parms = new java.util.Properties();
parms.setProperty Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
Context ctx = new javax.naming.InitialContext(parms);
ds = (javax.sql.DataSource)ctx.lookup("jdbc/OracleDSN");
Con = ds.getConnection();
}catch (NullPointerException ex) {out.println("NullPointerException Exception while creating the datasource");}
catch (Exception ex) {out.println("Exception while creating the datasource");}

Thanks
Neha
+Pie Number of slices to send: Send
Hi,
please try following
try
{
//get naming context
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("insert jndi name");
//cast to Home interface
beanhomeinterfaceHome = (BeanHome) PortableRemoteObject.narrow(ref, BeanHome.class);
}
catch(Exception e)
{
e.printStackTrace();
}

This should work,Let me know the result
regards,
Ashish Saraf

Originally posted by Neha Sharma:
Hi Andrew,
Now I'm trying the following, and I'm still getting a null pointer Exception. I've just created the DSN but in the websphere documentation it says you have to bind your dsn to the JNDI how do you do that ? do you perform this programmatically or is there a way to do it in websphere.
java.util.Properties parms = new java.util.Properties();
parms.setProperty Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
Context ctx = new javax.naming.InitialContext(parms);
ds = (javax.sql.DataSource)ctx.lookup("jdbc/OracleDSN");
Con = ds.getConnection();
}catch (NullPointerException ex) {out.println("NullPointerException Exception while creating the datasource");}
catch (Exception ex) {out.println("Exception while creating the datasource");}

Thanks
Neha


+Pie Number of slices to send: Send
The NullPointerException is probably caused by trying to call getConnection() on the datasource which doesn't exist because you weren't able to correctly perform the JNDI lookup.
As to why the it's not working it could be a number of things:
Where is the lookup occuring from? If it's from within an EJB then you should be able to simply access the initial context. However, you will also need to have declared the datasource as a a resource-ref element within the deployment descriptor for the bean. If you're accessing it from outside a bean then you will need the initial context factory and url for the lookup.
Also double check that you've got the correct name. You need to look up the JNDI name for the datasource and not its real name.
------------------
Craig Berry
Author of Professional EJB
Skool. Stay in. Smartness. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1072 times.
Similar Threads
How to Bound JNDI names and references
JNDI Lookup help in websphere 4.0
Not able to perform JNDI lookup in my servlet
Need JNDI example
Class.forName("..");
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:10:33.