美文网首页PHP经验分享MySQL部署运维
Tomcat常见问题之无法加载资源工厂类dbcp

Tomcat常见问题之无法加载资源工厂类dbcp

作者: Lemonlzy | 来源:发表于2019-12-24 19:05 被阅读0次

问题背景

前两天公司新开发了一个项目,使用的Spring Boot2.0,而原有的项目部署在Tomcat 7之上,所以需要升级一下Tomcat的版本,在安装新的Tomcat 8,并将原有的配置复制到Tomcat 8之后,启动服务会发现一直报一个错误:

[ERROR][org.springframework.web.context.ContextLoader]  Context initialization failed   
org.springframework.beans.factory.BeanCreationException: Error creating bean with name   
'xxx' defined in ServletContext resource [/WEB-INF/applicationContext-beans.xml]  
Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested   
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with   
name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext-dataSource.xml 
: Invocation of init method failed; nested exception is javax.naming.NamingException: 无法加载资源工厂类 [Root exception is   
java.lang.ClassNotFoundException:org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]

解决方法

看到这个报错ClassNotFoundException:org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory,第一反应自然是包没有加载进来,或者说没有读取到这个dbcp这个包,去Tomcat 8安装目录下的lib目录下查看,实际上tomcat-dbcp.jar这个包是存在的,那为什么在Tomcat 7里面可以加载这个jar包,在Tomcat 8就不行了呢?

此为Tomcat 8的lib目录:


Tomcat8lib.png

我们来看一下Tomcat 7关于javax.sql.DataSource的指定方式为org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory。
Apache Tomcat 7配置参考

ApacheTomcat7配置参考.png

在原来Tomcat 7的conf目录下context.xml文件中是这样进行配置的:

 <Resource 
           name="jdbc/xxx"
           type="javax.sql.DataSource"
           factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
           auth="Container" 
           driverClassName="com.mysql.jdbc.Driver" 
           url="jdbc:mysql://xxx/komdb?characterEncoding=utf-8&amp;useUnicode=true&amp;autoReconnect=true"
           username="xxx"
           password="xxx
           maxActive="100" 
           maxIdle="100" 
           maxWait="6000" 
           initialSize="20"
           minIdle="20"
           validationQuery="SELECT 1"
           testWhileIdle="true"
           testOnBorrow="false"
           timeBetweenEvictionRunsMillis="60000"
           minEvictableIdleTimeMillis="1800000"
           numTestsPerEvictionRun="10"
           removeAbandoned="true"
           removeAbandonedTimeout="180"
  />

接着来看一下Tomcat 8关于javax.sql.DataSource的指定方式为org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory。
Apache Tomcat 8配置参考

ApacheTomcat8配置参考.png

到这里终于破案了,在Tomcat 7和Tomcat 8之间更改了默认工厂的名称。一些属性名称也发生了变化。所以在升级Tomcat的时候有很多细节地方需要注意,将这里改为Tomcat 8的指定方式后,重启Tomcat,该jar包成功加载。

同时需注意Tomcat 7中配置context.xml时指定的最大线程,最大等待时间等参数名,在Tomcat 8中均有相应的调整,例如maxActive需改写为maxTotal,maxWait需改写为maxWaitMillis,当然,还有其他属性,在这里不做赘述,在Tomcat启动日志中会以warning的形式将过时的参数抛出,届时按warning提示修改即可。

更改后的Tomcat 8的conf目录下context.xml的配置:

    <Resource 
           name="jdbc/xxx"
           type="javax.sql.DataSource"
           factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
           auth="Container" 
           driverClassName="com.mysql.jdbc.Driver" 
           url="jdbc:mysql://xxx/komdb?characterEncoding=utf-8&amp;useUnicode=true&amp;autoReconnect=true"
           username="xxx"
           password="xxx"
           maxTotal="100" 
           maxIdle="100" 
           maxWaitMillis="6000" 
           initialSize="20"
           minIdle="20"
           validationQuery="SELECT 1"
           testWhileIdle="true"
           testOnBorrow="false"
           timeBetweenEvictionRunsMillis="60000"
           minEvictableIdleTimeMillis="1800000"
           numTestsPerEvictionRun="10"
           removeAbandonedOnBorrow="true"
           removeAbandonedTimeout="180"
  />

至于网上描述的其他方式,例如引入commons-dbcp2.jar包、commons-pool2.jar包等,笔者通过更改相应的context.xml中的factory属性为org.apache.commons.dbcp.BasicDataSourceFactory仍未成功,可能也是指定的引用路径不对,在此不做深究。

欢迎访问我的个人博客:Lemon - 万事顺遂

相关文章

网友评论

    本文标题:Tomcat常见问题之无法加载资源工厂类dbcp

    本文链接:https://www.haomeiwen.com/subject/sgbfoctx.html