Discuss tomcat-5 jndi issues in the Java Help forum on Dev Shed. tomcat-5 jndi issues Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
tomcat-5 jndi issues
I've seen this issue described on various forums and am in need of
an answer or solution. This is in reference to obtaining DataSource
for tomcat DBCP. I am running Tomcat 5.
Code snippet I've seen shows DataSource being obtained but error when
creating the connection from the DataSource.
I am only showing relevant pieces of code of the servlet stub I am running.
My comments and location of error are included:
// Get initial context.
// Result: OK
//
initCtx = new InitialContext();
envCtx = (Context)initCtx.lookup( "java:comp/env" );
// Get data source
// Result:OK
//
ds = (DataSource)envCtx.lookup( "jdbc/TestDBCP" );
The exception thrown is:
Exception: [org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null', cause: No suitable driver]
My relevant web.xml entries:
---------------------------
<web-app>
<display-name>Welcome to Arm Database</display-name>
<description>
Welcome to Arm Database
</description>
This is the way it is currently set up. Keep in mind that I could not even obtain a datasource
until I found some posts advising setting autoDeploy="false". When I did that I could get the
datasource. However, it does not work unless I have the resource-ref entries in web.xml.
Further, if I just attempt to get the Environment entry from server.xml, failure results:
<!-- Test entry for demonstration purposes -->
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
FAILS: Integer simpleValue = (Integer)envCtx.lookup( "simpleValue" );
Exception: envCtx.lookup(simpleValue): [javax.naming.NameNotFoundException: Name simpleValue is not bound in this Context]
This leads me to ask if this is more than just a database connection pool issue. Bottom line,
I cannot obtain elements from server.xml no matter where I put them in the server.xml file.
If it is just a matter of digging through documentation for solutions, please point to the doc. Complete examples would
work wonders, not just snippets.
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
tomcat-5 jndi solution
I spent some time working through just this issue and now have a tomcat-5 connection pool working against mysql.
Here are the applicable software versions:
tomcat: v5.0.27
mysql: Ver 12.22 Distrib 4.0.20
0. Shutdown tomcat.
1. If not already present, copy the following (recent) versions of the mysql connector and commons-* jars
to $TOMCAT_HOME/common/lib. Delete any old ones.
2. Ignore the advice in the tomcat-5 docs on jakarta.apache.org telling you to modify server.xml. Tomcat 5 uses a minimalist server.xml and now does application-specific configuration in separate files. If you have mucked up your server.xml either by hand editing or using the tomcat manager app, start fresh. Shutdown tomcat and copy a new minimalist server.xml in place.
If your webapp is called foo, create a file $TOMCAT_HOME/conf/Catalina/localhost/foo.xml. Save a copy of foo.xml in another location - the working copy is deleted if you undeploy foo using the tomcat manager.
If your mysql database is called sampdb, the host serving mysql is mysqlhost.mydomain.com, and your mysql user/password for schema sampdb is sampadm/xxx add the following to foo.xml:
public void doGet (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String query = "SELECT * FROM member";
try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
if(envCtx == null )
throw new Exception("Boom - No Environment Context");
// the following matches the resource name defined in foo.xml
DataSource ds =
(DataSource) envCtx.lookup("jdbc/sampdb");
if (ds != null) {
Connection conn = ds.getConnection();
int numCols = rs.getMetaData().getColumnCount ();
while ( rs.next() ) {
out.println("<tr>");
for (int i=1; i<=numCols; i++) {
out.print("<td>" + rs.getString(i) + "</td>" );
} // end for
out.println("</tr>");
} // end while
} // end try
catch ( IOException ex) {
ex.printStackTrace();
} // end catch
} // end returnHTML
} // end simpleDBCP
5. Deploy foo.war and start tomcat. Be on the lookout for complaints from tomcat (maybe unable to login using the provided user/password, etc.).
** Additional info (unrelated to DBCP) **
If you wish to create a resource link, say to a globally defined mail resource, you can add something like the following after the closing ResourceParams tag:
The above expects that a global resource mail/Session has been defined in server.xml as follows (note that this can be done via the tomcat manager application as well):
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks Rick
I've been hitting my head against a brick wall for a few hours here today with this null url problem when I came across your post. I'm surprised that this isn't better documented by tomcat! Anyway, when I moved my context out of the server.xml and into it's own config xml file the dbcp datasource worked like a charm.
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
my web app is not called foo!
I'm trying to get DBCP working with MySQL (4.1) and Tomcat (5.0.28). I can follow these instructions and get everything working... but, what if my web app is not called foo?
In other words, I don't want to type http://localhost/foo/test.jsp - I want to type http://localhost/test.jsp directly. When I do that I get an error message, "No suitable driver found" I thought I'd reconfigured everything everything correctly for the default (ROOT) app, but I must be missing something!
Does anyone know how to apply these instructions for the case of the default (ROOT) Tomcat web app? Or does anyone know of any other instructions to get it working for the default web app?