记录一次使用淘宝连接池com.alibaba.druid.stat.DruidDataSourceStatManager产生的错误

记录一次使用淘宝连接池com.alibaba.druid.stat.DruidDataSourceStatManager产生的错误

druid使用时很方便,提供了强大的监控和扩展功能。

错误产生场景:单台Tomcat部署多个 springboot 构建的项目,并且多个项目都使用driud连接池,单独启动其中任何一个都没有问题。同时启动时就会报错

 org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.alibaba.druid.filter.stat.StatFilter@36ee5032] with key 'statFilter'; nested exception is javax.ma
 nagement.InstanceAlreadyExistsException: com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter  at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:625)
     at org.springframework.jmx.export.MBeanExporter.lambda$registerBeans$2(MBeanExporter.java:551)
     at java.util.HashMap.forEach(HashMap.java:1288)
     at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:551)
     at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:434)
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:777)
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:155)
     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:135)
     at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:87)
     at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5178)
     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
     at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1107)
     at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1841)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
     at java.lang.Thread.run(Thread.java:745)
 Caused by: javax.management.InstanceAlreadyExistsException: com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter
     at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
     at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
     at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:137)
     at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:671)
     at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:615)
     ... 27 common frames omitted
 ​

上边错误总结起来就是:

 Caused by: javax.management.InstanceAlreadyExistsException: com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter

 

出现这种问题是因为同时部署的时候spring 默认的mbean名字都一样了,解决问题只需要在每个yml下配置spring.jmx.default-domain= 自定义名字 ,名字不同,实例化出的对象就不一样了。

后来找到了druid的源码,也有人在GitHub提问同样的问题。

https://github.com/alibaba/druid/issues?q=InstanceAlreadyExistsException

还有大神提供了另外的解决方案把spring的自动探测关闭,应用还没有到全局复用对象的时候,即便使用也估计是使用RPC框架,以下是关闭jmx自动探测的spring配置

 
<bean class="org.springframework.jmx.export.MBeanExporter">  
         <property name="autodetectModeName">  
             <value>AUTODETECT_NONE</value>  
         </property>  
     </bean> 
 Spring提供了4个取值:
 AUTODETECT_NONE 不启用自动探测,需要手动向MBean Server进行注册,即通过MBeanExporter的beans属性进入注册;
 AUTODETECT_MBEAN 在当前IOC容器中进行查找MBean组件;
 AUTODETECT_ASSEMBLER 设置根据MBeanInfoAssembler的策略进行探测;
 AUTODETECT_ALL 自动探测,是AUTODETECT_MBEAN和AUTODETECT_ASSEMBLER的并集。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,错误信息表明数据源已关闭。这可能是由于以下原因之一导致的: 1. 数据库连接池中的连接已经关闭,但是应用程序仍然在尝试使用它们。 2. 数据库连接池配置不正确,导致连接池无法正常工作。 为了解决这个问题,可以尝试以下步骤: 1. 检查应用程序中是否有未关闭的数据库连接。可以使用JDBC API手动关闭连接,或者使用Spring框架等ORM工具自动管理连接。 2. 检查数据库连接池的配置是否正确。可以尝试使用默认配置,或者根据官方文档调整配置参数。 以下是一些可能有用的代码片段和参考链接: 1. 手动关闭JDBC连接的示例代码: ```java Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); // 执行SQL语句等操作 } catch (SQLException e) { // 处理异常 } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // 处理异常 } } } ``` 2. Spring Boot使用Druid连接池的示例配置: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.filters=stat,wall,log4j spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值