Java JDBC:Reply.fill()

我有时会得到以下异常:

com.ibm.db2.jcc.b.gm: [jcc][t4][2030][11211][3.50.152] A communication error occurred during operations on the connection’s underlying socket,socket input stream,
or socket output stream. Error location: Reply.fill(). Message: Connection reset. ERRORCODE=-4499,sqlSTATE=08001

问题是,代码成功执行了一段时间,然后突然我得到了这个异常.但是,当我再次运行代码时,它会正常运行.

有人可以告诉我可能出现的问题并提供一些解决方法.

解决方法

这是未正确关闭/释放JDBC资源的标志.您需要在尽可能短的范围内获取关闭所有JDBC资源,即您需要在与获取它们完全相同的方法块的try块的finally块中以相反的顺序关闭它们.例如.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
    connection = database.getConnection();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);
    // ...
} finally {
    if (resultSet != null) try { resultSet.close(); } catch (sqlException logorIgnore) {}
    if (statement != null) try { statement.close(); } catch (sqlException logorIgnore) {}
    if (connection != null) try { connection.close(); } catch (sqlException logorIgnore) {}
}

如果你没有尽快正确关闭它们,数据库迟早会把它拿在手里,你的应用程序可能会在你遇到自己的时候迟早打破.

要提高连接性能,请使用连接池 – 您仍然需要以与上面相同的方式获取关闭它们!它现在只是连接池实现,它在引擎盖下担心实际关闭连接.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...