为什么 @Transactional 对于 EAP 7.2.x 和 7.3.x 的应用程序管理的 EntityManager 会失败,而 7.1.x 可以完美运行?
Why does @Transactional fail for an application-managed EntityManager with EAP 7.2.x and 7.3.x while 7.1.x works flawless?
下面的简单示例在 JBoss EAP 7.1.6(及之前)中可以正常工作,但在 EAP 7 中失败。2.x和最新的7.3.0.
我创建了一个简单的可重现示例并将其推送到 https://github.com/da3x/JBoss-EAP-EntityManager-Problem。我也在联系 Red Hat 支持...但有人可能已经解决了这个问题。
#save1()
和 #save2()
这两种方法用不同的 EntityManager
做同样的事情。
第一个是 application-managed,第二个是 container-managed.
使用 EAP 7.1.x 两者都正确加入事务...使用 EAP 7.2.x 和 7.3.x application-managed 刷新失败。
package eu.ecg.test;
import java.io.Serializable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
@Named
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String VIEW_ID = "/Transactional.xhtml?faces-redirect=true";
@Inject
@ApplicationManaged
private EntityManager em1;
@Inject
@ContainerManaged
private EntityManager em2;
@Transactional
public String save1() {
System.out.println("MyBean.save1()");
this.em1.flush(); // NOTE: needs Transaction!
System.out.println("OK!");
return VIEW_ID;
}
@Transactional
public String save2() {
System.out.println("MyBean.save2()");
this.em2.flush(); // NOTE: needs Transaction!
System.out.println("OK!");
return VIEW_ID;
}
}
两个EntityManager
都是由以下class产生的:
package eu.ecg.test;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceUnit;
@ApplicationScoped
public class Persistence implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceUnit(unitName = "example")
private EntityManagerFactory factory;
@PersistenceContext(unitName = "example")
private EntityManager em;
/**
* @see PersistenceContextType#EXTENDED
*/
@Produces
@ApplicationManaged
public EntityManager applicationManaged() {
System.out.println("Persistence.applicationManaged()");
return this.factory.createEntityManager();
}
/**
* @see PersistenceContextType#TRANSACTION
*/
@Produces
@ContainerManaged
public EntityManager containerManaged() {
System.out.println("Persistence.containerManaged()");
return this.em;
}
}
这个精简的示例演示了我现在遇到的核心问题。真正的应用程序要复杂得多,有超过 30 万行代码——但它表现出相同的行为。虽然它可以在 EAP 7.0.x 和 7.1.x 上正常工作,但在 7.2.x 和 7.3.0.
上会失败
我也知道我可以通过将 em.joinTransaction()
添加到 #save1()
来解决这个问题 – 但据我所知这不是必需的。 @Transactional
注释应该在这里发挥作用……不是吗?
我们正在使用应用程序管理的 EntityManager,因为有效的 PersistenceContextType#EXTENDED
允许我们通过在事务上下文之外使用托管实体来延迟加载——同时仅读取数据。
相关日志如下:
08:33:14,501 INFO [stdout] (default task-1) Persistence.applicationManaged()
08:33:14,607 INFO [stdout] (default task-1) Persistence.containerManaged()
08:33:14,639 INFO [stdout] (default task-1) MyBean.save2()
08:33:14,648 INFO [stdout] (default task-1) OK!
08:33:22,113 INFO [stdout] (default task-1) Persistence.applicationManaged()
08:33:22,113 INFO [stdout] (default task-1) Persistence.containerManaged()
08:33:22,114 INFO [stdout] (default task-1) MyBean.save1()
08:33:22,122 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-1) #{myBean.save1()}: javax.persistence.TransactionRequiredException: no transaction is in progress: javax.faces.FacesException: #{myBean.save1()}: javax.persistence.TransactionRequiredException: no transaction is in progress
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:96)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UICommand.broadcast(UICommand.java:222)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1396)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletChain.handleRequest(ServletChain.java:68)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.access0(ServletInitialHandler.java:78)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:133)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:130)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=16=](SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.access[=16=]0(ServletInitialHandler.java:78)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:99)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:830)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javax.faces.el.EvaluationException: javax.persistence.TransactionRequiredException: no transaction is in progress
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:76)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
... 57 more
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3578)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1462)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1458)
at deployment.A1E-3061.war//eu.ecg.test.MyBean.save1(MyBean.java:28)
at deployment.A1E-3061.war//eu.ecg.test.MyBean$Proxy$_$$_WeldSubclass.save1$$super(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.TerminalAroundInvokeInvocationContext.proceedInternal(TerminalAroundInvokeInvocationContext.java:51)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:78)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:174)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:53)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:88)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:73)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeAroundInvoke(InterceptorMethodHandler.java:84)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:72)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:56)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:79)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:68)
at deployment.A1E-3061.war//eu.ecg.test.MyBean$Proxy$_$$_WeldSubclass.save1(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.ELUtil.invokeMethod(ELUtil.java:245)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.BeanELResolver.invoke(BeanELResolver.java:338)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.CompositeELResolver.invoke(CompositeELResolver.java:198)
at org.glassfish.jakarta.el@3.0.2.redhat-00001//com.sun.el.parser.AstValue.invoke(AstValue.java:261)
at org.glassfish.jakarta.el@3.0.2.redhat-00001//com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:280)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
... 58 more
Red Hat 支持已打开 EAP 7.2 和 7.3 的拉取请求:
- https://issues.redhat.com/browse/JBEAP-19508 (public)
- https://issues.redhat.com/browse/JBEAP-19509 (public)
- https://access.redhat.com/solutions/5087381(红帽客户门户)
在此之前,解决方法是在 flush()
之前调用 EntityManger.joinTransaction()
。
下面的简单示例在 JBoss EAP 7.1.6(及之前)中可以正常工作,但在 EAP 7 中失败。2.x和最新的7.3.0.
我创建了一个简单的可重现示例并将其推送到 https://github.com/da3x/JBoss-EAP-EntityManager-Problem。我也在联系 Red Hat 支持...但有人可能已经解决了这个问题。
#save1()
和 #save2()
这两种方法用不同的 EntityManager
做同样的事情。
第一个是 application-managed,第二个是 container-managed.
使用 EAP 7.1.x 两者都正确加入事务...使用 EAP 7.2.x 和 7.3.x application-managed 刷新失败。
package eu.ecg.test;
import java.io.Serializable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
@Named
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String VIEW_ID = "/Transactional.xhtml?faces-redirect=true";
@Inject
@ApplicationManaged
private EntityManager em1;
@Inject
@ContainerManaged
private EntityManager em2;
@Transactional
public String save1() {
System.out.println("MyBean.save1()");
this.em1.flush(); // NOTE: needs Transaction!
System.out.println("OK!");
return VIEW_ID;
}
@Transactional
public String save2() {
System.out.println("MyBean.save2()");
this.em2.flush(); // NOTE: needs Transaction!
System.out.println("OK!");
return VIEW_ID;
}
}
两个EntityManager
都是由以下class产生的:
package eu.ecg.test;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceUnit;
@ApplicationScoped
public class Persistence implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceUnit(unitName = "example")
private EntityManagerFactory factory;
@PersistenceContext(unitName = "example")
private EntityManager em;
/**
* @see PersistenceContextType#EXTENDED
*/
@Produces
@ApplicationManaged
public EntityManager applicationManaged() {
System.out.println("Persistence.applicationManaged()");
return this.factory.createEntityManager();
}
/**
* @see PersistenceContextType#TRANSACTION
*/
@Produces
@ContainerManaged
public EntityManager containerManaged() {
System.out.println("Persistence.containerManaged()");
return this.em;
}
}
这个精简的示例演示了我现在遇到的核心问题。真正的应用程序要复杂得多,有超过 30 万行代码——但它表现出相同的行为。虽然它可以在 EAP 7.0.x 和 7.1.x 上正常工作,但在 7.2.x 和 7.3.0.
上会失败我也知道我可以通过将 em.joinTransaction()
添加到 #save1()
来解决这个问题 – 但据我所知这不是必需的。 @Transactional
注释应该在这里发挥作用……不是吗?
我们正在使用应用程序管理的 EntityManager,因为有效的 PersistenceContextType#EXTENDED
允许我们通过在事务上下文之外使用托管实体来延迟加载——同时仅读取数据。
相关日志如下:
08:33:14,501 INFO [stdout] (default task-1) Persistence.applicationManaged()
08:33:14,607 INFO [stdout] (default task-1) Persistence.containerManaged()
08:33:14,639 INFO [stdout] (default task-1) MyBean.save2()
08:33:14,648 INFO [stdout] (default task-1) OK!
08:33:22,113 INFO [stdout] (default task-1) Persistence.applicationManaged()
08:33:22,113 INFO [stdout] (default task-1) Persistence.containerManaged()
08:33:22,114 INFO [stdout] (default task-1) MyBean.save1()
08:33:22,122 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-1) #{myBean.save1()}: javax.persistence.TransactionRequiredException: no transaction is in progress: javax.faces.FacesException: #{myBean.save1()}: javax.persistence.TransactionRequiredException: no transaction is in progress
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:96)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UICommand.broadcast(UICommand.java:222)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1396)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
at javax.faces.api@3.0.0.SP01-redhat-00001//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletChain.handleRequest(ServletChain.java:68)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.access0(ServletInitialHandler.java:78)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:133)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:130)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=16=](SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at org.wildfly.extension.undertow@7.3.0.GA-redhat-00004//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=16=](UndertowDeploymentInfoService.java:1504)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.access[=16=]0(ServletInitialHandler.java:78)
at io.undertow.servlet@2.0.28.SP1-redhat-00001//io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:99)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
at io.undertow.core@2.0.28.SP1-redhat-00001//io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:830)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.3.3.Final-redhat-00001//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javax.faces.el.EvaluationException: javax.persistence.TransactionRequiredException: no transaction is in progress
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:76)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
... 57 more
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:398)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3578)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1462)
at org.hibernate@5.3.14.Final-redhat-00001//org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1458)
at deployment.A1E-3061.war//eu.ecg.test.MyBean.save1(MyBean.java:28)
at deployment.A1E-3061.war//eu.ecg.test.MyBean$Proxy$_$$_WeldSubclass.save1$$super(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.TerminalAroundInvokeInvocationContext.proceedInternal(TerminalAroundInvokeInvocationContext.java:51)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:78)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:174)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:53)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:88)
at org.jboss.jts//com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:73)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeAroundInvoke(InterceptorMethodHandler.java:84)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:72)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:56)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:79)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:68)
at deployment.A1E-3061.war//eu.ecg.test.MyBean$Proxy$_$$_WeldSubclass.save1(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.ELUtil.invokeMethod(ELUtil.java:245)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.BeanELResolver.invoke(BeanELResolver.java:338)
at javax.el.api@2.0.0.Final-redhat-00001//javax.el.CompositeELResolver.invoke(CompositeELResolver.java:198)
at org.glassfish.jakarta.el@3.0.2.redhat-00001//com.sun.el.parser.AstValue.invoke(AstValue.java:261)
at org.glassfish.jakarta.el@3.0.2.redhat-00001//com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:280)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.core@3.1.2.Final-redhat-00001//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
at com.sun.jsf-impl@2.3.9.SP05-redhat-00001//com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
... 58 more
Red Hat 支持已打开 EAP 7.2 和 7.3 的拉取请求:
- https://issues.redhat.com/browse/JBEAP-19508 (public)
- https://issues.redhat.com/browse/JBEAP-19509 (public)
- https://access.redhat.com/solutions/5087381(红帽客户门户)
在此之前,解决方法是在 flush()
之前调用 EntityManger.joinTransaction()
。