NutzCN Logo
问答 还是那个数据库连接的问题!!!
发布于 10天前 作者 qq_5d22b496 120 次浏览 复制 上一个帖子 下一个帖子
标签:

请教大神!咱们的nutz框架,是一开始就加载数据库了吧,然后如果我想在执行这个程序当中变换数据库,应该怎么办???还是数据库是写死的?要变换数据库必须关闭程序,在配置文件中修改???

14 回复

这标题完全不表意

NutDao都是从DataSource接口取数据库连接,

方案一,做个DataSourceProxy类,实现一下DataSource接口不就好了,在里面随意切换真正的dataSource实例

方案二,如果只是临时切换,那估计也是小应用,使用SimpleDataSource,非连接池实现的DataSource,随时设置其数据库参数,即时生效

大神,我的需求是动态的将一张数据库中的表(包括其数据)全部辅助到另外一张数据库中。我用的datesource去连接,可是只能连接一个数据库,另外一个数据库连接不上!!求解

2017-07-30 16:59:24,750 org.nutz.dao.impl.NutDao.<init>(NutDao.java:105) DEBUG - NutDao[_selfId=5] init ...
2017-07-30 16:59:24,750 org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:98) INFO  - Get Connection from DataSource for JdbcExpert

datasource是枚举来实现单例的

	public static Dao connection(T1_dblog db) {
		ds = getInstance();
		Dao d;
		try {
			ds.setDriverClassName("com.mysql.jdbc.Driver");
			ds.setUrl("jdbc:mysql://" + db.getServerIpPort() + "/" + db.getDbname());
			ds.setUsername(db.getDbuser());
			ds.setPassword(db.getDbpassword());
			d = new NutDao(ds);
		} catch (Exception e) {
			return null;
		}
		return d;

用的是druid?卡住了吗?

没有用池啊,只是单纯的先从一个数据库中取出数据然后存着,断开连接,准备连接第二个数据库的时候连接不上了!

用的是SimpleDataSource?

你的代码catch了异常,但没有打印出来,所以不知道报啥错误呢

我跑断点看了,所有的跑完了,然后运行到d = new NutDao(ds)之后就直接给catch了,控制台也没有,只是显示jsbcExveption

log.debug(e.getMessage(), e)

连接被关闭??

2017-07-30 17:13:44,696 org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:98) INFO  - Get Connection from DataSource for JdbcExpert
2017-07-30 17:13:49,637 com.backend.logic.CreateTable.connection(CreateTable.java:90) DEBUG - java.sql.SQLException: Data source is closed
java.lang.RuntimeException: java.sql.SQLException: Data source is closed
	at org.nutz.lang.Lang.wrapThrow(Lang.java:148)
	at org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:108)
	at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:162)
	at org.nutz.dao.impl.NutDao.<init>(NutDao.java:110)
	at com.backend.logic.CreateTable.connection(CreateTable.java:88)
	at com.backend.logic.CreateTable.insertData(CreateTable.java:146)
	at com.backend.Module.T1_dataBase.createTable(T1_dataBase.java:87)
	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:497)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
	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.NutActionChain.doChain(NutActionChain.java:40)
	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:183)
	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:218)
	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.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Data source is closed

确实连接关闭了!!!我用了一个单例,然后连接完第一个数据库的时候就close了dateSource,然后第二个数据库就连接不上!!!!但是大神不用单例会不会有什么影响???

不同连接信息可不能共享一个DataSource

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