关闭

JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题

标签: 数据库连接池jdbc数据库
454人阅读 评论(0) 收藏 举报
分类:

本文转自:http://k1121.iteye.com/blog/1279063


(1)    主要问题


针对关闭connection是否会自动关闭Statement和ResultSet的问题,以及Statement和ResultSet所占用资源是否会自动释放问题,JDBC处理规范或JDK规范中做了如下描述:

JDBC处理规范

JDBC. 3.0 Specification——13.1.3 Closing Statement Objects

An application calls the method Statement.close to indicate that it has finished processing a statement. All Statement objects will be closed when the connection that created them is closed. However, it is good coding practice for applications to close statements as soon as they have finished processing them. This allows any external resources that the statement is using to be released immediately.

Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object. The resources held by the ResultSet object may not be released until garbage collection runs again, so it is a good practice to explicitly close ResultSet objects when they are no longer needed.

These comments about closing Statement objects apply to PreparedStatement and CallableStatement objects as well.

JDBC. 4.0 Specification——13.1.4 Closing Statement Objects

An application calls the method Statement.close to indicate that it has finished processing a statement. All Statement objects will be closed when the connection that created them is closed. However, it is good coding practice for applications to close statements as soon as they have finished processing them. This allows any external resources that the statement is using to be released immediately.

Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object. The resources held by the ResultSet object may not be released until garbage collection runs again, so it is a good practice to explicitly close ResultSet objects when they are no longer needed.

Once a Statement has been closed, any attempt to access any of its methods with the exception of the is Closed or close methods will result in a SQLException being thrown.

These comments about closing Statement objects apply to PreparedStatement and CallableStatement objects as well.

规范说明:connection.close 自动关闭 Statement.close 自动导致 ResultSet 对象无效(注意只是 ResultSet 对象无效,ResultSet 所占用的资源可能还没有释放)。所以还是应该显式执行connection、Statement、ResultSet的close方法。特别是在使用connection pool的时候,connection.close 并不会导致物理连接的关闭,不执行ResultSet的close可能会导致更多的资源泄露。

JDK处理规范:

JDK1.4

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. 

Note: A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed. 

Note: A Connection object is automatically closed when it is garbage collected. Certain fatal errors also close a Connection object. 

JDK1.5

Releases this ResultSet object's 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. 

规范说明:

1.垃圾回收机制可以自动关闭它们;

2.Statement关闭会导致ResultSet关闭;

3.Connection关闭不一定会导致Statement关闭。


V6使用的是数据库连接池,Connection关闭并不是物理关闭,只是归还连接池,所以Statement和ResultSet有可能被持有,并且实际占用相关的数据库的游标资源,在这种情况下,只要长期运行就有可能报“游标超出数据库允许的最大值”的错误,导致程序无法正常访问数据库。

(2)    解决建议

 1)      由于垃圾回收的线程级别是最低的,为了充分利用数据库资源,有必要显式关闭它们,尤其是使用Connection Pool的时候;

 2)      最优经验是按照ResultSet,Statement,Connection的顺序执行close;

 3)      为了避免由于java代码有问题导致内存泄露,需要在rs.close()和stmt.close()后面一定要加上rs = null和stmt = null;

 4)      如果一定要传递ResultSet,应该使用RowSet,RowSet可以不依赖于Connection和Statement。Java传递的是引用,所以如果传递ResultSet,你会不知道Statement和Connection何时关闭,不知道ResultSet何时有效。

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题

(1)    主要问题  针对关闭connection是否会自动关闭Statement和ResultSet的问题,以及Statement和ResultSet所占用资源是否会自动释放问题,JDBC处理规范或JDK规范中做了如下描述:  JD...
  • qiruiduni
  • qiruiduni
  • 2014-09-05 13:41
  • 938

[疯狂Java]JDBC:Statement、ResultSet、连接资源自动关闭、Properties配置文件

1. 建立连接得到Connection对象后如何继续操作数据库?     1) 查询数据库就必须要执行SQL语句,可以通过Connection对象conn获取一个代表要提交给数据库的SQL语句的句柄,即Statement对象;     2) 使用Connect...
  • Lirx_Tech
  • Lirx_Tech
  • 2016-04-12 11:23
  • 1700

JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题

本文转自:http://k1121.iteye.com/blog/1279063(1)    主要问题针对关闭connection是否会自动关闭Statement和ResultSet的问题,以及Statement和ResultSet所占用资源是否会自动释放问题,JDBC...
  • baidu_29293979
  • baidu_29293979
  • 2017-04-14 11:59
  • 137

Connection、PreparedStatement、Statement、ResultSet是JDBC编程常用的接口,请分别简述它们的作用?

JDBC定义: JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的JavaAPI,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC为工具/数据库开发人员提供了一个标准的API,据此可以构建更高级的工具...
  • dingxingmei
  • dingxingmei
  • 2014-09-14 16:09
  • 2200

(韩顺平讲解)jdbc学习(一)---jdbc简介、Statement、ResultSet、Connection的使用

Jdbc简介 jdbc 是sun 公司定义的一套访问数据库的规范( 接口 + 类),主要是放在 java.sql 包javax.sql 包.  原理图:   做jdbc开发的几个前提 1.import...
  • u012301841
  • u012301841
  • 2014-07-27 17:30
  • 1160

使用JDBC过程中如何正确关闭connection

来看一段代码:import java.sql.*; /** * Created by N3verL4nd on 2017/4/17. */ public class JdbcDemo { public static void main(String[] args) { ...
  • lgh1992314
  • lgh1992314
  • 2017-04-18 15:38
  • 2088

怎样实现关闭connection时自动关闭Statement和ResultSet

转自:怎样实现关闭connection时自动关闭Statement和ResultSet 关闭数据连接时, 一定也要关闭Statement和ResultSet, 不然在并发量较大的时候可能导致内存泄漏. (如果是用tomcat自带的数据源实现, 则可以通过添加interceptor实现自...
  • diligentcat
  • diligentcat
  • 2017-07-27 10:18
  • 273

大家来说一说关于jdbc的那些事情,关于Connection,Statement和ResultSet的关闭顺序

以下只是个人观点,毕竟楼主只是大三学生一枚。还有很多需要学习的地方。 最近在做一个jsp+servlet的程序。 用的是Oracle数据库,然后安装的时候很多东西都是默认值。 用servlet,简单点的,当然是用jdbc操作数据库咯。 然后就出现了以下的事情,oracle经常会报错, ORA...
  • u012916287
  • u012916287
  • 2014-12-23 21:37
  • 4869

statement与resultset的对应关系

问题描述: 1,Connection是Statement的工厂,一个Connection可以生产多个Statement。 2,Statement是ResultSet的工厂,一个Statement却只能对应一个ResultSet(它们是一一对应的关系)。 所以在一段程序里要用多个ResultSet的...
  • hanghangde
  • hanghangde
  • 2015-12-06 21:55
  • 1734

java开发中如何在ResultSet结果集关闭后,还能使用数据库数据。

众所周知,在java开发中,涉及到数据库操作时,总会需要一系列的连接数据库的操作类的实例化和使用,如Connection类,PreparedStatement类/Statement类还有ResultSet类。但是,每每使用时,我们总需要在try/catch语句中,finally块中关闭所有资源。否则...
  • Krishnna
  • Krishnna
  • 2016-10-30 18:41
  • 1040
    个人资料
    • 访问:1255079次
    • 积分:16383
    • 等级:
    • 排名:第740名
    • 原创:256篇
    • 转载:1246篇
    • 译文:92篇
    • 评论:79条