1

Possible Duplicate:
What is resource-ref in web.xml used for?

I added the following tag in context.xml of Tomcat :

    <Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="org.apache.derby.jdbc.ClientDriver" url="jdbc:derby://localhost:1527/poll_database;create=true"
    username="suhail" password="suhail" maxActive="20" maxIdle="10" maxWait="-1" />

The above tag gets read,when the server is started and the application is deployed. I also added the following in web.xml of my project :

 <resource-ref>
  <description>my connection</description>
  <res-ref-name>jdbc/PollDatasource</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

What is the above tag meant for ?

Because when i write a statement like:

DataSource ds = (DataSource)envContext.lookup("jdbc/PollDatasource")

doesn't it directly look into the context.xml of the server ? I know I am wrong.In this case please help me understand what is the resource-ref tag meant for in web.xml ?

Community
  • 1
  • 1
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

2 Answers2

3

you have to lookup with following code

DataSource ds = (DataSource)envContext.lookup("java:comp/env/jdbc/PollDatasource");

within your web application,

The JNDI context is different within application and outside of the application, in this case inside context.xml, where you defined your resources.

All the resouces defined in resouce-ref section are mapped to application's Env Context. so , application can access those resources. The cross-reference between application context and container context is done at the deployment time.

awsbz77
  • 649
  • 5
  • 8
2

I believe its meant as a "hint" or live documentation that your web application expects to have access to JNDI resource with the specific name and other attributes. The context.xml file is a Tomcat artifact and while many app servers embed the Tomcat engine, it is far from universal.

Robert
  • 2,441
  • 21
  • 12