druid获取连接超时异常排查

1 现象com.alibaba.druid.pool.GetConnectionTimeoutException: loopWaitCount 0, wait millis 60011        
at com.alibaba.druid.pool.DruidDataSource.pollLast(DruidDataSource.java:1068)        
at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:748)        
at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:640)        
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:624)        
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:614)        
at cn.egame.common.data.dbpool.ConnectionManager.getConnection(ConnectionManager.java:33)        
at cn.egame.common.data.BaseDao.getConnection(BaseDao.java:142)        
at cn.egame.common.data.BaseDao.getObject(BaseDao.java:462)        
at cn.egame.common.data.BaseDao.getInt(BaseDao.java:233)        
at cn.egame.core.gc.TerminalServiceDao.getTerminalIdByUACode(TerminalServiceDao.java:30)        
at java.security.AccessController.doPrivileged(Native Method)        
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)        
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)        
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)        
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)        
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)        
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)        
at java.lang.Thread.run(Thread.java:662)
根据GetConnectionTimeoutException此异常,初步怀疑:(1) mysql连接不上,排查通mysql过mysql客户端能够连接上mysql,
且mysql的连接数并未被占满。
(2)连接泄露2 分析    System.out.println("pool dump : " + pool.dump());
//pool是DruidDataSource打印信息:pool dump :{        CreateTime:"2013-06-08 19:23:38",        ActiveCount:200,        PoolingCount:0,
CreateCount:319,        DestroyCount:119,        CloseCount:201403,        ConnectCount:213737,        Connections:[        ]}
ActiveCount:当前被占用的连接数,配置文件中MaxActive=200,最大连接数设置为200。PoolingCount:闲置的连接数。结论:druid连接泄露。
3 排查pool.setRemoveAbandoned(true);//会影响性能,只有在排查连接泄露的时候,才打开    System.out.println("activeStackTrace : "+
pool.getActiveConnectionStackTrace());//pool.setRemoveAbandoned(true)设置为true的时候,才有活跃连接的堆栈信息,
不然后没有打印信息:activeStackTrace : [java.lang.Thread.getStackTrace(Thread.java:1436)com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:678)
com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:624)
com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:614)
cn.egame.common.data.dbpool.ConnectionManager.getConnection(ConnectionManager.java:35)
cn.egame.common.data.BaseDao.getConnection(BaseDao.java:142)cn.egame.common.data.BaseDao.main(BaseDao.java:33),
java.lang.Thread.getStackTrace(Thread.java:1436)com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:678)
com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:624)
com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:614)
cn.egame.common.data.dbpool.ConnectionManager.getConnection(ConnectionManager.java:35)
cn.egame.common.data.BaseDao.getConnection(BaseDao.java:142)cn.egame.common.data.BaseDao.main(BaseDao.java:34)]     
我们就可以根据活跃连接的堆栈信息,看到具体的哪个业务方法持有了连接未释放。

你可能感兴趣的:(数据库相关)