在Java中关闭数据库连接 [英] Closing Database Connections in Java

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

问题描述

我有点困惑,我从 http://en.wikipedia.org阅读以下内容/ wiki / Java_Database_Connectivity

 连接conn = DriverManager.getConnection(
jdbc:somejdbcvendor:other data需要一些jdbc供应商,
myLogin,
myPassword);

语句stmt = conn.createStatement();
try {
stmt.executeUpdate(INSERT INTO MyTable(name)VALUES('my name'));
} finally {
//当你完成后关闭语句很重要
stmt.close();
}

您不需要关闭conn连接?
如果conn.close()没有发生,真正发生了什么?



我有一个私人网络应用程序,我目前没有关闭任何形式,但是重要的一个真正的stmt一个,conn一个,或两者?



网站会间歇性地关闭,但服务器一直说这是一个数据库连接问题,我怀疑它没有关闭,但我不知道如果有

当您完成使用连接后,您需要通过调用其 close()方法显式关闭它,以释放连接可能持有的任何其他数据库资源(游标,句柄等)。



实际上,Java中的安全模式是关闭您的 ResultSet 当你完成后,在 finally 块中的 Connection 类似:

 连接conn = null; 
PreparedStatement ps = null;
ResultSet rs = null;

try {
// Do stuff
...

} catch(SQLException ex){
//异常处理b $ b ...
} 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 * /}
}
}


b $ b

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

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

,这是非常详细所以你通常最终使用一个帮助类关闭对象在安全的安全助手方法和 finally 块变成这样:

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

实际上, Apache Commons DbUtils 有一个 DbUtils 这是正确的做法,所以没有必要自己写。


I am getting a little confused, I was reading the below from http://en.wikipedia.org/wiki/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();
}

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

I have a private web app 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.

解决方案

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.

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 that:

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 */}
    }
}

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 */ }
}

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 that:

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

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

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

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