码迷,mamicode.com
首页 > 编程语言 > 详细

[com.alibaba.druid.stat.DruidDataSourceStatManager.addDataSource(DruidDataSourceStatManager.java:154

时间:2014-11-19 01:54:50      阅读:580      评论:0      收藏:0      [点我收藏+]

标签:register mbean error   druiddatasourcestatm   

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) //这里入口
bubuko.com,布布扣

一路跟踪源码,传入的参数一直是name属性,查看上层 DruidDataSourceStatManager.java  addDataSource(Object dataSource, String name) 
bubuko.com,布布扣

在看上层: DruidDataSource.java line:1298

bubuko.com,布布扣

这里传入了数据源的name属性,经过上面的分析,是利用DataSource的name属性拼接成一个id,然后用次id注册mbean, 然而,druid的name属性写成了一个默认值

bubuko.com,布布扣

故此注册失败。


现在知道了原因,为什么运行几天后才出现这个错误,并且运维说有回滚到了上个版本,在部署(直接重新部署,并没有重启tomcat/jvm)的时候就直接报这个错误,经过上面的推断是jvm中已经有一个此id的mbean了,故此即使重新部署还是失败,我给想了个办法,重启tomcat,搞定,果然奏效。

在来看看druid的wiki中,虽然提示了配置name属性,但是在配置文件中配置后是不起作用的,经过查看源码发现并没有加载次属性,所以配置了没用,后来我在代码中获取配置文件的name属性,然后dataSource.setName(name),虽然名字设置成功了,在druid的监控页面也可以看的自定义的名字,但是不幸的是druid运行几天出错了或者重新部署,然后重新向jvm注册mbean,导致了出错。

再来看看datasource的name的默认值是怎么设置的DruidAbstractDataSource.java line: 849

bubuko.com,布布扣

可以看到在getName属性中是DataSource-加上计算的一个hash值,但是此方法只在getName时返回,而在注册mbean时druid的name默认是null, 在注册mbean时分析下面的代码DruidDataSourceStatManager.java line: 161

bubuko.com,布布扣

代码显示是根据dataSource计算出来莪一个hash值,然后进行注册mbean。


经过上面的分析在注册mbean时,一定要保证registerMBean的name参数唯一,还要在上层拦截异常

InstanceAlreadyExistsException,然后自动处理异常

在使用Druid的时候,不要使用name属性,druid官网并没有给出说明,本文在此给大家一个解释和处理方法。

附: https://github.com/alibaba/druid


[com.alibaba.druid.stat.DruidDataSourceStatManager.addDataSource(DruidDataSourceStatManager.java:154

标签:register mbean error   druiddatasourcestatm   

原文地址:http://blog.csdn.net/lanmo555/article/details/41248763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!