JDBC 关闭顺序问题
来源:互联网 发布:辐射4男主角捏脸数据 编辑:程序博客网 时间:2024/06/15 00:44
(转)
主要问题
针对关闭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有可能被持有,并且实际占用相关的数据库的游标资源,在这种情况下,只要长期运行就有可能报“游标超出数据库允许的最大值”的错误,导致程序无法正常访问数据库。
解决建议
(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
- JDBC 关闭顺序问题
- JDBC的关闭顺序深层解答
- jdbc的con、pstmt、rs的非正常关闭顺序探讨
- jdbc的con、pstmt、rs的非正常关闭顺序探讨
- JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题
- JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题
- JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题
- JDBC数据库连接池connection关闭后Statement和ResultSet未关闭的问题
- Java JDBC 关闭链接
- jdbc connection 关闭
- JDBC连接与关闭
- JDBC 资源关闭
- JDBC- 正确关闭数据库连接
- RAC启动关闭顺序
- 集群的关闭顺序
- io流关闭顺序
- 大家来说一说关于jdbc的那些事情,关于Connection,Statement和ResultSet的关闭顺序
- 关闭和释放 JDBC 资源
- 【数据处理】R语言--data.table介绍以及例子
- 选择排序
- PostgreSQL 10 流式物理、逻辑主从 最佳实践
- GC的算法和种类
- 负载均衡的几种算法Java实现代码
- JDBC 关闭顺序问题
- 我所理解的设计模式(C++实现)——命令模式(Command Pattern)
- 【如何快速的开发一个完整的iOS直播app】(原理篇)
- Dubbo项目启动后,没有提供者。项目启动日志正常,DUBBO服务启动没有注册到zookeeper。
- JAVA中System.getProperty()方法使用
- 深度优先搜索求最大流
- vim中的encoding、fileencoding、fileencodings如何设置
- 前端控制器设计模式
- 新手上手NVIDIA Jetson TX2--Jetpack 3.0 安装过程