gpt4 book ai didi

java - 关闭连接的正确方法

转载 作者:行者123 更新时间:2023-12-02 11:30:44 25 4
gpt4 key购买 nike

我们在项目中使用连接池。我们在项目中看到连接关闭后语句也关闭。我知道,在连接池的情况下,连接关闭后,与数据库的物理连接不会关闭,而是返回到池中以供重用。所以我的问题是:

如果在连接关闭后关闭语句会发生什么?语句是否会正确关闭/关闭连接是否会关闭所有语句,关闭语句是多余的/语句是打开的,虽然连接返回到池中,但由于打开语句而无法重用? (我们同时使用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).

我对此一无所知。如果连接关闭,则从其派生的 StatementsResultSets 也会关闭。这是有记录的。如果 Connection 未关闭,则 StatementsResultSets 也不会关闭,除非您关闭它们。

您必须组织您的代码,以便您不会在 Statement 的生命周期之外依赖 ResultSets,也不会在超出 Statement 的生命周期之外依赖 Statement 连接。这非常简单。

关于java - 关闭连接的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com