0

How does a look-up like :

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

proceed ?

I mean to say how is the name MyDataSource searched and in the end what is returned ?

There are two entries added to connect to the database. One in the WEB-INF/web.xml which is :

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

and the other added in the META-INF/context.xml which is :

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

How does these 2 entries help in the look up ?

What is looked first : web.xml or context.xml ?

Please explain the whole process in the look up.

saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

1

Resources are located in this order of preference: web.xml (via <resource-ref> elements, context.xml, server.xml (via <GlobalNamingResources>). Note that resource defined in your <Context> do not actually need to have corresponding <resource-ref> elements in your web.xml. See the Tomcat documentation regarding JNDI resources: http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • But if I remove the `resource-ref` tag from the web.xml I get an exception which says `Name not found Exception` – saplingPro Jul 22 '12 at 17:20
  • can you please explain _"Note that resource defined in your do not actually need to have corresponding elements in your web.xml"_ – saplingPro Jul 22 '12 at 17:37
  • Read the Tomcat documentation reference I posted above: it specifically mentions that `` if not necessary when you specify a `` in your `` in `META-INT/context.xml`. Post your whole `META-INF/context.xml` if you want me to comment on it. – Christopher Schultz Jul 22 '12 at 21:04
  • Note there is a difference between Tomcat's `conf/context.xml` (the default for all webapps) and your webapp's specific `META-INF/context.xml` (which is a far better choice for customization. If you don't want to put this kind of configuration into your webapp, then you should be using `` in `server.xml` instead (where you *do* need a `` in your `WEB-INF/web.xml`). – Christopher Schultz Jul 22 '12 at 21:27
  • If that is your `META-INF/context.xml`, you've deployed your webapp correctly (make sure there isn't an old, cached copy of your `context.xml` in `CATALINA_BASE/conf/Catalina/localhost/poll.xml`), you have your JDBC driver's JAR file in the right place, and you have no other references to Derby anywhere, then everything should work for you. (Note that the use of the `path` attribute in `META-INF/context.xml` `` element is illegal: remove it). – Christopher Schultz Jul 23 '12 at 21:32
0

The following are the steps to do a lookup:
STEP 1:

 Context context = new InitialContext(): 

The initial context is a reference to the JNDI lookup service. It is like the entry into the JNDI virtual directory tree.

STEP 2:

Object o = context.lookup("mejb"): 

Here in the lookup we need to give the name of the bean whatever that is deployed in the server, to get a reference to the home interface of that bean.We then get the object of type java.lang.Object we need to cast this object to the Home interface of whichever bean we did a lookup on.

STEP 3:

Home home = (Home) PortableRemoteObject.narrow(o,Home.class):

We actually need to cast the object to the type that we think it is type of. However, since this is RMI over IIOP it seems we need to use the PortableRemoteObject.narrow method this it seems filters the object type to the actual object type and checks for errors.

NiranjanBhat
  • 1,812
  • 13
  • 17
  • Can you explain your answer with context to `web.xml` and `context.xml`. B'coz these are the two files that help in the look-up. – saplingPro Jul 19 '12 at 10:44