从网上看了很多都是关于spring的 现工作要求写出数据库类,请大神帮帮忙!!!!
@wendal
var ioc = {
dataSource : {
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
},
fields : {
url : "jdbc:mysql://192.168.1.23:3306/test",
username : "dev",
password : "dev12345",
testWhileIdle : true, // 非常重要,预防mysql的8小时timeout问题
//validationQuery : "select 1" , // Oracle的话需要改成 select 1 from dual
maxActive : 100
}
},
slaveDataSource : {
// slave数据源,负责读
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
},
fields : {
url : "jdbc:mysql://192.168.1.23:3306/test",
username : "dev",
password : "dev12345",
testWhileIdle : true, // 非常重要,预防mysql的8小时timeout问题
//validationQuery : "select 1" , // Oracle的话需要改成 select 1 from dual
maxActive : 100
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource"}],//引用主数据源
fields : {
runner : {refer: "daoRunner"}
}
},
daoRunner : {
type : "org.nutz.dao.impl.sql.run.NutDaoRunner",
fields : {
slaveDataSource : {refer:"slaveDataSource"} //引用slave数据源
}
}
};
@Inject
protected Dao daoRunner;//读数据源
@Inject
protected Dao dao;//写数据源
org.nutz.ioc.IocException: IocBean[userModule] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:151)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:216)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:245)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at net.wendal.nutzbook.mvc.LogTimeProcessor.process(LogTimeProcessor.java:19)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2555)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2544)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: IocBean[userModule] fail at field=[daoRunner]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:137)
... 48 more
Caused by: java.lang.RuntimeException: Fail to set field[net.wendal.nutzbook.module.UserModule#daoRunner] using value[org.nutz.dao.impl.sql.run.NutDaoRunner@7b06b606]
at org.nutz.lang.inject.InjectByField.inject(InjectByField.java:27)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:33)
... 50 more
Caused by: org.nutz.castor.FailToCastObjectException: Fail to cast from <org.nutz.dao.impl.sql.run.NutDaoRunner> to <org.nutz.dao.Dao> for {org.nutz.dao.impl.sql.run.NutDaoRunner@7b06b606}
at org.nutz.castor.Castors.cast(Castors.java:262)
at org.nutz.castor.Castors.castTo(Castors.java:316)
at org.nutz.lang.inject.InjectByField.inject(InjectByField.java:22)
... 51 more
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'org.nutz.dao.Dao'
by args: [@(org.nutz.dao.impl.sql.run.NutDaoRunner@7b06b606)]
because: No suitable Constructor or Factory Method!!
at org.nutz.lang.Mirror.born(Mirror.java:1008)
at org.nutz.castor.castor.Object2Object.cast(Object2Object.java:12)
at org.nutz.castor.Castors.cast(Castors.java:252)
... 53 more
按照步骤,但是报错了,请大神解惑!!!@wendal
不过, 默认是单个slave(DataSource), 多个slave的话, 需要自行建一个DataSource类, 代理几个slave对象, 执行getConnection的时候, 随机取出一个slave来获取连接
public class MySlaveDataSource implements DataSource {
protected DataSource [] slaves;
protected Random r = new Random(System.currentTimeMillis());
public Connection getConnection() throws SQLException {
return slaves[r.nextInt(slaves.length)].getConnection();
}
... 其他无关紧要的方法
}
slave_1: {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "slave_1."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
slave_2: {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "slave_2."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
slave_3: {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "slave_3."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
slaveDataSource : {
type : "xxx.yyy.zzz.MySlaveDataSource",
fields : {
slaves : [{refer:"slave_1"}, {refer:"slave_2"}, {refer:"slave_3"}]
}
},
daoRunner : {
type : "org.nutz.dao.impl.sql.run.NutDaoRunner",
fields : {
slaveDataSource : {refer:"slaveDataSource"} //引用slave数据源
}
}