my dog learned polymorphism*
The moose likes Tomcat and the fly likes Unable make database connection using JNDI with tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Unable make database connection using JNDI with tomcat" Watch "Unable make database connection using JNDI with tomcat" New topic
Author

Unable make database connection using JNDI with tomcat

sanjay yadav yadav
Greenhorn

Joined: May 05, 2008
Posts: 9

Hi every one
I have configured tomcat with eclipse and made an struts application. i want to connect this to database using jndi. In which xml i have to write configuration.
my application workspace is out of webapp folder of tomacat6.

thanks in advance
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
You didn't told which version of tomcat you're using.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
sanjay yadav yadav
Greenhorn

Joined: May 05, 2008
Posts: 9
I gone through given link and configured my web.xml file like

<resource-ref>
<description>xyz</description>
<res-ref-name>jdbc/WareHouse</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

and there is context.xml file in project name as strutsapp/WebContent/META-INF/context.xml. and the entries are as:

<Context docBase="${CATALINA_HOME}" privileged="true" antiResourceLocking="false"
antiJarLocking="false">

<Resource name="jdbc/WareHouse"
auth="Container"
type="javax.sql.DataSource"
username="db2admin" password="password"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://localhost:50000/HONDAS2"
maxActive="10"
maxIdle="4"
/>

</Context>

and i wrote java code like

Context initContext = new InitialContext();
Context envCtx = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/WareHouse");
con = ds.getConnection();

remember application is out side the webapp folder of tomcat 6. i m running this project through eclipse

sanjay yadav yadav
Greenhorn

Joined: May 05, 2008
Posts: 9
Where should i have to put the context.xml file in this case.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

sanjay yadav yadav wrote:Where should i have to put the context.xml file in this case.

In META-INF folder and please quote your source code .
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

Welcome to the JavaRanch, sanjay, although we really could do without the red ink!

Eclipse doesn't factor into this. You should be OK with your META-INF/context.xml file, but since you aren't placing the WAR in the Tomcat webapps folder, you need to tell Tomcat where you did put it. CATALINA_HOME is a bad location, so I'm going to make up a location inside an Eclipse project. The exact location will vary, depending on how your system is set up, but this is typical:


Copy this file as "mywebapp.xml" into CATALINA_HOME/conf/Catalina/localhost/ and Tomcat will deploy it, including the configuration of your JDBC connection pool and its JNDI resource registration. The exact name you use for your external context doesn't really matter, but since there's only room in a directory for one file of a given name, I picked "mywebapp.xml" to make it distinct.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: Unable make database connection using JNDI with tomcat