Tomcat 5.5.9, correct directory for DB driver?

E

elektrophyte

I'm using Tomcat 5.5.9 on Windows 2000. I get the error below when I
try to visit a page of my app that accesses the DB.

I've tried putting the DB driver "mysql-connector-java-3.1.8-bin.jar"
in these locations:

[TOMCAT_HOME]\common\lib
[TOMCAT_HOME]\common\endorsed
[TOMCAT_HOME]\webapps\agenda\WEB-INF\lib
C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext

These are all directories that have been recommended in books, in
Tomcat docs, or in newsgroups as the place to put jar files like DB
drivers. I don't know if one, some, none, or all is the right
directory. In fact it seems to be one of the great mysteries of the
universe. Maybe there's an old guy sitting on top of a mountain in
Tibet who knows what the correct directory for database drivers is.

I notice that the Tomcat distribution also includes the
suggestively-named directory "shared\lib". I've never heard anyone say
that's the right one, but if you have a database driver that is clearly
a "library" and you want it to be available, or "shared" by all your
applications, then "shared\lib" might be a reasonable place to put it.
However I tried it and got the same error.

If anyone has any insight into the issues mentioned I'd be grateful to
hear it.

Thanks,

E

The error -->
================================================================
Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
driver of class '' for connect URL 'null'

[...]

Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)
 
J

James Yong

elektrophyte said:
I'm using Tomcat 5.5.9 on Windows 2000. I get the error below when I
try to visit a page of my app that accesses the DB.

I've tried putting the DB driver "mysql-connector-java-3.1.8-bin.jar"
in these locations:

[TOMCAT_HOME]\common\lib
[TOMCAT_HOME]\common\endorsed
[TOMCAT_HOME]\webapps\agenda\WEB-INF\lib
C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext

These are all directories that have been recommended in books, in
Tomcat docs, or in newsgroups as the place to put jar files like DB
drivers. I don't know if one, some, none, or all is the right
directory. In fact it seems to be one of the great mysteries of the
universe. Maybe there's an old guy sitting on top of a mountain in
Tibet who knows what the correct directory for database drivers is.

I notice that the Tomcat distribution also includes the
suggestively-named directory "shared\lib". I've never heard anyone say
that's the right one, but if you have a database driver that is clearly
a "library" and you want it to be available, or "shared" by all your
applications, then "shared\lib" might be a reasonable place to put it.
However I tried it and got the same error.

If anyone has any insight into the issues mentioned I'd be grateful to
hear it.

Thanks,

E

The error -->
================================================================
Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
driver of class '' for connect URL 'null'

[...]

Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)

Hi,

I suggest that you removed all copies of the driver and put only one in
common/lib.

I think the probable cause is the incorrect setting of the datasource in
server.xml of tomcat.

Regards,
james yong
 
J

Juha Laiho

(e-mail address removed) said:
an where on the classpath path should be ok. See
http://mindprod.com/jgloss/properties.html to find out the
java.classpath system property.

No, won't be reliable with Tomcat (could've worked somewhat with 3.x
series, perhaps 4.0, but not with 4.1 and onwards). Tomcat uses
rather extensively private classloaders, which do not honor system
classpath, but instead have classpaths explicitly configured. With
4.1.x the classpath configuration was very limited; with 5.x the
classpath configuration is vastly improved.

Initially this might not look like a good idea, but at least for
me this has proven to be extremely valuable tool to isolate the
Tomcat server environment from pretty much all influences of
command-line environment variables. I've seen cases of hard-to-trace
breakage when someone has "just made some small change to an
environment variable", and as a result the next Tomcat restart
(perhaps weeks later) fails -- or at least the application
misbehaves.
 
J

Juha Laiho

elektrophyte said:
I've tried putting the DB driver "mysql-connector-java-3.1.8-bin.jar"
in these locations:

[TOMCAT_HOME]\common\lib
[TOMCAT_HOME]\common\endorsed
[TOMCAT_HOME]\webapps\agenda\WEB-INF\lib
C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext

These are all directories that have been recommended in books, in
Tomcat docs, or in newsgroups as the place to put jar files like DB
drivers. I don't know if one, some, none, or all is the right
directory. In fact it seems to be one of the great mysteries of the
universe. Maybe there's an old guy sitting on top of a mountain in
Tibet who knows what the correct directory for database drivers is.

Read, and re-read, the Tomcat classloading documentation. Digesting
it will take some time - just accept that.

Then, if you create the database connections as you should (i.e.
by using the jdbc connection pooling support provided by Tomcat,
instead of creating the connections within your application),
place the library in common/lib. And make sure you don't have
compies of the library elsewhere -- there could be one hiding in
the Tomcat work directory for your webapp.
The error -->
================================================================
Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
driver of class '' for connect URL 'null'

[...]

Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)

.... but this somewhat looks like there isn't even an attempt to
load the mysql driver -- which could be caused by a bug in your
configuration.
 
E

elektrophyte

James said:
The error -->
================================================================
Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
driver of class '' for connect URL 'null'

[...]

Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)

Hi,

I suggest that you removed all copies of the driver and put only one in
common/lib.

I think the probable cause is the incorrect setting of the datasource in
server.xml of tomcat.

Regards,
james yong

Thanks for the reply. I did have an error in server.xml. I put the
driver in common/lib and it works. Onward!

E
 
E

elektrophyte

Then, if you create the database connections as you should (i.e.
by using the jdbc connection pooling support provided by Tomcat,
instead of creating the connections within your application),
place the library in common/lib. And make sure you don't have
compies of the library elsewhere -- there could be one hiding in
the Tomcat work directory for your webapp.

I edited server.xml to create a DB connection pool and make it a JNDI
resource. That now seems to work. I've also read various things about
where that connection pool resource configuration goes. Some say it
should be in conf\Catalina\localhost\WEBAPP_NAME.xml. I've also read
you should deploy it with the webapp in META-INF\context.xml.

However, I've found that, as you said, putting the configuration in
server.xml within the <Host name="localhost" ... > ... </Host> element
works. I put it right before the closing </Host> tag.

This is my server.xml context config that works on Tomcat 5.5.9 on
Windows 2000:

<Context path="/agenda" docBase="agenda">
<Resource name="jdbc/localagendads" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="USERNAME" password="PASSWORD"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/agenda" />
</Context>

Also have to add this to web.xml:

<resource-ref>
<description>Local DB connection</description>
<res-ref-name>jdbc/localagendads</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
 
J

Juha Laiho

elektrophyte said:
I edited server.xml to create a DB connection pool and make it a JNDI
resource. That now seems to work. I've also read various things about
where that connection pool resource configuration goes. Some say it
should be in conf\Catalina\localhost\WEBAPP_NAME.xml. I've also read
you should deploy it with the webapp in META-INF\context.xml.

However, I've found that, as you said, putting the configuration in
server.xml within the <Host name="localhost" ... > ... </Host> element
works. I put it right before the closing </Host> tag.

These different ways provide various levels of isolation - and
various levels of centralization.

For example, consider a situation where you deploy a final test
and production instances of an application to a single machine.
Both set the same database requirement in WEB-INF/web.xml, but
still you want to point these instances into separate databases.

This is where you need the "middle level" of webapp_name.xml
(or META-INF/context.xml within the webapp package). Even
with this model, you could encode the database information
directly in these "middle level" files, but I rather keep the
database configuration in single point (in server.xml), and
use these "middle level" files to map names as required by the
application to the names I use in the JNDI registry.

This way, I can deploy the exact same .war file to either
test or production instance, and the deployment environment
will handle the differences in databases and other external
requirements - and I still get to keep the actual JNDI
configuration in a single place in server.xml.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top