Fitzwilliam的专栏

Losers always whine about their best; Winners go home and fuck the prom queen.

示例代码:如何正确close从连接池中取得的db连接
Here is an example of properly written code to use a db connection obtained from a connection pool:
  Connection conn = null;
  Statement stmt = null;  // Or PreparedStatement if needed
  ResultSet rs = null;
  try {
    conn = ... get connection from connection pool ...
    stmt = conn.createStatement("select ...");
    rs = stmt.executeQuery();
    ... iterate through the result set ...
    rs.close();
    rs = null;
    stmt.close();
    stmt = null;
    conn.close(); // Return to connection pool
    conn = null;  // Make sure we don't close it twice
  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
    // Always make sure result sets and statements are closed,
    // and the connection is returned to the pool
    if (rs != null) {
      try { rs.close(); } catch (SQLException e) { ; }
      rs = null;
    }
    if (stmt != null) {
      try { stmt.close(); } catch (SQLException e) { ; }
      stmt = null;
    }
    if (conn != null) {
      try { conn.close(); } catch (SQLException e) { ; }
      conn = null;
    }
  }
 
摘自:

The Apache Jakarta Tomcat 5 Servlet/JSP Container

JNDI Datasource HOW-TO

阅读更多
文章标签: null apache tomcat
个人分类: 书摘
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

示例代码:如何正确close从连接池中取得的db连接

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭