本文介绍了关闭 Java 中的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点糊涂了.我正在从 Java 数据库连接阅读以下内容:

I am getting a little confused. I was reading the below from Java Database Connectivity:

Connection conn = DriverManager.getConnection(
     "jdbc:somejdbcvendor:other data needed by some jdbc vendor",
     "myLogin",
     "myPassword" );

Statement stmt = conn.createStatement();
try {
    stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
} finally {
    // It's important to close the statement when you are done with it
    stmt.close();
}

不需要关闭conn连接吗?如果 conn.close() 不发生,真正会发生什么?

Do you not need to close the conn connection?What is really happening if the conn.close() doesn't occur?

我有一个我正在维护的私人 Web 应用程序,它目前不会关闭任何一种表单,但真正重要的是 stmt 一个,conn 一个,还是两者都有?

I have a private web application I'm maintaining that doesn't currently close either form, but is the important one really the stmt one, the conn one, or both?

该站点断断续续地关闭,但服务器一直说这是数据库连接问题.我怀疑它没有被关闭,但我不知道要关闭哪个(如果有的话).

The site keeps going down intermittently, but the server keeps saying it's a database connection issue. My suspicion is that it's not being closed, but I don't know which, if any, to close.

推荐答案

当您使用完Connection 后,您需要通过调用其close() 来明确关闭它code> 方法以释放连接可能保留的任何其他数据库资源(游标、句柄等).

When you are done with using your Connection, you need to explicitly close it by calling its close() method in order to release any other database resources (cursors, handles, etc.) the connection may be holding on to.

实际上,Java 中的安全模式是将 ResultSetStatementConnection(按此顺序)关闭在 finally 完成后阻止.像这样:

Actually, the safe pattern in Java is to close your ResultSet, Statement, and Connection (in that order) in a finally block when you are done with them. Something like this:

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
    // Do stuff
    ...

} catch (SQLException ex) {
    // Exception handling stuff
    ...
} finally {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) { /* Ignored */}
    }
    if (ps != null) {
        try {
            ps.close();
        } catch (SQLException e) { /* Ignored */}
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) { /* Ignored */}
    }
}

finally 块可以稍微改进为(避免空检查):

The finally block can be slightly improved into (to avoid the null check):

} finally {
    try { rs.close(); } catch (Exception e) { /* Ignored */ }
    try { ps.close(); } catch (Exception e) { /* Ignored */ }
    try { conn.close(); } catch (Exception e) { /* Ignored */ }
}

但是,这仍然非常冗长,因此您通常最终使用帮助类来关闭空安全帮助方法中的对象,并且 finally 块变成这样:

But, still, this is extremely verbose so you generally end up using an helper class to close the objects in null-safe helper methods and the finally block becomes something like this:

} finally {
    DbUtils.closeQuietly(rs);
    DbUtils.closeQuietly(ps);
    DbUtils.closeQuietly(conn);
}

而且,实际上,Apache Commons DbUtils 有一个 DbUtils 类正是这样做的,所以有不需要自己写.

And, actually, the Apache Commons DbUtils has a DbUtils class which is precisely doing that, so there isn't any need to write your own.

这篇关于关闭 Java 中的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:28