Tomcat 5.5.9, correct directory for DB driver?

Discussion in 'Java' started by elektrophyte, Sep 25, 2005.

  1. elektrophyte

    elektrophyte Guest

    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)
    elektrophyte, Sep 25, 2005
    #1
    1. Advertising

  2. elektrophyte

    James Yong Guest

    "elektrophyte" <> wrote in message
    news:...
    > 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
    James Yong, Sep 25, 2005
    #2
    1. Advertising

  3. elektrophyte

    Roedy Green Guest

    On 24 Sep 2005 19:23:41 -0700, "elektrophyte" <>
    wrote or quoted :

    >Tibet who knows what the correct directory for database drivers is.


    an where on the classpath path should be ok. See
    http://mindprod.com/jgloss/properties.html to find out the
    java.classpath system property.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 25, 2005
    #3
  4. elektrophyte

    Juha Laiho Guest

    lid said:
    >On 24 Sep 2005 19:23:41 -0700, "elektrophyte" <>
    >wrote or quoted :
    >
    >>Tibet who knows what the correct directory for database drivers is.

    >
    >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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Sep 25, 2005
    #4
  5. elektrophyte

    Juha Laiho Guest

    "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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Sep 25, 2005
    #5
  6. elektrophyte

    elektrophyte Guest

    James Yong wrote:
    > >
    > > 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
    elektrophyte, Sep 25, 2005
    #6
  7. elektrophyte

    elektrophyte Guest

    > 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>
    elektrophyte, Sep 26, 2005
    #7
  8. elektrophyte

    Juha Laiho Guest

    "elektrophyte" <> said:
    >> 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.


    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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Sep 26, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  2. Frank Marx
    Replies:
    1
    Views:
    3,720
    =?ISO-8859-1?Q?Mario_M=E9ndez_Villegas?=
    Aug 6, 2003
  3. Franz
    Replies:
    0
    Views:
    3,134
    Franz
    Apr 6, 2004
  4. Andrew Johnson
    Replies:
    2
    Views:
    4,702
    Sudsy
    Oct 20, 2004
  5. Replies:
    0
    Views:
    4,612
Loading...

Share This Page