6

I have java web application using Spring, Hibernate, Tomcat7 & MySql. I use Datasource for database operations. I am not very clear about what is the standard location to load the jar files (Tomcat-jdbc.jar & Mysql-connector.jar) from? It works if I keep BOTH the jars either in CATALINA_HOME/lib/ or webapps/myApp/WEB-INF/lib. But I was told to use only the Tomcat-jdbc from CATALINA_HOME/lib/ and mysql-connector.jar from /WEB-INF/lib/, which gives a ClassNotFound Exception for Sql Driver. Can someone let me know which is the right location for these jars?

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="initialSize" value="${db.initialSize}" />
        <property name="minIdle" value="${db.minIdle}" />
        <property name="maxActive" value="${db.maxActive}" />
        <property name="maxIdle" value="${db.maxIdle}" />
        <property name="testWhileIdle" value="${db.testWhileIdle}" />
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />
        <property name="validationQuery" value="${db.validationQuery}" />
    </bean>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
PeterGriffin
  • 83
  • 1
  • 1
  • 5

2 Answers2

9

That depends on who's managing the datasource.

If you're manually constructing and managing the datasource in your own webapp like so new SomeDataSource(), etc, then the JDBC driver JAR file can be placed in webapp's /WEB-INF/lib. But if appserver happen to provide the very same JDBC driver JAR file in its own /lib already, then you could also just make use of it.

However, if you're instructing appserver to manage the datasource all by itself and you're merely making use of it in your webapp via @Resource, etc, then the JDBC driver JAR file has to be placed in appserver's own /lib, for the very simple reason because the data source is prepared on appserver's startup completely independently from any (to be) deployed web applications. This data source is in turn shareable among all webapps. It would technically just not work if the JDBC driver JAR file is in one of the yet-to-be-deployed webapps.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

When it comes to location of jars to be decided, the thumb rule is that:

  1. Use a jar in the server lib location.
  2. If the jar is not available, include the same in the application's lib folder.
  3. To find a suitable Jars, system looks in the following repositories, in the order:

Bootstrap classes of your JVM

System class loader classes (described above)

/WEB-INF/classes of your web application /WEB-INF/lib/*.jar of

your web application $CATALINA_HOME/common/classes

$CATALINA_HOME/common/endorsed/*.jar

$CATALINA_HOME/common/i18n/*.jar

$CATALINA_HOME/common/lib/*.jar

$CATALINA_BASE/shared/classes

$CATALINA_BASE/shared/lib/*.jar

Community
  • 1
  • 1
Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
  • The list in this answer is out-dated as of later versions of Tomcat. For Version 8.0, see [Class Loader HOW-TO](https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html) and [this Answer](http://stackoverflow.com/a/265552/642706) to a similar question. – Basil Bourque Apr 19 '16 at 03:43