Java,ResultSet.close(),PreparedStatement.close() - 为什么? [英] Java, ResultSet.close(), PreparedStatement.close() -- what for?
问题描述
在我的网络应用程序中,我广泛使用数据库。
In my web-application, i make extensive use of a database.
我有一个抽象的servlet,所有需要数据库连接的servlet都继承。该抽象servlet创建一个数据库连接,调用抽象方法,它必须由继承的servlet来覆盖以执行其逻辑,然后关闭连接。我不使用连接池,因为我的应用程序将有非常有限数量的用户和操作。
I have an abstract servlet, from which all the servlets that need a database connection, inherit. That abstract servlet creates a database connection, calls the abstract method which must be overriden by the inheriting servlets to do their logic, and then closes the connection. I do not use connection pooling, because my application will have a very limited number of users and operations.
我的问题是,如果我没有关闭 ResultSet
code> PreparedStatement s和语句
是我的继承servlet创建的,如果 Connection $ c $
My question is, what's the worst that can happen if i don't ever close the ResultSet
s, PreparedStatement
s and Statement
s that my inheriting servlets create, if the Connection
s that create them are always closed?
推荐答案
注意:当一个Statement对象被关闭时,它的当前ResultSet对象(如果存在)也被关闭。
Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
$ b b
所以你不必担心关闭ResultSet,只要你总是及时关闭语句。
So you don't need to worry about closing ResultSets, as long as you always close Statements in a timely manner.
Connection#close()不作出相应的保证,但它确实说:
The javadoc for Connection#close() does not make a corresponding guarantee, but it does say:
立即释放此Connection对象的数据库和JDBC资源,而不是等待它们被自动释放。
Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.
您可能合理地认为这意味着任何语句将被关闭。查看开源的jTDS驱动程序,并查看一个知名和昂贵的商业数据库的驱动程序,我可以看到他们做到了这一点。
Which you might reasonably construe as implying that any statements will be closed. Looking at the open-source jTDS driver, and peeking into the driver for a well-known and expensive commercial database, i can see that they do exactly that.
这篇关于Java,ResultSet.close(),PreparedStatement.close() - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!