You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Iñigo Mediavilla Saiz <im...@yahoo.es> on 2008/11/23 12:08:52 UTC

Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

When
I try to access to a Database through the connector it fails if I leave
it inside WEB-INF\lib, but everything works if I copy the jar in the
lib folder of the apache distribution. Is there any other way to let a
web app access to a lib without having to copy the jar in apache\lib?
I'm using apache 6.0.18 and the exception I receive when I try to 
access the db is:


org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
    at discoteca.authentication.AuthenticationServlet.obtenerConexionBD(AuthenticationServlet.java:105)
    at discoteca.authentication.AuthenticationServlet.doPost(AuthenticationServlet.java:61)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
    ... 17 more


      

Re: Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

Posted by Michael Ludwig <mi...@gmx.de>.
Iñigo Mediavilla Saiz schrieb am 23.11.2008 um 11:08:52 (+0000):
> When I try to access to a Database through the connector it fails if I
> leave it inside WEB-INF\lib, but everything works if I copy the jar in
> the lib folder of the apache distribution.

Putting the driver in WEB-INF/lib only allows connections to be
established via java.sql.DriverManager, doing Class.forName(DRIVER).

> I'm using apache 6.0.18 and the exception I receive when I try to 
> access the db is:
> 
> org.apache.tomcat.dbcp.dbcp.SQLNestedException:
> Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver' at
> org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource( ...

You're trying to use the connection pool via javax.sql.DataSource and a
configured <Resource>. This fails, because the container doesn't have
the driver, only the web app has it.

> Is there any other way to let a web app access to a lib without having
> to copy the jar in apache\lib?

Yes: In Tomcat 6, to make the driver available for use in a connection
pool <Resource> without copying it to Tomcat\lib, edit the configuration
file Tomcat\conf\catalina.properties and add the driver jar to the
"common.loader" property.

common.loader=${catalina.home}/lib,[...],file:///C:/jlib/mysqldriver.jar

Note the syntax particularities:

* comma-separated
* file URIs (three slashes mean there is no host part)
* Windows drive letter as first path component

This has the same effect as copying the JAR into Tomcat/lib. In fact,
you can see that Tomcat/lib is part of "common.loader".

The Tomcat documentation says of the "Common" classloader: "Normally,
application classes should NOT be placed here." But for a JNDI
datasource, the "Common" classloader is needed.

If instead you wanted to make the driver available to all web apps
without using JNDI and the connection pool (which generally is not a
recommended technique for database drivers), you'd add it to the
"shared.loader" property in the same file. That way, you wouldn't have
to copy it into all WEB-INF/lib directories. The server won't see what
you add here.

For completeness, there is also the "server.loader", where you may add
classes only the server is supposed to see.

* server.loader -> for the container, Tomcat
* shared.loader -> for all web apps
* common.loader -> for both the container and all web apps

Tomcat's classloading mechanism has changed slightly between 5 and 6.
In Tomcat 5, there were Tomcat/{server,shared,common}/{lib,classes}.
In Tomcat 6, there is only a unified Tomcat/lib directory.

http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
http://tomcat.apache.org/migration.html
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
http://weblogs.java.net/blog/arungupta/archive/2007/07/metro_on_tomcat.html

Finer granularity of class loading seems only to be possible via the
file "conf/catalina.properties". The Tomcat 6 documentation doesn't
reveal this yet, though.

Michael Ludwig

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

Posted by Mark Thomas <ma...@apache.org>.
Michael Ludwig wrote:
> Mark Thomas schrieb am 23.11.2008 um 11:25:43 (+0000):
>> Iñigo Mediavilla Saiz wrote:
>>> [Tomcat 6] When I try to access to a Database through the connector
>>> it fails if I leave it inside WEB-INF\lib, but everything works if I
>>> copy the jar in the lib folder of the apache distribution. Is there
>>> any other way to let a web app access to a lib without having to
>>> copy the jar in apache\lib?
>> You can't use Tomcat's built in (DBCP based) connection pooling
>> without placing the JDBC driver jar in CATALINA_HOME/lib.
> 
> You can, via conf/catalina.properties, as explained in my other post on
> this thread. But it is not documented very well.

True. To be more accurate, you can't do what the OP wanted to do unless the JDBC
driver is visible to the common class loader. It has to be visible to the Tomcat
 internals and the webapp.

>> To do what you want, you need to include your own copy of DBCP with
>> your web app and do all the pool configuration in your web app.
> 
> I wonder if there is a scenario in which this would make sense?

- You want to use a different version of DBCP that is provided by Tomcat.
- You want a greater level of independence from the container.

> And wouldn't more memory be required for 5 web apps all loading the same
> classes redundantly instead of relying on the container to do it once
> instead?

Yes.

Mark



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

Posted by Michael Ludwig <mi...@gmx.de>.
Mark Thomas schrieb am 23.11.2008 um 11:25:43 (+0000):
> Iñigo Mediavilla Saiz wrote:
> > [Tomcat 6] When I try to access to a Database through the connector
> > it fails if I leave it inside WEB-INF\lib, but everything works if I
> > copy the jar in the lib folder of the apache distribution. Is there
> > any other way to let a web app access to a lib without having to
> > copy the jar in apache\lib?
> 
> You can't use Tomcat's built in (DBCP based) connection pooling
> without placing the JDBC driver jar in CATALINA_HOME/lib.

You can, via conf/catalina.properties, as explained in my other post on
this thread. But it is not documented very well.

> To do what you want, you need to include your own copy of DBCP with
> your web app and do all the pool configuration in your web app.

I wonder if there is a scenario in which this would make sense?

And wouldn't more memory be required for 5 web apps all loading the same
classes redundantly instead of relying on the container to do it once
instead?

Michael Ludwig

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

Posted by Mark Thomas <ma...@apache.org>.
Iñigo Mediavilla Saiz wrote:
> When
> I try to access to a Database through the connector it fails if I leave
> it inside WEB-INF\lib, but everything works if I copy the jar in the
> lib folder of the apache distribution. Is there any other way to let a
> web app access to a lib without having to copy the jar in apache\lib?

You can't use Tomcat's built in (DBCP based) connection pooling without placing
the JDBC driver jar in CATALINA_HOME/lib.

To do what you want, you need to include your own copy of DBCP with your web app
and do all the pool configuration in your web app.

Mark



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Deploying mysql-connector-java-5.1.7-bin.jar in /WEB-INF/lib

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Iñigo,

Iñigo Mediavilla Saiz wrote:
> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC
> driver class 'org.gjt.mm.mysql.Driver'

Others have answered your original question.

Note that Connector/J has used the com.mysql.jdbc.Driver class for many,
many versions, now. You should switch from the org.gjt package (they are
aliases of each other, so it doesn't really matter, but one day they
will probably drop the alias).

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkk0NoUACgkQ9CaO5/Lv0PD5ZACePq1u0VPbZ/+gRd6ysCq8N2pC
wyEAoL7wRHjyOlCPqjghdlxFDgpCKQbx
=2FQM
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org