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的活动连接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Hibernate,可以通过在配置文件进行以下步骤来配置DBCP连接池: 1. 添加DBCP依赖 在Hibernate项目添加DBCP依赖,例如在Maven项目可以添加以下依赖: ``` <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> ``` 2. 配置DBCP连接池 在Hibernate配置文件,通过添加以下配置来配置DBCP连接池: ``` <property name="connection.provider_class">org.hibernate.connection.DBCPConnectionProvider</property> <property name="hibernate.dbcp.initialSize">5</property> <property name="hibernate.dbcp.maxTotal">20</property> <property name="hibernate.dbcp.maxIdle">10</property> <property name="hibernate.dbcp.minIdle">5</property> <property name="hibernate.dbcp.maxWaitMillis">5000</property> <property name="hibernate.dbcp.testOnBorrow">true</property> <property name="hibernate.dbcp.validationQuery">SELECT 1 FROM DUAL</property> ``` 其,`connection.provider_class`指定了使用DBCP连接池,`hibernate.dbcp.*`属性用于配置连接池的相关参数,例如`initialSize`表示连接池初始连接数,`maxTotal`表示连接池最大连接数,`maxIdle`表示连接池最大空闲连接数,`minIdle`表示连接池最小空闲连接数,`maxWaitMillis`表示获取连接的最大等待时间,`testOnBorrow`表示在获取连接时是否测试连接的可用性,`validationQuery`表示测试连接可用性的SQL语句。 3. 配置数据库连接信息 在Hibernate配置文件,需要添加以下配置来指定数据库连接信息: ``` <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> ``` 其,`hibernate.connection.driver_class`指定了数据库驱动类,`hibernate.connection.url`指定了数据库连接URL,`hibernate.connection.username`和`hibernate.connection.password`分别指定了数据库用户名和密码。 通过以上配置,就可以在Hibernate成功配置DBCP连接池了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值