Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to do JNDI lookup?

 
Anuj Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having a JNDI named as jdbc/raDataSource.Now how to do the JNDI lookup??HOw will my application get the datasource which I have created?Also,what does the following mean: "java:comp/env/jdbc/raDataSource"??Where&in which class of my app will I write the code for the JNDI lookup?
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do a JNDI lookup you have to aquire the naming context and use that to access any resource.

Example:


 
Anuj Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijitha for the reply.What does this mean:java:comp/env?Anyways..moreover now how will I tell the webapp abt the datasource?Is it in the web.xml smthg like dis:
<res-ref-name>jdbc/raDataSource</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>
Also,in which part of my code will I write the code for JNDI lookup?

Thanks
Anuj
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java:comp/.... means the JNDI name for a particular resource, as for here, it is used for the data source. And your XML structure is correct for configuring a datasource to be used with JNDI, but that is 'not' to be used in web.xml descriptor. You need a separate descriptor like ejb-jar.xml (which will be deployed in the EJB container) to use this XML structure. However, with EJB 3.0, you don't need to use these XML configuration or even looking up for JNDI, because you can use annotations instead.

Devaka
 
Ray Blue
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does comp/env also work with windows?? my app ran under jboss. and in glassfish i managed to get it deployed too. even my stateless session beans work. but i am not allowed to use DI for stateful session beans. so I would have to use lookup. But lookup works different in Glassfish than in JBoss Some "full" example would be great. I already added some ConnectionPool successfuly and some jdbc/mydb connection resource. The only thing that i think is missing is the correct JNDI lookup
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic