• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to perform JNDI lookup

 
Neha Sharma
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrew Ao
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what null point exception did you get?
regards,
-aao
 
Neha Sharma
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Saraf
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Craig Berry
Author
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!