JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Random Connection Closed Exceptions from connection pool
KH

跳的好看嗎 ?



發文: 135
積分: 0
於 2004-03-26 23:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請教各位先進,近日於Tomcat 5 網站看到此訊息, 好似過去我們計畫曾經發生之問題,但弟體會不出他的建議方式, 不知有人可解釋否? 謝謝.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  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;
    }
  }


KH edited on 2004-03-26 23:37
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
3259 Random Connection Closed Exceptions from connection pool KH 2170 2004-03-26 23:01
1972 Re:Random Connection Closed Exceptions from connection pool browser 295 2004-03-26 23:23
2168 Re:Random Connection Closed Exceptions from connection pool KH 13 2004-03-26 23:38
1981 Re:Random Connection Closed Exceptions from connection pool koji 344 2004-03-26 23:43
2228 Re:Random Connection Closed Exceptions from connection pool KH 805 2004-03-27 09:41
1950 Re:Random Connection Closed Exceptions from connection pool koji 1312 2004-03-27 12:10
2087 Re:Random Connection Closed Exceptions from connection pool KH 31 2004-03-27 20:26
2047 Re:Random Connection Closed Exceptions from connection pool joeyli 239 2004-07-07 00:47
» JWorld@TW »  JDBC/SQL討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8