Tomcat JNDI 连接池文档 - 随机连接关闭异常

发布于 2024-08-03 18:42:48 字数 2274 浏览 2 评论 0原文

在 Tomcat 文档 这里 中找到了这个

我 不明白的是为什么他们关闭所有 JDBC 对象两次 - 一次在 try{} 块中,一次在 finally{} 块中。为什么不在finally{}子句中关闭它们一次呢?

这是相关文档:

随机连接关闭异常

These can occur when one request gets a db connection from the connection pool and closes it twice. When using a connection pool, closing the connection just returns it to the pool for reuse by another request, it doesn't close the connection. And Tomcat uses multiple threads to handle concurrent requests. Here is an example of the sequence of events which could cause this error in Tomcat:

  Request 1 running in Thread 1 gets a db connection.

  Request 1 closes the db connection.

  The JVM switches the running thread to Thread 2

  Request 2 running in Thread 2 gets a db connection
  (the same db connection just closed by Request 1).

  The JVM switches the running thread back to Thread 1

  Request 1 closes the db connection a second time in a finally block.

  The JVM switches the running thread back to Thread 2

  Request 2 Thread 2 tries to use the db connection but fails
  because Request 1 closed it.

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;
    }
  }

I found this in the Tomcat documentation here

What I don't understand is why they close all the JDBC objects twice - once in the try{} block and once in the finally{} block. Why not just close them once in the finally{} clause?

This is the relevant docs:

Random Connection Closed Exceptions

These can occur when one request gets a db connection from the connection pool and closes it twice. When using a connection pool, closing the connection just returns it to the pool for reuse by another request, it doesn't close the connection. And Tomcat uses multiple threads to handle concurrent requests. Here is an example of the sequence of events which could cause this error in Tomcat:

  Request 1 running in Thread 1 gets a db connection.

  Request 1 closes the db connection.

  The JVM switches the running thread to Thread 2

  Request 2 running in Thread 2 gets a db connection
  (the same db connection just closed by Request 1).

  The JVM switches the running thread back to Thread 1

  Request 1 closes the db connection a second time in a finally block.

  The JVM switches the running thread back to Thread 2

  Request 2 Thread 2 tries to use the db connection but fails
  because Request 1 closed it.

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;
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-08-10 18:42:48

我通常只是在finally块中关闭一次连接,但没有发现任何问题。

然而,在更复杂的用例中可能需要双重关闭。例如,如果您在 try 块中打开多个语句或多个连接,则需要在使用后立即关闭每个语句或连接。然后,您需要捕获由于finally 块中的异常而剩下的任何内容。例如,

try {
    conn = ds1.getConnection();
    ... Do something with datasource 1 ...
    conn.close();
    conn=null;

    conn = ds2.getConnection();
    ... Do something with datasource 2 ...
    conn.close();
    conn = null;
  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
    if (conn != null) {
      try { conn.close(); } catch (SQLException e) { ; }
      conn = null;
    }
  }

I normally just close my connection once in the finally block and I haven't found any issues.

However, double close may be needed in more complicated use cases. For example, if you open multiple statements or multiple connections in the try block, you want close each one right after you use it. Then, you need to catch any left-over due to exceptions in the finally block. For example,

try {
    conn = ds1.getConnection();
    ... Do something with datasource 1 ...
    conn.close();
    conn=null;

    conn = ds2.getConnection();
    ... Do something with datasource 2 ...
    conn.close();
    conn = null;
  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
    if (conn != null) {
      try { conn.close(); } catch (SQLException e) { ; }
      conn = null;
    }
  }
岛歌少女 2024-08-10 18:42:48

或者您可以使用 Spring-JDBC 并忘记所有这些复杂性(Spring-JDBC 会自动为您处理这个问题)

Or you can using Spring-JDBC and forget about all this complications (Spring-JDBC will deal with this automatically for you)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文