我正在开发一个使用Tomcat 7.0.64的遗留应用程序,我们希望将apache连接池配置为Tomcat中的一个资源。应用程序运行Spring4.x和Hibernate 4.x。在阅读Tomcat文档之后,当我尝试从Spring应用程序访问dbcp2连接池时,我将得到以下异常-
javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]为什么使用org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory,即使在server.xml中添加的“工厂”属性是org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory类型的。以下是这些配置的细节-
在server.xml中添加了以下内容-
<GlobalNamingResources>
<Resource name="jdbc/mytestDB"
auth="Container"
factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
type="javax.sql.DataSource"
username="test"
password="test"
driverClassName="oracle.jdbc.driver.OracleDriver"
description="test db"
url="jdbc:oracle:thin:@mytestDB:1521/mytestDB"
maxActive="15"
maxIdle="5"/>
</GlobalNamingResources>在web.xml中添加了以下内容-
<resource-ref>
<description>PVO Database</description>
<res-ref-name>jdbc/mytestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>添加了以下maven依赖项-
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.1</version>
</dependency>在测试类中添加了以下内容-
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
DataSource ds = envContext.lookup("jdbc/mytestDB"); // This line gives the above mentioned error.
Connection connection = ds != null ? ds.getConnection() : null;发布于 2020-10-05 00:52:42
修正了这个问题,方法是在tomcat -configure数据源中添加以下JVM属性,通过JNDI检索-
Djavax.sql.DataSource.Factory="org.apache.commons.dbcp2.BasicDataSourceFactory“
JAVA_OPTS="${JAVA_OPTS}”
然而,我们决定升级tomcat版本。
https://stackoverflow.com/questions/64130203
复制相似问题