JDBC:如果我失去对Connection对象的引用,连接是否会中断? [英] JDBC: Does the connection break if i lose reference to the Connection object?

查看:256
本文介绍了JDBC:如果我失去对Connection对象的引用,连接是否会中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下方法 -

If i have the following method -

public static void C()  {
    Connection con = DriverManager.getConnection();

    .... // code

    return;
}

我不打电话给 con.close(),一旦方法返回,连接会自动终止吗?

and i dont call con.close() , will the connection terminate automatically once the method returns?

推荐答案


。 ..一旦方法返回,连接会自动终止吗?

...will the connection terminate automatically once the method returns?

不,它不会。它可能会或可能不会最终关闭,但它会在很长一段时间之前完成,如果有的话。连接类的终结器可能会在连接打开时关闭连接,但是很多情况下终结器都没有运行。 必要明确地调用 con.close()

No, it won't. It may or may not eventually close, but it's going to be a long time before it does, if ever. The connection class's finalizer probably closes the connection if it's open, but there are lots of situations where finalizers aren't ever run. It's essential to call con.close() explicitly.

这是我通常的方式处理它(虽然我已经将很多这样的逻辑考虑到帮助器中,因为这样做很详细):

Here's how I usually handle it (although I've factored a lot of this logic out into helpers, since this is verbose otherwise):

public static void C()
throws SQLException
{
    Connection con = DriverManager.getConnection();
    try {
        .... // code

        // done with the connection
        con.close();
        con = null;
    }
    finally {
        if (con != null) {
            try {
                con.close();
            }
            catch (Exception e) {
                // Eat it to avoid masking any exception that
                // got us here
            }
        }
    }
}

请注意,在<$ c $中检测到未关闭的连接c> finally 子句,我关闭它但不允许任何异常这样做可能导致抛出。这是因为主逻辑正确关闭了连接,这意味着如果我在 finally 块中找到了一个打开的连接,那么就会抛出一个异常,我们就是处理它,所以我不想通过从 con.close()中抛出不同的异常来掩盖它。

Note that having detected the unclosed connection in the finally clause, I close it but don't allow any exception doing so may cause to get thrown. This is because the main logic closes the connection correctly, which means that if I've found an open connection in the finally block, an exception has already been thrown and we're handling it, so I don't want to mask that by throwing a different exception from con.close().

有了不错的助手,这会变得更短更容易写:

With decent helpers, that gets a lot shorter and easier to write:

public static void C()
throws SQLException
{
    Connection con = DriverManager.getConnection();
    try {
        .... // code

        // done with the connection
        con = JDBCHelper.close(con);      // <== This one *allows* any exception that occurs
    }
    finally {
        con = JDBCHelper.quietClose(con); // <== This one *eats* any exception that occurs
    }
}

...其中JDBCHelper(一个假设类)包含:

...where JDBCHelper (a hypothetical class) contains:

public static final Connection close(Connection con)
throws SQLException
{
    con.close();
    return null;
}

public static final Connection quietClose(Connection con)
{
    if (con != null) {
        try {
            con.close();
        }
        catch (Exception e) {
        }
    }
    return null;
}

这篇关于JDBC:如果我失去对Connection对象的引用,连接是否会中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆