java - 迁移到 Tomcat 8 : InstanceAlreadyExistsException datasource

标签 java tomcat configuration context.xml

我有一个关于 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&amp;characterEncoding=UTF-8&amp;profileSQL=false&amp;autoSlowLog=false&amp;slowQueryThresholdMillis=100&amp;autoReconnect=true"/>

所以,它在 tomcat 7 中运行没有任何问题。在 Tomcat 8 中,我可以通过 2 种方式解决这个问题:

  1. 通过添加资源:singleton = "false";
  2. 通过添加资源:factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

如果我清楚地了解 tomcat 为我的应用程序和 jmx 创建数据源,但在 Tomcat 7 中它是单个对象,在 Tomcat 8 中它必须不同。所以我的问题是为什么会发生这种情况?我在文档中找不到有关此更改的任何信息。而且我很感兴趣什么更好:创建单个数据源(我认为是这样)或通过工厂创建多个。

最佳答案

我们遇到了同样的问题。我们将数据源声明为 spring bean,看起来 spring 和 bean 本身都试图注册一个导致此冲突的 Mbean。 我们所要做的就是像这样配置我们的 Mbean Exporter:

@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
    AnnotationMBeanExporter annotationMBeanExporter = new AnnotationMBeanExporter();
    annotationMBeanExporter.addExcludedBean("dataSource");
    return annotationMBeanExporter;
}

虽然我想将注册策略设置为:

annotationMBeanExporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);

也许也可以。

关于java - 迁移到 Tomcat 8 : InstanceAlreadyExistsException datasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28295503/

相关文章:

Tomcat多域Jelastic教程

java - 在 tomcat 集群中协调工作人员的最佳方式是什么?

Java:在 war 中访问属性文件

java - 如何在一组类型中创建具有通配符类型的通用方法?

Java - DB2 连接问题

java - 通过两个不同的按钮播放两个不同的声音文件?

java - Apache Tomcat 9 无法启动(eclipse)

configuration - Cucumber - 如何将其配置为默认使用 "--format pretty"?

configuration - 如何修复 "Your account does not belong to the same domain as this cloud project or app"错误?

java - 调试java银行账户程序(1个错误)