在使用数据库连接池时(本文通用于其它使用jmx mbean的应用)。执行几天后出现例如以下错误
2014/11/18 10:31:00,617 [ERROR] [localhost-startStop-6] [com.alibaba.druid.stat.DruidDataSourceStatManager.addDataSource(DruidDataSourceStatManager.java:154)] register mbean error javax.management.InstanceAlreadyExistsException: com.alibaba.druid:type=DruidDataSource,id=Druid MySQL DB pool at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312) at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:482) at com.alibaba.druid.stat.DruidDataSourceStatManager.addDataSource(DruidDataSourceStatManager.java:152) at com.alibaba.druid.pool.DruidDataSource$1.run(DruidDataSource.java:1298) at java.security.AccessController.doPrivileged(Native Method) at com.alibaba.druid.pool.DruidDataSource.registerMbean(DruidDataSource.java:1294) at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:623) at com.longdai.data.ConnectionManagerDruid.<init>(ConnectionManagerDruid.java:68) at com.longdai.data.ConnectionManager.getInstance(ConnectionManager.java:86) at com.longdai.data.dao.Database.<clinit>(Database.java:22) at com.longdai.service.admin.CloseNetWorkService.getNetWorkById(CloseNetWorkService.java:87) at com.longdai.service.admin.CloseNetWorkService$$FastClassByCGLIB$$bbdb465c.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at com.gozap.services.ServiceMethodInterceptor.invoke(ServiceMethodInterceptor.java:31) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635) at com.longdai.service.admin.CloseNetWorkService$$EnhancerByCGLIB$$19cbebaf.getNetWorkById(<generated>) at com.longdai.system.listener.CloseNetWorkConfigiListener.contextInitialized(CloseNetWorkConfigiListener.java:37) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:963) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1600) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619)查看源代码,一直跟踪到
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484) // 这里 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312) //这里入口
一路跟踪源代码。传入的參数一直是name属性,查看上层 DruidDataSourceStatManager.java addDataSource(Object dataSource, String name)
在看上层: DruidDataSource.java line:1298
这里传入了数据源的name属性。经过上面的分析,是利用DataSource的name属性拼接成一个id,然后用次id注冊mbean, 然而。druid的name属性写成了一个默认值
故此注冊失败。
如今知道了原因,为什么执行几天后才出现这个错误,而且运维说有回滚到了上个版本号,在部署(直接又一次部署,并没有重新启动tomcat/jvm)的时候就直接报这个错误,经过上面的判断是jvm中已经有一个此id的mbean了,故此即使又一次部署还是失败,我给想了个办法,重新启动tomcat。搞定,果然奏效。
在来看看druid的wiki中,尽管提示了配置name属性,可是在配置文件里配置后是不起作用的。经过查看源代码发现并没有载入次属性,所以配置了没用。后来我在代码中获取配置文件的name属性。然后dataSource.setName(name)。尽管名字设置成功了,在druid的监控页面也能够看的自己定义的名字,可是不幸的是druid执行几天出错了或者又一次部署,然后又一次向jvm注冊mbean,导致了出错。
再来看看datasource的name的默认值是怎么设置的DruidAbstractDataSource.java line: 849
能够看到在getName属性中是DataSource-加上计算的一个hash值,可是此方法仅仅在getName时返回,而在注冊mbean时druid的name默认是null, 在注冊mbean时分析以下的代码DruidDataSourceStatManager.java line: 161
代码显示是依据dataSource计算出来莪一个hash值,然后进行注冊mbean。
经过上面的分析在注冊mbean时。一定要保证registerMBean的name參数唯一。还要在上层拦截异常
InstanceAlreadyExistsException。然后自己主动处理异常
总结:在使用Druid的时候。不要使用name属性,druid官网并没有给出说明,本文在此给大家一个解释和处理方法。另外1.0.5版本号配置后是不起作用的,须要自己强行setName。如自己配置了name,为防止出错。最好改动Druid源代码 DruidDataSourceStatManager.java addDataSource(Object dataSource, String name) 。在生成objectName是在加一个 hash(參考161)。另外大家在注冊MBean时。一定要保证id的唯一,以及反复的处理方式。假设出现了这个错误,就将与本项目使用的同一个jvm的项目都关掉,清理掉本jvm,然后重新启动服务
附: https://github.com/alibaba/druid