关闭连接的正确方法
Proper way to close connection
我们正在项目中使用连接池。我们在我们的项目中看到,语句在连接关闭后关闭。我知道在连接池的情况下,连接关闭后,与数据库的物理连接不会关闭,而是 returns 连接到池以供重用。所以我的问题是:
如果在关闭连接后关闭语句会发生什么? Will the statements be closed properly/will closing the connection close all statements 关闭语句是 redundant/the statements are open and though the connection returns to the pool, it is not reusable because of open声明? (我们同时使用 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).
我搞不懂这个。如果连接关闭,则从它派生的 Statements
和 ResultSets
也将关闭。这是记录在案的。如果 Connection
没有关闭,那么 Statements
和 ResultSets
也不会关闭,除非你关闭它们。
您必须组织您的代码,这样您就不会依赖 ResultSets
超出 Statement
的生命周期,并且 Statement
超出 Connection
的生命周期.这非常简单。
我们正在项目中使用连接池。我们在我们的项目中看到,语句在连接关闭后关闭。我知道在连接池的情况下,连接关闭后,与数据库的物理连接不会关闭,而是 returns 连接到池以供重用。所以我的问题是:
如果在关闭连接后关闭语句会发生什么? Will the statements be closed properly/will closing the connection close all statements 关闭语句是 redundant/the statements are open and though the connection returns to the pool, it is not reusable because of open声明? (我们同时使用 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).
我搞不懂这个。如果连接关闭,则从它派生的 Statements
和 ResultSets
也将关闭。这是记录在案的。如果 Connection
没有关闭,那么 Statements
和 ResultSets
也不会关闭,除非你关闭它们。
您必须组织您的代码,这样您就不会依赖 ResultSets
超出 Statement
的生命周期,并且 Statement
超出 Connection
的生命周期.这非常简单。