【发布时间】:2015-04-02 10:44:33
【问题描述】:
我对 Tomcat 8 中的上下文配置有疑问。我将项目从 Tomcat 7 迁移到 8 并遇到异常问题:如果配置中没有任何变化,我发现了一个错误:
"2015-02-03 12:05:48,310 FIRST_ADMIN ERROR web.context.ContextLoader:331
-> Context initialization failed org.springframework.jmx.export.UnableToRegisterMBeanException:
Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource@434990dd]
with key 'dataSource'; nested exception is
javax.management.InstanceAlreadyExistsException:
Catalina:type=DataSource,host=localhost,context=/first-
admin,class=javax.sql.DataSource,name="jdbc/datasource/first"
部分上下文:
<Resource name="jdbc/datasource/first"
auth="Container"
type="javax.sql.DataSource"
poolPreparedStatements="true"
initialSize="25"
maxActive="100"
maxIdle="100"
minIdle="25"
username="us"
password="pa"
driverClassName="com.mysql.jdbc.Driver"
validationQuery="select 1"
testOnBorrow="true"
url="jdbc:mysql://localhost:3306/firstproject?useUnicode=true&characterEncoding=UTF-8&profileSQL=false&autoSlowLog=false&slowQueryThresholdMillis=100&autoReconnect=true"/>
所以,它在 tomcat 7 中运行没有任何问题。在 Tomcat 8 中,我可以通过 2 种方式解决这个问题:
- 通过添加到资源:
singleton = "false"; - 通过添加到资源:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
如果我清楚地了解 tomcat 为我的应用程序和 jmx 创建数据源,但在 Tomcat 7 中它是单个对象,在 Tomcat 8 中它必须不同。所以我的问题是为什么会发生这种情况?我在文档中找不到有关此更改的任何信息。而且我很感兴趣什么更好:创建单个数据源(我认为是这样)或通过工厂创建多个。
【问题讨论】:
-
tomcat 7 (from) 和 tomcat 8 (to) 的版本是什么?您还可以添加您的 web.xml 吗?您的应用程序是否在其自己的 context.xml 文件 (META-INF) 中使用上述声明 - 如果是,您可以发布这个吗?顺便说一句,使用 JDBC 资源时,单例必须为“true”
标签: java tomcat configuration context.xml