`
k1121
  • 浏览: 176586 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • JDBC
 
阅读更多
(1)    主要问题

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

JDBC处理规范

JDBC. 3.0 Specification——13.1.3 Closing Statement Objects

An applicationcalls the method Statement.close to indicate that it has finished processing astatement. All Statement objects will be closed when the connection thatcreated them is closed. However, it is good coding practice for applications toclose statements as soon as they have finished processing them. This allows anyexternal resources that the statement is using to be released immediately.

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

These commentsabout closing Statement objects apply to PreparedStatement and CallableStatementobjects as well.

JDBC. 4.0 Specification——13.1.4 Closing Statement Objects

An applicationcalls the method Statement.close to indicate that it has finished processing astatement. All Statement objects will be closed when the connection that createdthem is closed. However, it is good coding practice for applications to closestatements as soon as they have finished processing them. This allows any externalresources that the statement is using to be released immediately.

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

Once aStatement has been closed, any attempt to access any of its methods with theexception of the isClosed or close methods will result in a SQLException beingthrown.

These commentsabout closing Statement objects apply to PreparedStatement andCallableStatement 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 theStatement object that generated it when that Statement object is closed,re-executed, or is used to retrieve the next result from a sequence of multipleresults. A ResultSet object is also automatically closed when it is garbagecollected.

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

Note: A Connection object isautomatically closed when it is garbage collected. Certain fatal errors alsoclose a Connection object.

JDK1.5

Releases this ResultSet object'sdatabase and JDBC resources immediately instead of waiting for this to happenwhen it is automatically closed.

Note: A ResultSet object isautomatically closed by the Statement object that generated it when thatStatement object is closed, re-executed, or is used to retrieve the next resultfrom a sequence of multiple results. A ResultSet object is also automaticallyclosed 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何时有效。
5
1
分享到:
评论
2 楼 k1121 2015-10-09  
zhrb 写道
3.Connection关闭不一定会导致Statement关闭。


这句错了吧,除非是连接池。


这个博文的标题就是jdbc连接池的资源泄露
1 楼 zhrb 2015-06-05  
3.Connection关闭不一定会导致Statement关闭。


这句错了吧,除非是连接池。

相关推荐

    Spring Data JDBC与JDBC的区别

    JDBC规范   java.sql和javax.sql两个包中的类与接口(天龙八部):  DataSource:数据源  DriverManager:驱动管理  Driver:JDBC驱动 ... JDBC数据库连接池/Connection Pool  DBCP:apache tomcat内置  

    用java写的一个数据库连接池

    自己开发的一个数据库连接池,包含代码,在oracle测试了一下,请各位对代码指正。 使用的方法如下: 1.在工程里面加入以下jar文件:mydatasource.jar,dom4j.jar,ojdbc14.jar 2.在工程目录任意位置放入连接池配置文件...

    数据库-数据库编码解决方案

    我们知道进行编码和转码工作都是集中在JDBC的两个接口PreparedStatement和ResultSet上进行的,主要涉及PreparedStatement的setString方法以及ResultSet的getString方法。前面我们讲过需要加入一个连接封装层来对...

    JDBC相关单元测试及通用的Dao

    jdbc详细测试用例,包括connection ,statement,preparedstatement,resultset,BeanUtils,DBUtils,数据库连接池dbcp,cp03的单元测试,及dao层的统一封装,适合项目开发人员学习和项目中使用。

    JDBC笔记 JDBC笔记

    如为连接管理、分布式事务和旧有的连接提供了更好的抽象,它引入了容器管理的连接池、分布式事务和行集等。 注:除了标出的Class,其它均为接口。 API 说明 java.sql.Connection 与特定数据库的连接(会话)。...

    数据库工具类DatabaseUtil.java

    * Close a jdbc resource, such as ResultSet, Statement, Connection.... All * these objects must have a method signature is void close(). * * @param resource - * jdbc resouce to close */ public ...

    DruidJDBCUtils.java

    * 获取连接池方法 */ public static DataSource getDataSource(){ return ds; } /** * 关闭资源方法 * close()查询sql方法 */ public static void close(ResultSet resultSet, Statement ...

    Java数据库编程宝典3

    4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement语句 4.8.3 CallableStatement 4.9 事务 4.9.1 事务独立性等级 ...

    Java数据库编程宝典2

    4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement语句 4.8.3 CallableStatement 4.9 事务 4.9.1 事务独立性等级 ...

    Java数据库编程宝典1

    4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement语句 4.8.3 CallableStatement 4.9 事务 4.9.1 事务独立性等级 ...

    Java数据库编程宝典4

    4.6 连接池 4.7 分布式事务处理 4.7.1 分布式事务管理 4.7.2 Connection对象 4.8 SQL语句 4.8.1 Statement对象 4.8.2 PreparedStatement语句 4.8.3 CallableStatement 4.9 事务 4.9.1 事务独立性等级 ...

    很好的一个jsp分页

    因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。  因此比较好的分页做法应该是每次翻页的时候只从数据库里检索页面大小的块区的数据。这样虽然每次翻页...

    C3P0连接池

    import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Test { public ...

    spring-boot-data-source-decorator:与p6spy,datasource-proxy,flexy-pool和spring-cloud-sleuth集成的Spring Boot

    增加了拦截和记录sql查询的功能,包括拦截大多数Connection , Statement和ResultSet方法调用 -增加了拦截所有查询以及Connection , Statement和ResultSet方法调用的功能 添加连接池指标(jmx,codahale,...

    MYSQL中文手册

    MySQL如何打开和关闭表 7.4.10. 在同一个数据库中创建多个表的缺陷 7.5. 优化MySQL服务器 7.5.1. 系统因素和启动参数的调节 7.5.2. 调节服务器参数 7.5.3. 控制查询优化器的性能 7.5.4. 编译和链接怎样影响...

    mysql官方中文参考手册

    MySQL如何打开和关闭表 7.4.10. 在同一个数据库中创建多个表的缺陷 7.5. 优化MySQL服务器 7.5.1. 系统因素和启动参数的调节 7.5.2. 调节服务器参数 7.5.3. 控制查询优化器的性能 7.5.4. 编译和链接怎样影响MySQL的...

    MySQL 5.1参考手册中文版

    MySQL如何打开和关闭表 7.4.10. 在同一个数据库中创建多个表的缺陷 7.5. 优化MySQL服务器 7.5.1. 系统因素和启动参数的调节 7.5.2. 调节服务器参数 7.5.3. 控制查询优化器的性能 7.5.4. 编译和链接怎样影响...

    MySQL 5.1参考手册

    MySQL如何打开和关闭表 7.4.10. 在同一个数据库中创建多个表的缺陷 7.5. 优化MySQL服务器 7.5.1. 系统因素和启动参数的调节 7.5.2. 调节服务器参数 7.5.3. 控制查询优化器的性能 7.5.4. 编译和链接怎样影响MySQL的...

    ibatis 开发指南(pdf)

    这里的“半自动化”,是相对Hibernate 等提供了全面的数据库封装机制的“全自动化” ORM 实现而言,“全自动”ORM 实现了POJO 和数据库表之间的映射,以及SQL 的自动 生成和执行。而ibatis 的着力点,则...

Global site tag (gtag.js) - Google Analytics