NutzCN Logo
问答 编写数据库类,关于主从数据库读写分离,从该类获取连接,使用druid连接池!
发布于 287天前 作者 qq_3ef73b2c 194 次浏览 复制 上一个帖子 下一个帖子
标签:

从网上看了很多都是关于spring的 现工作要求写出数据库类,请大神帮帮忙!!!!
@wendal

29 回复
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

protected Dao daoRunner; // 怎么就能写出这样的东西呢?

总共就一个Dao实例啊

那我要怎么改呢?我现在要用从库对数据库进行操作

意思要换一个数据源吗?一个数据库只能对应一个dao实例吗?

你要手动进行读写分离??

要求是能够有好几个从库,对从库进行操作是随机选择从库,反正只要读写分离就行。
dao.js读写分离配置之后,它对读写操作是怎么执行的?不用手动调用吗?

不需要的,直接用dao就行啦

你意思是读写分离是Nutz自己本身就自己执行的不需要手动调用吗?

不过, 默认是单个slave(DataSource), 多个slave的话, 需要自行建一个DataSource类, 代理几个slave对象, 执行getConnection的时候, 随机取出一个slave来获取连接

是啊, 不然NutDaoRunner提供个slaveDataSource干啥

好的,有demo吗?我是菜鸟,能有代码最好了,谢谢

看完新手入门,并敲完代码,但是有些配置的定义我觉得不是很清楚,有进阶文档吗?

您能给我一个关于datasource类的代码段吗?有点懵逼

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();
    }
    ... 其他无关紧要的方法
}

非常感谢!!!!

关于编写db类,还需要写那些execute close方法吗?还是Nutz本身已经封装好了?

db类是什么东西

就是数据库操作的工具类

不需要工具类吧, 直接写service层就是了

那我现在只需要在dao.js中配置多个从库的配置,之后编写datasorce类就可以了是吗?

把slave数据源作为数组,注入到MySlaveDataSource,再把后者注入到daoRunner

是按照您给我的代码这样写就可以了吗?注入的方法不是很清楚

我很怀疑你把nutzbook看完做完没

做到redis就没做了,就做一次就开始编写新项目的工具类所以不是很深刻,求解答

        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数据源
        }
    }

非常非常感谢!!!

添加回复
请先登陆
回到顶部