关于No ManagedConnections available within configured blocki

关于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导致的,最后再确认数据库连接数是不是真的吃紧。

  如下代码仅供参考:

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

posted on

,香港虚拟主机任何业绩的质变都来自于量变的积累。

关于No ManagedConnections available within configured blocki

相关文章:

你感兴趣的文章:

标签云: