An attempt by a client to checkout a Connection has timed out.
丨苏幕遮丨 2015-10-08 10:24:12 各位大大好,小弟遇到了一个不理解的问题,求解决方法:
有一个每5分钟执行一次的定时任务,执行了两次查询和一次写入数据库的操作。在运行到第三天时发生了如下问题:
An attempt by a client to checkout a Connection has timed out.
然后我重新执行那个任务,结果还是报一样的错,只能重新启动了这个程序,才能正常的开始执行这个定时任务。
在网上查了一下,说是数据库连接没有释放,我代码里这样写:
public int getBoxid(String Boxno) {
Connection conn = null;
int Boxid = 0;
try {
String userName = Constant.ROOT;
String password = Constant.PASSWARD;
String url = Constant.URL;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
PreparedStatement ps = null;
ResultSet rset;
String sql = "select t.ID from t_box_info t where t.BOXNO = '"
+ Boxno + "'";
ps = conn.prepareStatement(sql);
rset = ps.executeQuery();
while (rset.next()) {
// Boxid
Boxid = rset.getInt(1);
}
} catch (Exception e) {
System.err.println("Cannot connect to database server");
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */
}
}
}
return Boxid;
}
请问,怎么修改能够做到确保我申请的连接能够释放呢?