问题描述
我们在项目中使用连接池.在连接关闭后,我们在项目中看到语句已关闭.我知道在使用连接池的情况下,关闭连接后,与数据库的物理连接不会关闭,而是返回到池中以供重用.所以我的问题是:
We are using connection pool in our project. We see in our project the statements are closed after the connection is closed. I know that in case of a connection pool, after a connection is closed the physical connection to the database is not closed but returns to the pool for reuse. So my question is:
如果在关闭连接后关闭语句,会发生什么?语句是否可以正确关闭/关闭连接会关闭所有语句并关闭语句是多余的/语句是打开的,尽管连接返回到池中,但由于打开的语句而无法重用? (我们同时使用Statement和PreparedStatement.)
What will happen if statements are closed after a connection is closed? Will the statements be closed properly/will closing the connection close all the statements and closing the statements are redundant/the statements are open and though the connection returns to the pool, it is not reusable because of open statements? (We are using both Statement and PreparedStatement).
推荐答案
什么都没有.他们已经关门了.这是有据可查的.
Nothing. They were already closed. This is documented.
是的.这是有据可查的.
Yes. This is documented.
是的.这是有据可查的.
Yes. This is documented.
是的
我不能为此做头或尾.如果连接已关闭,则从其派生的Statements
和ResultSets
也将关闭.这是有据可查的.如果Connection
没有关闭,则Statements
和ResultSets
也不会关闭,除非您将它们关闭.
I cannot make head or tail of this. If the connection is closed, the Statements
and ResultSets
derived from it are also closed. This is documented. If the Connection
isn't closed, the Statements
and ResultSets
aren't closed either, unless you close them.
您必须组织代码,以免在Statement
生命周期之外不依赖ResultSets
,而在Connection
生命周期之外不依赖Statement
.这很容易.
You must organise your code so that you don't rely on ResultSets
beyond the life of the Statement
, and Statement
beyond the life of the Connection
. This is trivially easy.
这篇关于正确的连接方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!