最近因为系统和业务重构须要,须要把线上1亿数据迁移到新库,因为业务变动,新表老表结构有变化,无法直接用dba dump的方式,须要本身写转换程序迁移。今天在调试的时候,碰到一个蛋疼的问题,就是一开始查询数据都正常,可是查询几条后日志就会报超时错误,具体日志以下:java
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致使的,最后再确认数据库链接数是否是真的吃紧。spa
以下代码仅供参考:调试
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 }