Closing dangling connections. All connections must have a close() in a finally block.

spring+openjpa

 

线上项目每隔3,4天会出现这个异常:

Closing dangling connections.  All connections must have a close() in a finally block.
java.lang.IllegalStateException: unclosed connection: UserPoolItem[jdbc/dataSource,4078246] was allocated at
 at com.caucho.jca.UserPoolItem.(UserPoolItem.java:95)
 at com.caucho.jca.UserPoolItem.(UserPoolItem.java:101)
 at com.caucho.jca.PoolItem.toActive(PoolItem.java:249)
 at com.caucho.jca.ConnectionPool.allocateIdle(ConnectionPool.java:854)
 at com.caucho.jca.ConnectionPool.allocatePool(ConnectionPool.java:773)
 at com.caucho.jca.ConnectionPool.allocate(ConnectionPool.java:740)
 at com.caucho.jca.ConnectionPool.allocateConnection(ConnectionPool.java:551)
 at com.caucho.sql.DataSourceImpl.getConnection(DataSourceImpl.java:65)
 at com.caucho.sql.DBPool.getConnection(DBPool.java:686)
 at org.apache.openjpa.lib.jdbc.DelegatingDataSource.getConnection(DelegatingDataSource.java:106)
 at org.apache.openjpa.lib.jdbc.DecoratingDataSource.getConnection(DecoratingDataSource.java:87)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.connectInternal(JDBCStoreManager.java:773)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.connect(JDBCStoreManager.java:758)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.retainConnection(JDBCStoreManager.java:201)
 at org.apache.openjpa.kernel.DelegatingStoreManager.retainConnection(DelegatingStoreManager.java:163)
 at org.apache.openjpa.kernel.DelegatingStoreManager.retainConnection(DelegatingStoreManager.java:163)
 at org.apache.openjpa.kernel.BrokerImpl.retainConnection(BrokerImpl.java:3700)
 at org.apache.openjpa.kernel.BrokerImpl.beginStoreManagerTransaction(BrokerImpl.java:1280)
 at org.apache.openjpa.kernel.BrokerImpl.beginStore(BrokerImpl.java:1265)
 at org.apache.openjpa.jdbc.kernel.PessimisticLockManager.ensureStoreManagerTransaction(PessimisticLockManager.java:158)
 at org.apache.openjpa.jdbc.kernel.PessimisticLockManager.selectForUpdate(PessimisticLockManager.java:80)
 at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:342)
 at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:319)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.getInitializeStateResult(JDBCStoreManager.java:411)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:307)
 at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:263)
 at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
 at org.apache.openjpa.datacache.DataCacheStoreManager.initialize(DataCacheStoreManager.java:348)
 at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
 at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
 at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:894)
 at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:852)
 at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:769)
 at org.apache.openjpa.kernel.DelegatingBroker.find(DelegatingBroker.java:183)
 at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:452)
 at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
 at $Proxy19.find(Unknown Source)
 at projects.common.comment.imp.CommentListener.synUpdate(CommentListener.java:44)
 at projects.common.comment.ICommentService.synUpdate(ICommentService.java:38)
 at projects.common.comment.ICommentService$$FastClassByCGLIB$$a2087761.invoke()
 at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
 at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:628)
 at projects.common.comment.imp.CommentService$$EnhancerByCGLIB$$1774b234.synUpdate()
 at _jsp._comment_0do__jsp._jspService(_comment_0do__jsp.java:331)
 at com.caucho.jsp.JavaPage.service(JavaPage.java:61)
 at com.caucho.jsp.Page.pageservice(Page.java:587)
 at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:192)
 at cn.pconline.common.monitor.MonitorFilter.doFilter(MonitorFilter.java:88)
 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
 at projects.common.filter.HtmlReduceFilter.doFilter(HtmlReduceFilter.java:29)
 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
 at com.caucho.filters.GzipFilter.doFilter(GzipFilter.java:155)
 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
 at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:112)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
 at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:151)
 at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:181)
 at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
 at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
 at com.caucho.server.port.TcpConnection.run(TcpConnection.java:602)
 at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:690)
 at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:612)
 at java.lang.Thread.run(Thread.java:619)
java.lang.IllegalStateException: Connection UserConnection[ManagedConnectionImpl[jdbc/dataSource.2877]] was not closed. Connections must have a close() in a finally block.
 at com.caucho.jca.UserTransactionImpl.abortTransaction(UserTransactionImpl.java:501)
 at com.caucho.jca.UserTransactionProxy.abortTransaction(UserTransactionProxy.java:179)
 at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:201)
 at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:266)
 at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
 at com.caucho.server.port.TcpConnection.run(TcpConnection.java:602)
 at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:690)
 at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:612)
 at java.lang.Thread.run(Thread.java:619)

 

同时写操作会报乐观锁异常, 而且之后乐观锁异常会频繁出现

 

重启后故障消失

 

但是会反复

 

这个异常,应该是数据库连接未关闭导致, 但连接都是由spring容器管理,按理不会出现这个问题

 

猜测可能由于一些由EntityManagerFactory创建的em没有关闭导致, 而spring只管理em和emf,不会去管理emf创建的em

 

修改了代码,观察是否还会出现同样问题

你可能感兴趣的:(Closing dangling connections. All connections must have a close() in a finally block.)