设为首页 收藏本站
查看: 863|回复: 0

[经验分享] Mysql连接过期导致dbcp连接池中连接的使用异常

[复制链接]

尚未签到

发表于 2016-10-19 10:00:41 | 显示全部楼层 |阅读模式
  异常信息:
  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的活动连接。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-288312-1-1.html 上篇帖子: 使用批处理和LOAD导入数据到MySQL数据库 下篇帖子: Mysql 关于日期的查询 查询某年某月末日 或单年单月单日
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表