關於No ManagedConnections available within configured blocking timeout異常的解決


  最近由於系統和業務重構需要,需要把線上1億數據遷移到新庫,由於業務變更,新表老表結構有變化,沒法直接用dba dump的方式,需要自己寫轉換程序遷移。今天在調試的時候,碰到一個蛋疼的問題,就是一開始查詢數據都正常,但是查詢幾條后日志就會報超時錯誤,具體日志如下:

No ManagedConnections available within configured blocking timeout ( 5000 [ms] ); - nested throwable: (javax.resource.ResourceException: No ManagedConnections available within configured blocking timeout ( 5000 [ms] ))

  搜了下這個錯誤,各種說法比較多,但是感覺都沒說到點上,找DBA看了下,DBA直接表示這個庫連接數一直吃緊,從這個錯誤看也是連接超時,本來以為就這么着了,但是調試了幾次都是一開始正常,后來報異常,就感覺肯定還是代碼有問題導致連接數吃緊,后來仔細看了下代碼,發現是connection沒有關閉...應該說是沒有關閉全。把PrepareStatement和ResultSet關閉了,但是沒把最重要的Connection關閉掉...關閉了Connection就正常了。

  網上幾個搜的結果太過於誤導人,所以就記錄一下,碰到這個錯誤,首先是確認自己代碼是否有相關connection沒關閉掉,基本都是沒關閉connection導致的,最后再確認數據庫連接數是不是真的吃緊。

  如下代碼僅供參考:

 1         try{
 2             conn = sourceDs.getConnection();
 3             ps = conn.prepareStatement(selectTcBizOrder);
 4             rs = ps.executeQuery();
 5             if(rs.next()){
 6                 result.put("auction_id", rs.getLong("auction_id"));
 7                 result.put("logistics_status", rs.getInt("logistics_status"));
 8                 result.put("attributes", rs.getString("attributes"));
 9                 return result;
10             }else{
11                 return null;
12             }
13         }catch(SQLException e){
14             LogFactory.getTaskLog().error("[select tc_biz_order SQLException], bizOrderId="+bizOrderId, e);
15             return null;
16         }catch(Exception e){
17             LogFactory.getTaskLog().error("[select tc_biz_order other Exception], bizOrderId="+bizOrderId, e);
18             return null;
19         }finally{
20             if(rs != null){
21                 try{
22                     rs.close();
23                 }catch(SQLException e){
24                     LogFactory.getTaskLog().error("[close ResultSet SQLException], bizOrderId="+bizOrderId, e);
25                 }
26             }
27             
28             if(ps != null){
29                 try {
30                     ps.close();
31                 } catch (SQLException e) {
32                     LogFactory.getTaskLog().error("[close PreparedStatement SQLException], bizOrderId="+bizOrderId, e);
33                 }
34             }
35             
36             if(conn != null){
37                 try{
38                     conn.close();
39                 }catch(SQLException e){
40                     LogFactory.getTaskLog().error("[close Connection SQLException], bizOrderId="+bizOrderId, e);
41                 }
42             }
43         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM