You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Joe Reger, Jr." <jo...@joereger.com> on 2005/05/31 22:10:31 UTC
Embedded Tomcat JDBC Issue
Any tips on how to get a JDBC connection going using the embedded version of
Tomcat? Where does the MySql driver go? How is the JNDI resource
configured? I've tried deploying a .war file with a context.xml, but have
had some problems (below). Thanks, Joe
-----Original Message-----
From: Joe Reger, Jr. [mailto:joe@joereger.com]
Sent: Sunday, May 29, 2005 3:56 PM
To: tomcat-user@jakarta.apache.org
Subject: Embedded Tomcat JNDI/JDBC Configuration Questions
Hi!
I've successfully created a project that embeds Tomcat. Excellent! I can
see the sample page and the manager app, reporting Tomcat 5.5.9. Now I'm
working on deploying a .war file programatically:
public void registerWAR(String contextPath, String absolutePath) throws
Exception {
Context context = this.embedded.createContext(contextPath,
absolutePath);
context.setReloadable(false);
this.host.addChild(context);
}
I pass in the path "" and the location of the .war file. When I run the
application the .war file is found and exploded to the /ROOT directory
properly. Inside of the .war file is a context.xml file. The "jdbc/db"
resource is defined in context.xml, inside of the .war file with the
following:
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/db">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>foo</value>
</parameter>
<parameter>
<name>password</name>
<value>bar</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/reger?autoReconnect=true</value>
</parameter>
</ResourceParams>
When I make a call to my application the code inside the .war file attempts
to connect to the database like this:
private static final String jndiPrePend = "java:comp/env/"; private static
final String jndiDB = "jdbc/db"; public static Connection getConnection(){
Connection conn=null;
try{
Context ctx = new InitialContext();
if(ctx != null){
DataSource ds = (DataSource)ctx.lookup(jndiPrePend + jndiDB);
if(ds != null){
conn = ds.getConnection();
return conn;
}
}
} catch (Exception e){
e.printStackTrace();
util.errorsave(e);
}
return null;
}
But I get the following error in the console window, repeatedly, each time
my app tries to connect to the db:
javax.naming.NamingException: Cannot create resource instance at
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.
java:132)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at org.apache.naming.NamingContext.lookup(NamingContext.java:792)
at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
at org.apache.naming.NamingContext.lookup(NamingContext.java:139)
at org.apache.naming.NamingContext.lookup(NamingContext.java:780)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at reger.db.RunSQL(db.java:64)
at reger.db.RunSQL(db.java:124)
at reger.scheduler.MasterThread.setupThread(MasterThread.java:377)
at reger.scheduler.MasterThread.run(MasterThread.java:48)
It looks like the jndi/jdbc resource isn't configured correctly and/or isn't
available to the code from the .war file running inside of the embedded
tomcat.
Questions:
1) Is my jndiPrePend variable correct? It works for a standard deployment
on a non-embedded tomcat.
2) Is there anything special I need to do to get a jndi/jdbc resource
configured under the embedded tomcat?
3) Does the error message I'm seeing point to anything that I need to
change?
Thanks,
Joe
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org