Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a <Resource> in Tomcat 7 so that I don't need to use "java:/comp/env" in the code?

I am new to setting up JNDI resources and setting up JNDI resources in Tomcat.

I inherited a servlet application. It runs on a test server via WebLogic. The servlet application accesses its database resource in the following way:

ctx  = new InitialContext();
ds   = (javax.sql.DataSource)ctx.lookup("myDataBaseName"); 
conn = ds.getConnection();

When I tried that in a test JSP it doesn't work. I get

javax.naming.NameNotFoundException: Name myDataBaseName is not bound in this Context

However I was able to make the test JSP work if I altered the test JSP code thus:

ctx  = new InitialContext();
Context envContext = (Context)ctx.lookup("java:/comp/env");
ds   = (DataSource)envContext.lookup("myDataBaseName");
conn = ds.getConnection();

I have this entry in TOMCAT_HOME/conf/context.html ( I'm just using it as a dev environment on my box )

<Resource name="myDataBaseName"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.OracleDriver"
        url="jdbc:oracle:thin:@blahblahblah:1521:database3"
        username="joeuser"
        password="password"
        maxActive="20"
        maxIdle="30"
        maxWait="-1"/>

</Context>

And I have this in my TOMCAT_HOME/conf/web.xml:

<resource-ref>
    <res-ref-name>myDataBaseName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

I read about 10 stackoverflow pages on related problems, but I am not familiar enough with setting up JNDI resources to abstract those solutions to my problem.

How can I change the way I have Tomcat 7 set up to allow the existing servlet application to access its database as "myDatabaseName"? I don't have the option of altering the code in the servlet application or altering the way WebLogic is set up on the test server.

I need to alter the way my copy of Tomcat 7 is set up on my computer ( for a dev environment ) to allow the servlet application to access its database in the style of the first chunk of quoted code at the top of this post.

like image 941
Steve Avatar asked Jul 11 '11 17:07

Steve


People also ask

How do I use data sources with Tomcat?

Tomcat DataSource JNDI Configuration Example - server.Add below code in the tomcat server. xml file. The code should be added in the GlobalNamingResources element. Also make sure that database driver is present in the tomcat lib directory, so in this case mysql jdbc jar have to be present in the tomcat lib.

Does Tomcat support JNDI?

Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.

What is the use of context XML in Tomcat?

In Tomcat, the Context Container represents a single web application running within a given instance of Tomcat. A web site is made up of one or more Contexts. For each explicitly configured web application, there should be one context element either in server. xml or in a separate context XML fragment file.


2 Answers

I see 2 possibilities here:

Implement a InitialContextFactory and make it available to tomcat (via system property), so that new InitialContext() gives you a context the lookup would work on.

or

Bind the data source to another place in the JNDI, which actually works although it is neither recommended nor documented (tomcat FM states the tree being read-only which only seems to apply to the subtree java:/comp/env):

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/myDataBaseName");
ctx.bind("myDataBaseName", ds);
like image 123
Enrice Avatar answered Sep 30 '22 06:09

Enrice


With the configuration that you provided you should be able to do the following:

    ctx  = new InitialContext();
ds   = (DataSource)ctx.lookup("java:/comp/env/myDataBaseName");
conn = ds.getConnection();
like image 44
Olaf Avatar answered Sep 30 '22 05:09

Olaf