Mysql连接过期导致dbcp连接池中连接的使用异常

异常信息:

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:


** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Broken pipe

STACKTRACE:

java.net.SocketException: Broken pipe
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
 at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
 at com.mysql.jdbc.DatabaseMetaData.getUserName(DatabaseMetaData.java:6452)
 at org.apache.commons.dbcp.DelegatingConnection.toString(DelegatingConnection.java:104)
 at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.toString(PoolingDataSource.java:344)
 at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:225)
 at $Proxy0.toString(Unknown Source)
 at java.lang.String.valueOf(String.java:2827)
 at java.lang.StringBuffer.append(StringBuffer.java:219)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:186)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:220)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:267)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:261)
 at com.hisense.hitv.microblog.dao.impl.ThirdPartyMicroBlogDaoImpl.getVisibleMicroBlog(ThirdPartyMicroBlogDaoImpl.java:20)
 at com.hisense.hitv.microblog.service.impl.MicroblogServiceImpl.getMblogList(MicroblogServiceImpl.java:63)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 at $Proxy12.getMblogList(Unknown Source)
 at com.hisense.hitv.microblog.facade.impl.MicroblogFacadeImpl.getMblogListExt(MicroblogFacadeImpl.java:182)
 at com.hisense.hitv.server.CamServiceImpl.getMblogListExt(CamServiceImpl.java:279)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 at $Proxy13.getMblogListExt(Unknown Source)
 at com.hisense.hitv.server.serverrpc.CamService$Processor$getMblogListExt.getResult(CamService.java:3193)
 at com.hisense.hitv.server.serverrpc.CamService$Processor$getMblogListExt.getResult(CamService.java:1)
 at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:32)
 at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:34)
 at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:176)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619)


** END NESTED EXCEPTION **

 

Last packet sent to the server was 0 ms ago.

 

Mysql服务器默认的“wait_timeout”是28800秒,即为8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。connections如果空闲超过8小时,Mysql将其断开,而DBCP并不知道该connection已经失效,如果这时有 Client请求connection,DBCP将该失效的Connection提供给Client,将会造成上面的异常。
        mysql配置中的wait_timeout值一定要大于等于连接池种的idle_timeout 值。否则mysql会在wait_timeout的时间后关闭连接,然而连接池还认为该连接可用,这样就会产生异常。

mysql中如何查看wait_timetout:进入mysql命令  输入命令:

mysql> show global variables like 'wait_timeout';

 

 一般不建议修改mysql默认的wait_timeout值。

 

直接在dbcp的配置中简单配置即可:

timeBetweenEvictionRunsMillis = 20000
 
//这个值要小于mysql中的 'wait_timeout' 28800   

//这个值要小于mysql中的 'wait_timeout' 28800

 
7.minEvictableIdleTimeMillis = 28000
 
  

 

 

 

timeBetweenEvictionRunsMillis毫秒秒检查一次连接池中空闲的连接,把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止

DBCP连接池说明:driverClassNameurlusernamepassword上面四个分别是驱动,连接字符串,用户名和密码。

maxActive连接池支持的最大连接数。

maxIdle连接池中最多可空闲maxIdle个连接。

minIdle连接池中最少空闲maxIdle个连接。

initialSize初始化连接数目。

maxWait连接池中连接用完时,新的请求等待时间,毫秒。

timeBetweenEvictionRunsMillis和minEvictableIdleTimeMillis一起使用,每timeBetweenEvictionRunsMillis毫秒秒检查一次连接池中空闲的连接,把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止主要把这两个参数加上就好。

minEvictableIdleTimeMillis连接池中连接可空闲的时间。

毫秒removeAbandonedtrue,false,是否清理。

removeAbandonedTimeout秒没有使用的活动连接,清理后并没有放回连接池。

removeAbandonedTimeout活动连接的最大空闲时间。

logAbandonedtrue,false,连接池收回空闲的活动连接时是否打印消息。

minEvictableIdleTimeMillis,removeAbandonedTimeout这两个参数针对的连接对象不样。

minEvictableIdleTimeMillis针对连接池中的连接对象。

removeAbandonedTimeout针对未被close的活动连接。

猜你喜欢

转载自lvmlvy.iteye.com/blog/1725354