类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
一个resultset执行完毕是否需要关闭。
例如:
resultset=sqlstat.executequery(sql);
.........
这里需要执行resultset.close(); 么?
resultset=sqlstat.executequery(sql);
如果不需要,那么其他方法,例如executeUpdate,execute需要么?
因为我看到jdk1.4.2的chm帮助文件上是这么写的:
close
public void close() throws SQLException
Releases this ResultSet objects database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results. A ResultSet object is also automatically closed when it is garbage collected.
特别是这句: ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed
因此我认为在执行第二个executequery(sql),不需要resultset.close(); 、
请大家帮忙确认一下,谢谢。
网友回答:
实际上Connection.close只是断开同DB的连接,它实际并没去关闭Statement, ResultSet。之所以说Connection.close后,ResultSet无效,是因为ResultSet的操作之前,会检查连接是否有效。所以如next,first等操作都会出错。其他一些跟数据无关的操作就不会有问题。
一定要close掉ResultSet,否则资源会泄漏,因为ResultSet里有Stream对象,close才会调用Stream.close。
规范说明: connection.close 自动关闭 Statement.close 自动导致 ResultSet 对象无效,注意只是 ResultSet 对象无效,ResultSet 所占用的资源可能还没有释放。所以还是应该显式执行connection、Statement、ResultSet的close方法。特别是在使用connection pool的时候,connection.close 并不会导致物理连接的关闭,不执行ResultSet的close可能会导致更多的资源泄露。
看文档的意思,你不需要显示的关闭 ResultSet,
但是建议你不审显示的关闭一下比较好
不用关闭吧!
俺觉得不用关闭
严格来说这些资源的释放可以在finally里完成,严谨的程序设计应该关闭这些以释放资源。