当前位置:   article > 正文

使用JDBC过程中如何正确关闭connection_closing jdbc connection

closing jdbc connection

来看一段代码:

  1. import java.sql.*;
  2. /**
  3. * Created by N3verL4nd on 2017/4/17.
  4. */
  5. public class JdbcDemo
  6. {
  7. public static void main(String[] args) {
  8. Connection conn = null;
  9. Statement stmt = null;
  10. ResultSet rs = null;
  11. String url = "jdbc:mysql://localhost:3306/weibo?"
  12. + "user=root&password=lgh123&useUnicode=true&characterEncoding=UTF8&useSSL=true";
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. try {
  19. conn = DriverManager.getConnection(url);
  20. stmt = conn.createStatement();
  21. /* System.out.println(sql); */
  22. rs = stmt.executeQuery("SELECT * FROM t_account");
  23. while (rs.next()){
  24. System.out.println(rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
  25. }
  26. rs.close();
  27. System.out.println("ResultSet closed");
  28. stmt.close();
  29. System.out.println("Statement closed");
  30. conn.close();
  31. System.out.println("Connection closed");
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }

假如rs = stmt.executeQuery("SELECT * FROM s_account");我们查找一个不存在的表,就会出现异常:

而此时,

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;都没有被正确关闭。


正确的处理方式应该是:在finally语句块中执行关闭操作。

  1. Connection conn = null;
  2. PreparedStatement ps = null;
  3. ResultSet rs = null;
  4. try {
  5. // Do stuff
  6. ...
  7. } catch (SQLException ex) {
  8. // Exception handling stuff
  9. ...
  10. } finally {
  11. if (rs != null) {
  12. try {
  13. rs.close();
  14. } catch (SQLException e) { /* ignored */}
  15. }
  16. if (ps != null) {
  17. try {
  18. ps.close();
  19. } catch (SQLException e) { /* ignored */}
  20. }
  21. if (conn != null) {
  22. try {
  23. conn.close();
  24. } catch (SQLException e) { /* ignored */}
  25. }
  26. }
当然如果可以确保不是空指针,则可以简写为:
  1. } finally {
  2. try { rs.close(); } catch (Exception e) { /* ignored */ }
  3. try { ps.close(); } catch (Exception e) { /* ignored */ }
  4. try { conn.close(); } catch (Exception e) { /* ignored */ }
  5. }
这对于我们来时还是过于冗长,我们可以使用 Apache Commons DbUtils 里的  DbUtils 

  1. } finally {
  2. DbUtils.closeQuietly(rs);
  3. DbUtils.closeQuietly(ps);
  4. DbUtils.closeQuietly(conn);
  5. }
内部实现:

public static void close(ResultSet rs) throws SQLException {
    if (rs != null) {
        rs.close();
    }
}

public static void close(Statement stmt) throws SQLException {
    if (stmt != null) {
        stmt.close();
    }
}

public static void close(Connection conn) throws SQLException {
    if (conn != null) {
        conn.close();
    }
}

public static void closeQuietly(Connection conn) {
    try {
        close(conn);
    } catch (SQLException e) {
        //e.printStackTrace();
    }
}

public static void closeQuietly(ResultSet rs) {
    try {
        close(rs);
    } catch (SQLException e) {
        //e.printStackTrace();
    }
}

public static void closeQuietly(Statement stmt) {
    try {
        close(stmt);
    } catch (SQLException e) {
        //e.printStackTrace();
    }
}

public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) {
    try {
        closeQuietly(rs);
    } finally {
        try {
            closeQuietly(stmt);
        } finally {
            closeQuietly(conn);
        }
    }
}

参考:

http://stackoverflow.com/questions/2225221/closing-database-connections-in-java

http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html

http://www.cnblogs.com/jfqiu/p/3197014.html

https://shinesolutions.com/2007/08/04/how-to-close-jdbc-resources-properly-every-time/

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号