Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in connecting java web application with database using context file in Tomcat 7

 
leo jacob
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on a web application. I have placed it in webapps folder and the context file (application.xml) at Tomcat 7.0\conf\Catalina\localhost. I have configured the database connection in context file (application.xml). My Context file is as follows

<Context path="/application" docBase="application" debug="0" privileged="true">
<ResourceParams name="jdbc/APPLICATIONDataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbcracle:thin:@120.120.40.90:1521:SERF</value>
</parameter>
<parameter>
<name>username</name>
<value>User1</value>
</parameter>
<parameter>
<name>password</name>
<value>Password1</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="APPLICATION." suffix=".log" timestamp="true"/>
</Context>


I am using jndi for database connectivity

package com.company.util;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;


public class DBUtil
{

public static Connection getConnection() throws NamingException {
Connection objConnection=null;
try{
Context objInitCtx = new InitialContext();
Context objEnvCtx = (Context) objInitCtx.lookup("java:comp/env");
DataSource objDS =
(DataSource) objEnvCtx.lookup((String) objEnvCtx.lookup("DataSourceName"));
objConnection = objDS.getConnection();
//System.out.println("Got Connection");

}catch(SQLException e){
System.out.println("Database connection was refused");

}
return objConnection;

}
}


The application is working fine in tomcat 4 but it is not getting database reference in Tomcat 7.


Any idea to resolve this issue will be highly appreciated.

Thanks



 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make your XML and java code easier to read if you use the "Code" button on our message editor to wrap the examples with code tags.

I'm not too sure about your "<Logging>" element in the Context. Logging has traditionally been setup inside the webapp.

Tomcat7 changes the Connection pool infrastructure, so it's a good idea to RTFM. However, your examples seem to be defining the datasource under the JDNI name "jdbc/APPLICATIONDataSource" but looking for "DataSourceName".

That wouldn't work even in Tomcat 4, though.
 
leo jacob
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Tim for your reply.

Please let me know the meaning of RTFM.

I have configured this mapping in web.xml file and it is working fine in tomcat 4. I am facing problem in Tomcat 7.

Code in web.xml is like this :




my application is not able to read the context file thats why I am not able to connect to the database as I am getting the message : "Database connection was refused". That I have printed from my java class mentioned in my first mail.

I feel it is the issue of the location and naming convention of context file .

Thanks






 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RTFM is geek slang for Read The Freakin' Manual. Or something like that.

As I said, you sample code and Context don't match and therefore I wouldn't expect them to work under any version of Tomcat. Hopefully the actual files do match.

Your application does not "read the context file". The Context file is technically-known as the "server-specific deployment descriptor". It, along with the "server-independent deployment descriptor" (WEB-INF/web.xml) are read and processed by the web application server (Tomcat), not by the web application itself.

Instead of printing out your own error message when an exception is received, use "e.printStackTrace()" to get the real reason behind the problem.
 
leo jacob
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Let me put full details of my problem:

I am not able to connect with my database by using jndi in tomcat 7 and I am not able to get where am I wrong in my configuration. The applications with these details are working fine in tomcat 4 but it is giving problem in tomcat 7 for database connection. I have placed the web application at webapps folder and the context file at -- application/WebContent/META-INF/context.xml

Details:
Java class:



web.xml




Context.xml




Error Details :



Any help is highly appreciated.

Thanks in Advance



 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I hate that message! I'm getting it myself right now. It's murder to figure out what's wrong. In my case, doubly so, since there seems to be a phantom connection unrelated to the one I'm actually using.

Your resource has parameters but no definition. So I think something very important is missing. I also notice that although you are supplying an Oracle JDBC URL, the stack track says it's trying to work with the jdbc-odbc bridge driver, not the Oracle driver.

Go to http://tomcat.apache.org and read the manual section on JDBC data sources. I believe that there's an actual Oracle example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic