Java JDBC 驱动程序 JAR 文件应该位于具有数据源的 Tomcat 部署中的什么位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18621662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:35:46  来源:igfitidea点击:

Where should the JDBC driver JAR files reside on a Tomcat deployment with a datasource?

javatomcatjdbcdatasource

提问by PeterGriffin

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.jarfrom /WEB-INF/lib/, which gives a ClassNotFoundException for Sql Driver. Can someone let me know which is the right location for these jars?

我有使用 Spring、Hibernate、Tomcat7 和 MySql 的 Java Web 应用程序。我使用 Datasource 进行数据库操作。我不太清楚加载 jar 文件 ( Tomcat-jdbc.jar& Mysql-connector.jar)的标准位置是什么?如果我将两个罐子都放在CATALINA_HOME/lib/或 中,它会起作用webapps/myApp/WEB-INF/lib。但是我被告知只能使用来自 CATALINA_HOME/lib/ 和mysql-connector.jar来自的 Tomcat-jdbc /WEB-INF/lib/,这ClassNotFoundSql Driver. 有人可以告诉我这些罐子的正确位置吗?

<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>

回答by Gyanendra Dwivedi

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:
  1. 在服务器库位置使用 jar。
  2. 如果 jar 不可用,请将其包含在应用程序的 lib 文件夹中。
  3. 要找到合适的 Jars,系统会按顺序在以下存储库中查找:

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

JVM 的引导类

系统类加载器类(如上所述)

您的 Web 应用程序的 /WEB-INF/classes /WEB-INF/lib/*.jar of

您的 Web 应用程序 $CATALINA_HOME/common/classes

$CATALINA_HOME/common/endorsed/*.jar

$CATALINA_HOME/common/i18n/*.jar

$CATALINA_HOME/common/lib/*.jar

$CATALINA_BASE/共享/类

$CATALINA_BASE/shared/lib/*.jar

回答by BalusC

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 /libalready, then you could also just make use of it.

如果您像这样在自己的 webapp 中手动构建和管理数据源new SomeDataSource(),那么 JDBC 驱动程序 JAR 文件可以放在 webapp 的/WEB-INF/lib. 但是,如果 appserver 碰巧自己/lib已经提供了完全相同的 JDBC 驱动程序 JAR 文件,那么您也可以使用它。

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.

但是,如果您指示 appserver 自行管理数据源,而您只是通过@Resource等在您的 web 应用程序中使用它,那么 JDBC 驱动程序 JAR 文件必须放置在 appserver 自己的 中/lib,原因非常简单因为数据源是在 appserver 启动时准备好的,完全独立于任何(将要)部署的 Web 应用程序。该数据源反过来可在所有 web 应用程序之间共享。如果 JDBC 驱动程序 JAR 文件位于尚未部署的 web 应用程序之一中,那么它在技术上是行不通的。