现在的位置: 首页 > 综合 > 正文

不关闭Statement对象会发生什么

2013年02月28日 ⁄ 综合 ⁄ 共 2763字 ⁄ 字号 评论关闭
不关闭Statement对象会发生什么?——“译”笑大方
2008年05月28日 星期三 下午 02:29
java.sun论坛找到一篇资料,把鸟语翻成了中国字,小试下水果刀,希望不是弄巧成拙、“译”笑大方。

不关闭Statement对象会发生什么?

当一些Statement对象在循环内部创建且没有关闭,会发生什么嘞?

===================================================================================
1,通过这些Statement对象创建的ResultSet对象也将不会关闭;

2,取决于数据库对JDBC驱动的具体实现,有可能因数据库连接保持对Statement对象的引用,它们将不会被GC回收,直到数据库连接关闭;

3,当数据库连接被关闭(真正的关闭,而不是放回连接池)时,这些Statement对象以及它们创建的ResultSets对象也将关闭;

4,一些数据库,大量的Statements对象和开放的ResultSets对象会耗尽数据库服务器的资源——Oracle的“开放游标”问题是个典型的例子,大都是因为存在大量开放的ResultSets对象;

最终一切还是取决于你的代码,未关闭的Statements简直是一件糟糕透顶的事情,如果碰巧使用了数据库连接池,相信不用多久你就会祈祷:神那,您还是带我走吧。

===================================================================================
当从 数据源 或者 数据库连接池 获取 数据库连接,用完后再放回连接池时,那些未关闭的Statement对象会发生什么?它们会自行了断还是被GC灭掉?

===================================================================================
如果数据库连接保持对一个Statement对象的引用,那么,怎么说呢,它们俩将这么一直耗着,直至你的JVM内存溢出亦或歇菜(崩溃),你的数据库呢当然也好不到哪儿去,资源耗尽罢工后中止你的所有数据库连接,或者直到有哪位好心人帮你关掉这个数据库连接。

在使用数据库连接池的情况下,未关闭的Statement对象通常是一朵娇艳诱人的罂粟花。

附原文
==================================================================================
unclosed Statement objs: what happens?

When a number of Statement objects are created in a loop and they are not closed what happens?

===================================================================================
a) Any ResultSet objects they create that are not otherwise closed also remain open

b) depending on the driver implementation, the parent Connection may reference them and they therefore don't become eligible for garbage collection until the parent Connection is closed

c) when the parent Connection is closed (really closed, not returned to a connection pool), then the Statements and any child ResultSets are closed

d) On some database, having enough Statements or ResultSets open will run the database out of resources - the Oracle "open cursors" problem is the classic example, caused (mostly) by open ResultSets

Depending on you code, not closing Statements can therefore be a very very bad thing, and it can be something that you "get away with" until you add connection pooling.

===================================================================================
But when connection is obtained from a datasource or connection pool and the connection is returned to the pool ( not closed ), what happens to the opened Statement objects? Will they be closed or become eligible for garbage collection?

===================================================================================
If the Connection maintains a reference to the Statement, then nothing, they just keep accumulating until either your Java VM runs out of memory and crashes, your database runs out of some resource and does something (such as crash) that terminates your connections, or until something closes the Connection.

In a pooled environment, not closing your Statements is generally a pretty bad thing.

原文地址:http://forum.java.sun.com/thread.jspa?threadID=751624&messageID=4296236

抱歉!评论已关闭.