NutzCN Logo
问答 手动写NutDao,数据库断开没有重连,怎么解决
发布于 2682天前 作者 qq_a42f26f3 1746 次浏览 复制 上一个帖子 下一个帖子
标签:

我是用Druid连接数据库,发现数据库偶尔断掉后就不正常了,我采用的是从数据库中读取配置,然后加载Dao的方式,
如果已经初始化则从hm从获取。
Properties properties=new Properties();
try {
properties.put("url", url);
properties.put("username", username);
properties.put("password", password);
properties.put("validationQuery", validationQuery);
properties.put("initialSize", "10");
properties.put("maxActive", "50");
properties.put("testOnBorrow", "false");
properties.put("testOnReturn", "false");
properties.put("testWhileIdle", "true");
properties.put("timeBetweenEvictionRunsMillis", "3000");//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
Dao dao=new NutDao(DruidDataSourceFactory.createDataSource(properties));
hm.put(name, dao);
return dao;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
具体错误如下:
com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:1863)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
不知道还要配置什么项才不会报错?

4 回复

testOnBorrow=true,稍微降低性能,但应该能解决

testOnReturn要不要也改为true?

一般不需要

好,谢谢,我试试看

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