今天线上系统碰到个怪问题DBCP数据连接异常,java.sql.SQLException: Already closed.

本文分析了一个关于Java SQL的AlreadyClosed异常的问题,详细介绍了异常产生的原因及堆栈跟踪,并提出了配置连接池参数testOnBorrow和validationQuery来预防连接关闭异常的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:84) ~[commons-dbcp-1.2.2.jar:1.2.2]
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:181) ~[commons-dbcp-1.2.2.jar:1.2.2]
at sun.reflect.GeneratedMethodAccessor25.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.tinygroup.dynamicdatasource.DynamicDataSource$ConnectionInvocationHandler.invoke(DynamicDataSource.java:227) ~[org.tinygroup.dynamicdatasource-2.0.0.jar:na]
at com.sun.proxy.$Proxy21.close(Unknown Source) ~[na:na]
at org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:313) ~[spring-jdbc-2.5.6.jar:2.5.6]
at org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(DataSourceUtils.java:274) ~[spring-jdbc-2.5.6.jar:2.5.6]
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:237) [spring-jdbc-2.5.6.jar:2.5.6]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374) [spring-tx-2.5.6.jar:2.5.6]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263) [spring-tx-2.5.6.jar:2.5.6]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101) [spring-tx-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) [spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) [spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) [spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-2.5.6.jar:2.5.6]
at com.sun.proxy.$Proxy16.addCreditSerial(Unknown Source) [na:na]
at com.hsyr.cloud.p2p.loan.svr.credit.PLCreditService.addCreditSerialForUpdate(PLCreditService.java:323) [com.hsyr.cloud.p2p.loan.service-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.svr.credit.PLCreditService.updateCreditRemain(PLCreditService.java:291) [com.hsyr.cloud.p2p.loan.service-1.0.0-SNAPSHOT.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216) [org-exp-redis-1.0.0.jar:na]
at org.tinygroup.service.ServiceProxy.execute(ServiceProxy.java:150) [org.tinygroup.service-2.0.0.jar:na]
at org.tinygroup.service.impl.ServiceProviderImpl.execute(ServiceProviderImpl.java:85) [org.tinygroup.service-2.0.0.jar:na]
at org.tinygroup.serviceprocessor.ServiceEventProcessorImpl.process(ServiceEventProcessorImpl.java:47) [org.tinygroup.serviceprocessor-2.0.0.jar:na]
at org.tinygroup.cepcoreimpl.CEPCoreImpl.deal(CEPCoreImpl.java:279) [org.tinygroup.cepcoreimpl-2.0.0.jar:na]
at org.tinygroup.cepcoreimpl.CEPCoreImpl.process(CEPCoreImpl.java:224) [org.tinygroup.cepcoreimpl-2.0.0.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.callService(PLProcessorServiceAction.java:83) [com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.retryInnerRequest(PLProcessorServiceAction.java:404) [com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.performGetResult(PLProcessorServiceAction.java:393) [com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.reallyProcess(PLProcessorServiceAction.java:185) [com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.ctl.PLTaskServiceAction.processTaskServiceAction(PLTaskServiceAction.java:57) [com.hsyr.cloud.p2p.loan.ctl-1.0.0-SNAPSHOT.jar:na]
at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.tinygroup.commons.tools.ReflectionUtils.invokeMethod(ReflectionUtils.java:190) [org.tinygroup.commons-2.0.0.jar:na]
at org.tinygroup.weblayer.mvc.HandlerExecutionChain.execute(HandlerExecutionChain.java:133) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.mvc.impl.MappingModelExecuteImpl.execute(MappingModelExecuteImpl.java:40) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.tinyprocessor.MvcTinyProcessor.reallyProcess(MvcTinyProcessor.java:74) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.AbstractTinyProcessor.process(AbstractTinyProcessor.java:68) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyProcessorManagerImpl.execute(TinyProcessorManagerImpl.java:68) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.TinyFilterHandler.tinyFilterProcessor(TinyFilterHandler.java:107) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterChain.doFilter(TinyFilterChain.java:62) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.filter.gzip.GZIPFilter.doFilter(GZIPFilter.java:75) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterChain.doFilter(TinyFilterChain.java:59) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterWrapper.filterWrapper(TinyFilterWrapper.java:79) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.TinyHttpFilter.doFilter(TinyHttpFilter.java:142) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1288) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:443) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:532) [jetty-security-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1044) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:372) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:978) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.Server.handle(Server.java:369) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:486) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:933) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:995) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644) [jetty-http-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) [jetty-http-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667) [jetty-io-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) [jetty-io-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) [jetty-util-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) [jetty-util-7.6.17.v20150415.jar:7.6.17.v20150415]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
08:55:18.279 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'exceptionHandlerManager'
08:55:18.279 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'exceptionHandlerManager'
08:55:18.279 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
08:55:18.279 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'exceptionHandlerManager' to allow for resolving potential circular references
08:55:18.282 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
08:55:18.282 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
08:55:18.283 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'exceptionHandlerManager'
08:55:18.284 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'exceptionHandlerManager'
08:55:18.284 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'exceptionHandlerManager'
08:55:18.284 [qtp116331389-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'exceptionHandlerManager'
08:55:18.289 [qtp116331389-16] ERROR c.h.c.p.l.b.u.PLProcessorServiceAction - null
org.tinygroup.service.exception.ServiceRunException: null
at org.tinygroup.service.ServiceProxy.execute(ServiceProxy.java:166) ~[org.tinygroup.service-2.0.0.jar:na]
at org.tinygroup.service.impl.ServiceProviderImpl.execute(ServiceProviderImpl.java:85) ~[org.tinygroup.service-2.0.0.jar:na]
at org.tinygroup.serviceprocessor.ServiceEventProcessorImpl.process(ServiceEventProcessorImpl.java:47) ~[org.tinygroup.serviceprocessor-2.0.0.jar:na]
at org.tinygroup.cepcoreimpl.CEPCoreImpl.deal(CEPCoreImpl.java:279) ~[org.tinygroup.cepcoreimpl-2.0.0.jar:na]
at org.tinygroup.cepcoreimpl.CEPCoreImpl.process(CEPCoreImpl.java:224) ~[org.tinygroup.cepcoreimpl-2.0.0.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.callService(PLProcessorServiceAction.java:83) ~[com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.retryInnerRequest(PLProcessorServiceAction.java:404) ~[com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.performGetResult(PLProcessorServiceAction.java:393) ~[com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.biz.util.PLProcessorServiceAction.reallyProcess(PLProcessorServiceAction.java:185) ~[com.hsyr.cloud.p2p.loan.biz-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.ctl.PLTaskServiceAction.processTaskServiceAction(PLTaskServiceAction.java:57) [com.hsyr.cloud.p2p.loan.ctl-1.0.0-SNAPSHOT.jar:na]
at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.tinygroup.commons.tools.ReflectionUtils.invokeMethod(ReflectionUtils.java:190) [org.tinygroup.commons-2.0.0.jar:na]
at org.tinygroup.weblayer.mvc.HandlerExecutionChain.execute(HandlerExecutionChain.java:133) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.mvc.impl.MappingModelExecuteImpl.execute(MappingModelExecuteImpl.java:40) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.tinyprocessor.MvcTinyProcessor.reallyProcess(MvcTinyProcessor.java:74) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.AbstractTinyProcessor.process(AbstractTinyProcessor.java:68) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyProcessorManagerImpl.execute(TinyProcessorManagerImpl.java:68) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.TinyFilterHandler.tinyFilterProcessor(TinyFilterHandler.java:107) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterChain.doFilter(TinyFilterChain.java:62) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.filter.gzip.GZIPFilter.doFilter(GZIPFilter.java:75) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterChain.doFilter(TinyFilterChain.java:59) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.impl.TinyFilterWrapper.filterWrapper(TinyFilterWrapper.java:79) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.tinygroup.weblayer.TinyHttpFilter.doFilter(TinyHttpFilter.java:142) [org.tinygroup.weblayer-2.0.0.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1288) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:443) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:532) [jetty-security-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1044) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:372) [jetty-servlet-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:978) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.Server.handle(Server.java:369) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:486) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:933) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:995) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644) [jetty-http-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) [jetty-http-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) [jetty-server-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667) [jetty-io-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) [jetty-io-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) [jetty-util-7.6.17.v20150415.jar:7.6.17.v20150415]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) [jetty-util-7.6.17.v20150415.jar:7.6.17.v20150415]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_45]
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:216) ~[org-exp-redis-1.0.0.jar:na]
at org.tinygroup.service.ServiceProxy.execute(ServiceProxy.java:150) ~[org.tinygroup.service-2.0.0.jar:na]
... 49 common frames omitted
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:238) ~[spring-jdbc-2.5.6.jar:2.5.6]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374) ~[spring-tx-2.5.6.jar:2.5.6]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263) ~[spring-tx-2.5.6.jar:2.5.6]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101) ~[spring-tx-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) ~[spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) ~[spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) ~[spring-aop-2.5.6.jar:2.5.6]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ~[spring-aop-2.5.6.jar:2.5.6]
at com.sun.proxy.$Proxy16.addCreditSerial(Unknown Source) ~[na:na]
at com.hsyr.cloud.p2p.loan.svr.credit.PLCreditService.addCreditSerialForUpdate(PLCreditService.java:323) ~[com.hsyr.cloud.p2p.loan.service-1.0.0-SNAPSHOT.jar:na]
at com.hsyr.cloud.p2p.loan.svr.credit.PLCreditService.updateCreditRemain(PLCreditService.java:291) ~[com.hsyr.cloud.p2p.loan.service-1.0.0-SNAPSHOT.jar:na]
... 55 common frames omitted
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888) ~[mysql-connector-java-5.0.5.jar:na]
at com.mysql.jdbc.Connection.getMutex(Connection.java:3714) ~[mysql-connector-java-5.0.5.jar:na]
at com.mysql.jdbc.Connection.setAutoCommit(Connection.java:5341) ~[mysql-connector-java-5.0.5.jar:na]
at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331) ~[commons-dbcp-1.2.2.jar:1.2.2]
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317) ~[commons-dbcp-1.2.2.jar:1.2.2]
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_45]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_45]
at org.tinygroup.dynamicdatasource.DynamicDataSource$ConnectionInvocationHandler.invoke(DynamicDataSource.java:227) ~[org.tinygroup.dynamicdatasource-2.0.0.jar:na]
at com.sun.proxy.$Proxy21.setAutoCommit(Unknown Source) ~[na:na]
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:221) ~[spring-jdbc-2.5.6.jar:2.5.6]
... 65 common frames omitted


有一个参数说明一下:testOnBorrow true 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个. 


<property name="testOnBorrow"> 
    <value>trues</value> 
   </property> 

这个参数设为true时,还有一个参数的设置:validationQuery:SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录 

如果testOnBorrow为true而validationQuery没有设置,或为空,或你给的不是一个select语句且没有数据返回。testOnBorrow也是白设。validationQuery这个的SQL语句一般查询dual表.常用的有SELECT 1 FROM DUAL。最也不要使用SELECT sysdate FROM DUAL.这个语句的效率没有前面说的一个好。WebLogic 就是使用的SELECT 1 FROM DUAL这个sql语句。 

<property name="testOnBorrow"> 
    <value>true</value> 
   </property> 
   <property name="validationQuery"> 
    <value>SELECT 1 FROM DUAL</value> 
</property> 

要正式系统中使用的数据源最好是用上这个testOnBorrow。这样可以保证使用的每个数据源都不会出现长时间没有使用中断后。出现Could not close JDBC Connection这种问题。 
<think>嗯,用户遇到了java.sql.SQLException: connection disabled的错误,这个问题看起来和数据连接池有关。首先,我需要回忆一下常见的数据连接问题连接池中的连接如果长时间空闲,可能会被数据库服务器断开,而连接池并不知道这个情况,导致尝试使用已经失效的连接时抛出异常。 根据用户提供的引用内容,他们使用的是Spring 3.2和Hibernate 4,并且连接池配置可能有问题。引用[2]提到尝试过多个连接池但没有找到满意的解决方案,所以可能需要调整现有的连接池配置。 首先,我应该考虑连接池的配置参数。比如,验证连接是否有效的机制,例如testOnBorrow或testOnReturn,这些参数可以在获取或归还连接时执行验证查询,确保连接仍然有效。如果用户没有设置这些,连接池可能在不知情的情况下分配了已经关闭的连接,导致错误。 另外,连接的最大存活时间(maxLifetime)也是一个关键参数。如果数据库服务器设置了连接超时,而连接池连接存活时间超过这个值,就会导致连接被服务器关闭。需要确保maxLifetime略小于数据库的超时设置。 还有空闲连接的验证,比如使用空闲连接检测(idle validation),定期检查空闲连接是否仍然有效。比如,在Tomcat JDBC连接池中,可以设置timeBetweenEvictionRunsMillis和minEvictableIdleTimeMillis来定期检查和驱逐无效连接。 此外,连接泄漏的检测也很重要。如果应用程序没有正确关闭连接,可能会导致连接泄漏,最终耗尽连接池。启用连接泄漏检测(如removeAbandonedTimeout)可以帮助回收这些连接。 用户可能需要检查他们的数据源配置,确保已经正确配置了验证查询、超时参数和泄漏检测。例如,在Tomcat JDBC连接池中,配置validationQuery为SELECT 1,并设置testOnBorrow=true。同时调整maxActive和minIdle以适应负载,避免连接过多或过少。 另外,数据库服务器的等待超时设置(如MySQL的wait_timeout)也需要检查,确保连接池的配置与之匹配,避免服务器主动关闭连接。如果数据库在远程,网络问题也可能导致连接中断,所以超时设置可能需要更短。 最后,可能需要升级连接池库到更现代的版本,比如HikariCP或Tomcat JDBC Pool,这些连接池有更好的管理和恢复机制。例如,HikariCP默认有较好的心跳机制来维持连接活性。 总结来说,解决方案需要从连接验证、超时设置、泄漏检测和连接池参数调优几个方面入手,确保连接池能够有效管理连接,及时剔除无效连接,避免使用已关闭的连接导致异常。</think>### 解决 `java.sql.SQLException: connection disabled` 的逐步方案 #### 1. **检查连接池配置** 数据连接池可能因空闲连接超时或未正确验证导致异常。需确保以下配置: - **验证查询(Validation Query)**:在获取连接前执行简单查询(如 `SELECT 1`)验证连接有效性。 - **最大存活时间(maxLifetime)**:设置略小于数据库服务器的超时时间(如 MySQL 默认 `wait_timeout=28800秒`)。 - **空闲连接检查**:定期检测并关闭无效空闲连接。 **示例配置(Tomcat JDBC Pool):** ```xml <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> <property name="url" value="jdbc:mysql://remote-server:3306/db"/> <property name="username" value="user"/> <property name="password" value="password"/> <property name="validationQuery" value="SELECT 1"/> <!-- 验证查询 --> <property name="testOnBorrow" value="true"/> <!-- 借出时验证 --> <property name="maxActive" value="20"/> <property name="maxLifetime" value="28000"/> <!-- 单位:毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 空闲检查间隔 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <!-- 最小空闲时间 --> </bean> ``` #### 2. **处理数据库服务器超时设置** 若数据库服务器主动断开空闲连接(如 MySQL 的 `wait_timeout`),需确保连接池参数与之匹配: - 修改 MySQL 超时时间(需权限): ```sql SET GLOBAL wait_timeout = 28800; -- 单位:秒 ``` - 或调整连接池的 `maxLifetime` 小于 `wait_timeout`[^2]。 #### 3. **启用连接泄漏检测** 未正确关闭连接可能导致泄漏,最终触发异常。在连接池中启用泄漏检测: ```xml <property name="removeAbandonedTimeout" value="60"/> <!-- 单位:秒 --> <property name="removeAbandoned" value="true"/> ``` #### 4. **升级连接池库** 某些旧版连接池(如 DBCP 1.x)存在稳定性问题,建议切换至更现代的库: - **HikariCP**(推荐): ```java HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://remote-server:3306/db"); config.setUsername("user"); config.setPassword("password"); config.setConnectionTestQuery("SELECT 1"); // 验证查询 HikariDataSource dataSource = new HikariDataSource(config); ``` - **Tomcat JDBC Pool**(兼容 Spring 3.x): ```xml <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> <version>9.0.0.M1</version> </dependency> ``` #### 5. **添加心跳机制** 通过定期发送查询保持连接活跃,例如使用调度任务: ```java @Scheduled(fixedRate = 300000) // 每5分钟执行一次 public void keepAlive() { try (Connection con = dataSource.getConnection(); Statement stmt = con.createStatement()) { stmt.execute("SELECT 1"); } catch (SQLException e) { // 处理异常 } } ``` ### 总结 通过 **验证连接有效性**、**调整超时参数**、**启用泄漏检测** 和 **使用可靠连接池**,可有效解决 `connection disabled` 错误。若问题持续,需检查网络稳定性或数据库服务器日志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值