有没有从Connection生成的Statement和ResultSet的生成方法外部进行close的方法?

发布于 2024-11-25 18:13:49 字数 185 浏览 0 评论 0原文

我是日本人。英语不熟练。对不起。 有没有从Connection生成的Statement和ResultSet的生成方法外部进行close的方法?

但是,我想在不调用 Connection#close() 的情况下 close() 它。 我从方法的外部了解 Connection 的实例。

I am Japanese. English is unskilled. I'm sorry.
Is there a method of doing close from the outside of the method of generating Statement and ResultSet generated from Connection?

However, I want to close() it these one without calling Connection#close().
I learn the instance of Connection from the outside of the method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-12-02 18:13:50

不可以。JDBC 要求您自己关闭所有这些对象(并在finally 块中以便能够处理异常)。

仅关闭连接应该关闭从其派生的所有对象,但我不会依赖于此,而且最好尽快关闭这些对象。相反的情况肯定不正确(关闭 ResultSet 不会关闭连接),并且也无法从 ResultSet 获取父连接(我认为这是您问题的要点)。

我建议不要直接使用 JDBC,而是在上面使用更友好的框架,至少像 Commons DBUtils 这样最小的框架,它负责清理这些资源。

No. JDBC requires you to close all these objects by yourself (and in finally blocks to be able to handle exceptions).

Closing just the connection should close all objects derived from it, but I would not depend on that, and it is better to close those as soon as possible anyway. The opposite is certainly not true (closing a ResultSet will not close the connection), and there is also no way to get the parent Connection from a ResultSet (which I think was the gist of your question).

I would suggest to not use JDBC directly, but a more friendly framework on top, at least something as minimal like Commons DBUtils, which takes care of cleaning up these resources.

凑诗 2024-12-02 18:13:50

当您打开任何连接或创建任何语句/结果集时,显式关闭它们。
完成 JDBC 代码后,始终尝试调用此方法

public  void closeResultSetAndStatement(final Connection connection,
            final Statement statement, final ResultSet resultSet) {
        try {
            if (null != resultSet) {
                resultSet.close();
            }
        } catch (Exception e) {
            Log.warn(
                    "Exception while resultset statement. Not Throwing Exception, Ignoring it.",
                    e);
        }

        try {
            if (null != statement) {
                statement.close();
            }
        } catch (Exception e) {
            Log.warn(
                    "Exception while closing statement. Not Throwing Exception, Ignoring it.",
                    e);
        }

        try {
                    if (null != connection) {
            connection.close();
                     } 
        } catch (Exception e) {
            Log.warn(
                    "Connection could not be closed. Not Throwing Exception, Ignoring it.",
                    e);
        }
    }

When ever you open any connection or creating any Statement/ResultSet explicitly close them.
Always try to call this method after you done with your JDBC code

public  void closeResultSetAndStatement(final Connection connection,
            final Statement statement, final ResultSet resultSet) {
        try {
            if (null != resultSet) {
                resultSet.close();
            }
        } catch (Exception e) {
            Log.warn(
                    "Exception while resultset statement. Not Throwing Exception, Ignoring it.",
                    e);
        }

        try {
            if (null != statement) {
                statement.close();
            }
        } catch (Exception e) {
            Log.warn(
                    "Exception while closing statement. Not Throwing Exception, Ignoring it.",
                    e);
        }

        try {
                    if (null != connection) {
            connection.close();
                     } 
        } catch (Exception e) {
            Log.warn(
                    "Connection could not be closed. Not Throwing Exception, Ignoring it.",
                    e);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文