原因:java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.

Posted

技术标签:

【中文标题】原因:java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.ada【英文标题】:Caused by: java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.ada 【发布时间】:2013-04-02 12:19:44 【问题描述】:

我有一个带有 Jboss、Seam、Hibernate 和 h2 的应用程序。我编写了一个简单的操作,用于从外部文件将数据导入数据库。

@Name("importAction")
@AutoCreate
@Scope(ScopeType.CONVERSATION)
@Transactional
public class ImportCosAction extends AbstractAction 

saveOrUpdate(member);

protected void saveOrUpdate(AbstractEntity entity) 
    final Session session = getSession();
        session.saveOrUpdate(entity);
    flushSession();

它按预期的数字工作,但是,在某些时候我得到以下异常:

16:50:13,558 ERROR [JDBCExceptionReporter] Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5@3c4b0529
16:50:13,558 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
        at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
        at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
        at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
        at org.jboss.seam.persistence.HibernateSessionProxy.flush(HibernateSessionProxy.java:187)
        at com.capecapital.dealmarket.usecase.AbstractAction.flushSession(AbstractAction.java:434)
        at com.capecapital.dealmarket.usecase.registration.AbstractRegistrationAction.createNewAccount(AbstractRegistrationAction.java:251)
        at com.capecapital.dealmarket.usecase.registration.RegistrationAction.primRegister(RegistrationAction.java:71)
        at com.capecapital.dealmarket.usecase.registration.AbstractRegistrationAction.register(AbstractRegistrationAction.java:146)
        at com.capecapital.dealmarket.usecase.importCoS.ImportCosAction.register(ImportCosAction.java:401)
        at com.capecapital.dealmarket.usecase.importCoS.ImportCosAction.execute(ImportCosAction.java:154)
        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.jboss.seam.util.Reflections.invoke(Reflections.java:22)
        at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
        at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97)
        at org.jboss.seam.util.Work.workInTransaction(Work.java:47)
        at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
        at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
        at com.capecapital.dealmarket.usecase.importCoS.ImportCosAction_$$_javassist_seam_18.execute(ImportCosAction_$$_javassist_seam_18.java)
        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.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:280)
        at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59)
        at org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:65)
        at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
        at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
        at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
        at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:387)
        at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
        at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
        at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
        at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
        at org.openfaces.ajax.richfaces.A4JAjaxViewRoot.access$301(A4JAjaxViewRoot.java:28)
        at org.openfaces.ajax.richfaces.A4JAjaxViewRoot$1.parentProcessApplication(A4JAjaxViewRoot.java:51)
        at org.openfaces.ajax.CommonAjaxViewRoot.processApplication(CommonAjaxViewRoot.java:197)
        at org.openfaces.ajax.richfaces.A4JAjaxViewRoot.processApplication(A4JAjaxViewRoot.java:112)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openfaces.util.ResourceFilter.doFilter(ResourceFilter.java:99)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
        at org.jboss.seam.web.RewriteFilter.doFilter(RewriteFilter.java:63)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at com.capecapital.dealmarket.filter.UserAgentFilter.doFilter(UserAgentFilter.java:46)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at com.capecapital.dealmarket.filter.DealmarketExceptionFilter.doFilter(DealmarketExceptionFilter.java:72)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:42)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at com.capecapital.dealmarket.filter.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:90)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
        at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5@3c4b0529
        at org.jboss.resource.adapter.jdbc.WrappedConnection.lock(WrappedConnection.java:81)
        at org.jboss.resource.adapter.jdbc.WrappedStatement.lock(WrappedStatement.java:64)
        at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:767)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
        ... 117 more

【问题讨论】:

【参考方案1】:

此错误表示您的代码正在使用不再有效的连接(即,由于连接错误或应用程序的无效连接缓存而关闭/破坏了连接,其中缓存的连接被上游的其他代码关闭)。在您的情况下,池中的连接 ID 是@3c4b0529。因此,您可以在 TRACE 级别的日志记录中参考此内容,以查看是否可以找出连接关闭的原因(由于一些错误,例如 DB 终止连接或网络问题或应用程序代码错误地处理连接)。

    应该优化您的代码以缩短交易完成时间。另一种选择是增加deploy/transaction-jboss-beans.xmlTransactionManager bean 的事务超时时间:-

    …… 300 ....

    默认值为 5 分钟(300 秒)。增加此事务超时可能会有所帮助,但这实际上并不能解决根本原因,即您的应用程序中长时间运行的事务。增加事务超时的副作用是您将资源保留更长时间,因此它会对 JBoss 中其他服务的性能产生负面影响。

我希望您不打算在生产中使用h2h2 不适用于持久性的生产用途。

    您还应该检查您的数据源设置,以确保您的 idle-timeout-minutes 未被禁用(设置为 0)。默认值为 15 分钟,如果您的连接在事务之间的空闲时间超过该时间,您可以增加它。

    最后,请确保您没有缓存任何数据源连接对象或语句,因为这些对象或语句可能会保持过时的连接或语句太久。

【讨论】:

感谢您的回复。不幸的是,增加超时并没有解决问题.. 您是否真的看到交易在任何地方超时?我讨论了 3 种可能的解决方案。根据您编码和配置应用程序的方式,解决方案会有所不同。 不,我没有看到它超时,但是当我减少记录数以导入时,一切似乎都正常。 这告诉我您的事务正在超时,或者您可能正在代码中缓存您的连接或准备好的语句对象,而不是从 JBoss 的连接池中获取它。 请参阅***.com/a/43379662/274677 了解与连接超时无关的另一种可能解释。显然,所有类型的错误(不仅仅是超时)都可能使您的连接对象处于这样一种状态,即在随后尝试对该连接执行某些操作时可能会遇到此异常(很可能是异常处理代码本身的一部分,就像这种情况一样在链接的答案中)。【参考方案2】:

最近遇到同样的问题,调试后发现我在执行语句之前关闭了连接,请确保您的连接仍然是 OPEN 和 VALID。

【讨论】:

【参考方案3】:

我也遇到了同样的问题。我的问题是我尝试在只执行读取操作的连接上调用提交或回滚。

【讨论】:

以上是关于原因:java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.的主要内容,如果未能解决你的问题,请参考以下文章

原因:java.sql.SQLException:无法删除表“投票”上的外键约束“FK336ctjyksuuwnpmffcogcdyet”引用的表“链接”

原因:java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.

Oracle DB:java.sql.SQLException:关闭连接

解读可能导致此报错的原因 Error updating database. Cause: java.sql.SQLException: Data truncated for column '

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 发生错误

java.sql.SQLException: 无效的列索引