Featured Post

The great debacle of healthcare.gov

This is the first time in history when the president of the United States of America, or probably for any head of state around the world,...

Wednesday, November 14, 2007

Datasource scope issue in j2ee application

Before jump into the issue, lets discuss how JNDI naming works.

The JNDI lookup for any resources is resolved relative to the context defined in the Initial Context (which can be created from various jndi api like InitialContext, InitialLdapContext etc.). The exception happens only if the URL string is specified to lookup any resources (which i. URL format is defined as:
scheme_id:scheme-specific-part

J2EE specification implementation provides a root URL context as java: which is made available by any vendor container supporting J2EE specification. At the root context of the namespace, 'comp' is bounded for the components like ejb, servlet and jsp. And the 'env' is bounded to the 'comp' namespace and provided to component by the container. Usually the component deployment descriptor (e.g. web.xml for servlet/jsp component) defines the requires resource references in the descriptor and are available through java:comp/env namespace.

To understand the issue more clearly, we also need to understand the scope of the jndi lookup. There are two scopes in which any resources are looking up:

i. Component scope: This scope is resolved using the 'java:comp/env' text before the jndi resource name

ii. Server scope: This scope is resolved just by using the resource jndi name.

Now come to the point. Below is the xml tag of web application configuration file.
<resource-ref id="ResourceRef_121212121" >

<description>TestDataSource</description>

<res-ref-name>jdbc/TestDataSource</res-ref-name>

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

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

</resource-ref>

In the web.xml file of web application, the above tag creates the reference of resources. The child tag declares the name for the container bound resource so that the servlet in the web application can get the resource within the component scope (java:comp/env/). If the datasource were not declared in the web.xml then the datasource couldn't be found in the java:comp/env namespace. Though the resource bound in the application server can be found in the using the JNDI name directly, but it is not portable because the JNDI binding name could be changed anytime by the Application Server administrator.


In Tomcat, the datasource can be found in the component scope without refering the datasource in the web.xml, most probably, becuase the web component is the only component that can be deployed within it. But WebSphere Application Server (WAS) restricts access to resources those are not referenced in the web.xml. Whereas, in RAD-TestEnvironment (WAS 5.1 Test Environment), the datasource can be found with lookup using the JNDI name directly (the jndi name itself) but in WAS it doesn't work. That means inorder to access datasource from web component in WAS, the lookup name must be in component scope i.e. java:comp/env scope and the resource must be referenced in web.xml, the web component deployment descriptor

Resources
http://java.sun.com/products/jndi/tutorial/beyond/misc/policy.html
http://java.sun.com/products/jndi/tutorial/beyond/url/initctx.html
http://javahowto.blogspot.com/2006/07/when-and-where-not-to-use-javacompenv.html
http://forum.springframework.org/showthread.php?t=17712
http://www.theserverside.com/discussions/thread.tss?thread_id=27675
http://forum.java.sun.com/thread.jspa?threadID=568055&messageID=2807207
http://stackoverflow.com/questions/7167212/how-do-i-connect-to-a-websphere-datasource-with-a-given-jndi-name

No comments: