定义了两个数据源:
#mysql
db.name=jmsmall
db.url=jdbc:mysql://127.0.0.1:3306/jmsmall?useUnicode=true&characterEncoding=utf8&useSSL=false
db.username=root
db.password=123456
db.validationQuery=select 1
db.maxActive=100
db.testWhileIdle=true
db.filters=mergeStat
db.connectionProperties=druid.stat.slowSqlMillis=2000
db.defaultAutoCommit=true
db2.name=jmsmall
db2.url=jdbc:mysql://127.0.0.1:3306/jms?useUnicode=true&characterEncoding=utf8&useSSL=false
db2.username=root
db2.password=123456
db2.validationQuery=select 1
db2.maxActive=100
db2.testWhileIdle=true
db2.filters=mergeStat
db2.connectionProperties=druid.stat.slowSqlMillis=2000
db2.defaultAutoCommit=true
dataSource : {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "db."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource"}],
fields : {
executor : {refer:"cacheExecutor"}
}
},
dataSource2 : {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "db2."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
dao2 : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource2"}],
},
测试用例如下:
package cn.wizzer.test;
import cn.wizzer.app.web.commons.core.Module;
import net.sf.ehcache.CacheManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.IocLoader;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.combo.ComboIocLoader;
import org.nutz.mvc.annotation.IocBy;
public class TestMultipleDataSource {
protected Ioc ioc;
protected Dao dao;
protected Dao dao2;
@Before
public void before() throws Exception {
CacheManager.getInstance();
IocBy iocBy = Module.class.getAnnotation(IocBy.class);
IocLoader loader = new ComboIocLoader(iocBy.args());
ioc = new NutIoc(loader);
dao = ioc.get(Dao.class);
dao2 = ioc.get(Dao.class, "dao2");
}
@After
public void after() throws Exception {
if (ioc != null) {
ioc.depose();
}
}
/**
* 测试多数据源
*/
@Test
public void test_dataSource() {
Sql sql = Sqls.create("select database()");
sql.setCallback(Sqls.callback.str());
dao.execute(sql);
String database = sql.getString();
System.out.println("first dataSource:" + database);
Sql sql2 = Sqls.create("select database()");
sql2.setCallback(Sqls.callback.str());
dao2.execute(sql2);
String database1 = sql2.getString();
System.out.println("second dataSource:" + database1);
}
}
执行抛出如下异常:
[DEBUG] 2017-12-06 15:41:13,906 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - select database()
first dataSource:jmsmall
[DEBUG] 2017-12-06 15:41:13,911 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - select database()
second dataSource:jms
[INFO ] 2017-12-06 15:41:13,915 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:261) - org.nutz.ioc.impl.NutIoc@2050370411 is closing. startup date [17-12-06 15:41:12.963]
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'dao2' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheManager' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheProvider' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheExecutor' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'conf' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'dao' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_wkcache' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_syslog' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_slog' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_cuxlog' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_async' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'dynamicInterceptor' ...
[DEBUG] 2017-12-06 15:41:13,916 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object '$aop_1_plugin' ...
[DEBUG] 2017-12-06 15:41:13,917 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:113) - Depose object 'dataSource2' ...
[INFO ] 2017-12-06 15:41:13,919 com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1514) - {dataSource-2} closed
[DEBUG] 2017-12-06 15:41:13,919 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:113) - Depose object 'dataSource' ...
[ERROR] 2017-12-06 15:41:13,920 com.alibaba.druid.stat.DruidDataSourceStatManager.removeDataSource(DruidDataSourceStatManager.java:192) - unregister mbean error
javax.management.InstanceNotFoundException: com.alibaba.druid:type=DruidDataSource,id=jmsmall
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.exclusiveUnregisterMBean(DefaultMBeanServerInterceptor.java:427)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.unregisterMBean(DefaultMBeanServerInterceptor.java:415)
at com.sun.jmx.mbeanserver.JmxMBeanServer.unregisterMBean(JmxMBeanServer.java:546)
at com.alibaba.druid.stat.DruidDataSourceStatManager.removeDataSource(DruidDataSourceStatManager.java:190)
at com.alibaba.druid.pool.DruidDataSource$2.run(DruidDataSource.java:1542)
at java.security.AccessController.doPrivileged(Native Method)
at com.alibaba.druid.pool.DruidDataSource.unregisterMbean(DruidDataSource.java:1538)
at com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1497)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
at org.nutz.ioc.ObjectProxy.depose(ObjectProxy.java:83)
at org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:114)
at org.nutz.ioc.impl.ScopeContext.depose(ScopeContext.java:121)
at org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:271)
at cn.wizzer.test.TestMultipleDataSource.after(TestMultipleDataSource.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
[INFO ] 2017-12-06 15:41:13,921 com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1514) - {dataSource-1} closed
请问大神应该如何排查?