2012-11-22 57 views
0

我有兩個無狀態EJB。 一個使用容器管理的事務,另一個使用bean管理。從容器管理的EJB中,我調用一個bean管理的EJB的方法。爪哇,JPA,Bean管理事務,TransactionRequiredException

public class firstEJB{ 
    public void myMethod(){ 
     ejb.longRunningMethod(); 
    } 

    @EJB 
    private secondEJB ejb; 
} 


@TransactionManagement(TransactionManagementType.BEAN) 
public class secondEJB { 
    public void longRunningMethod(){ 
     while (true) { 
      this.ut.begin(); 
      //do smth 
      this.ut.commit(); 
     } 
    } 

    @Resource 
    private UserTransaction ut; 
} 

,我得到了錯誤

Caused by: javax.persistence.TransactionRequiredException: 
Exception Description: No externally managed transaction is currently active for this thread 
at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.throwCheckTransactionFailedException(JTATransactionWrapper.java:86) 
at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.checkForTransaction(JTATransactionWrapper.java:46) 
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:1776) 
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:449) 
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742) 
at com.sun.enterprise.container.common.impl.QueryWrapper.getResultList(QueryWrapper.java:195) 
at com.sun.enterprise.container.common.impl.TypedQueryWrapper.getResultList(TypedQueryWrapper.java:129) 
at VIGS.Statistics.DataProcessor.secondEJB.longRunningMethod(secondEJB.java:34) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) 
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) 
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:42) 
at sun.reflect.GeneratedMethodAccessor69.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) 
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) 
at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) 
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) 
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) 
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) 
... 42 more 

我可以調用從容器管理EJB與Bean管理事務的EJB的方法是什麼? 如果是 - 出了什麼問題?

P.S. 我已經得到了錯誤的線34這是此行的代碼:

while ((lf = this.em.createNamedQuery("getNotProcessedLogFiles", LogFile.class).setMaxResults(0).setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList().get(0)) != null) { 

回答

0

一切都非常容易 - 在我while循環我嘗試設置鎖定模式LockModeType.PESSIMISTIC_WRITE 但我們可以做到這一點只有在交易中...