问题描述
我对Tomcat 8中的上下文配置有疑问。我将项目从Tomcat 7迁移到8,并且遇到了不寻常的问题:如果配置没有任何变化,我捕获了一个错误:
I have a question about context config in Tomcat 8. I migrating project from Tomcat 7 to 8 and have unusual problem: if nothing change in config I caught an error:
"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中,我可以通过两种方式解决此问题:
So, it's works in tomcat 7 without any problem. In Tomcat 8 I can solve this problem in 2 ways:
- 通过添加到资源中:
singleton = false ;
- 通过添加到资源中:
factory = org.apache.tomcat.jdbc.pool.DataSourceFactory
- By adding to resource:
singleton = "false";
- By adding to resource:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
如果我清楚地知道tomcat为我的应用程序和jmx创建了数据源,但是在Tomcat 7中,它是单个对象,在Tomcat中8它必须不同。所以我的问题是为什么会发生这种情况?我在文档中找不到有关此更改的任何信息。而且,我感兴趣的是更好的方法:创建单个数据源(我认为是这样)或按工厂创建多个数据源。
If I clearly understand tomcat creates datasource for my app and for jmx, but in Tomcat 7 it was single object, in Tomcat 8 it must be different. So my question is why that situation has happened? I couldn't find any information of this change in documentation. And I'm interesting what is better: create single datasource (I think so) or create several by factory.
推荐答案
我们遇到了同样的问题。我们将数据源声明为spring bean,并且看起来spring和bean本身都试图注册导致这种冲突的Mbean。
我们要做的就是像这样配置Mbean Exporter:
We had the same problem. We declared our data source as a spring bean, and it looks like both spring and the bean itself try to register an Mbean which leads to this conflict.All we had to do is configure our Mbean Exporter like this:
@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
AnnotationMBeanExporter annotationMBeanExporter = new AnnotationMBeanExporter();
annotationMBeanExporter.addExcludedBean("dataSource");
return annotationMBeanExporter;
}
尽管我想将注册策略设置为:
Although I suppose setting the registration policy to:
annotationMBeanExporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);
也可能起作用。
这篇关于迁移到Tomcat 8:InstanceAlreadyExistsException数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!