Where should the JDBC driver JAR files be stored in a Tomcat deployment with a data source?

I have a Java application using Spring, Hibernate, Tomcat7 and MySql. I use Datasource for database operations. I donโ€™t quite understand what is the standard location for loading jar files ( Tomcat-jdbc.jar and Mysql-connector.jar )? It works if I keep BOTH banks in CATALINA_HOME/lib/ or webapps/myApp/WEB-INF/lib . But I was told to use only Tomcat-jdbc from CATALINA_HOME / lib / and Mysql-connector.jar from /WEB-INF/lib/ , which gives a ClassNotFound Exception for the Sql Driver . Can someone tell me what is the right place for these cans?

 <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> 
+6
source share
2 answers

It depends on who controls the data source.

If you manually create and manage a data source in your own webapp, for example new SomeDataSource() , etc., then the JDBC driver JAR file can be placed in webapp /WEB-INF/lib . But if the server application already has the same JDBC driver JAR file in its own /lib , then you can also just use it.

However, if you instruct application servers to independently manage the data source, and you simply use it in your webapp via @Resource , etc., then the JDBC driver JAR file must be placed in your own /lib application server, for the simplest reason, because the data source is ready to run application applications completely independent of any (former) deployed web applications. This data source, in turn, is available for all web applications. This technically just does not work if the JD file of the JDBC driver is in one of the web applications that have not yet been deployed.

+9
source

When it comes to locating the can, the rule of thumb is:

  • Use the jar at the server location.
  • If the jar is not available, include it in the lib application folder.
  • To find suitable Jars, the system will look in the following repositories in order:

The boot classes of your JVM

System class loader classes (described above)

/ WEB-INF / your web application classes / WEB-INF / lib / *. Jar

your web application $ CATALINA_HOME / common / classes

$ CATALINA_HOME / generic / approved / *. jar

$ CATALINA_HOME / general / i18n / *. Jar

$ CATALINA_HOME / common / lib / *. jar

$ CATALINA_BASE / general / classes

$ CATALINA_BASE / generic / Library / *. Jar

0
source

Source: https://habr.com/ru/post/953186/


All Articles