聊聊Druid register mbean error的问题
key: [com.alibaba.druid.stat.druiddatasourcestatmanager.adddatasource(druiddatasourcestatmanager.java:154)] register mbean error
在使用数据库连接池时(本文通用于其他使用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
到此这篇关于聊聊druid register mbean error的问题的文章就介绍到这了,更多相关druid register mbean error内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!