5 Replies Latest reply: Jan 6, 2009 8:57 PM by Susanne Jarl RSS

Seam UserTransaction Timout problem

Susanne Jarl Newbie

I want to have a bean where I can run batch jobs that takes more than 5 minutes. I have read a lot in this forum and jboss but I can not find a solution that works for me. I tried different timeout settings but no one works. My code is below. I added a thread sleep of 5 minutes in the code just to test.
The entityManager always times out and fails. Any help is very much appreciated.



@Name("statService")
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@TransactionTimeout(1000000000)
public class StatServiceBean implements StatServiceLocal {

     @Logger
     private Log log;

     @In
     EntityManager entityManager;

     @Resource
     UserTransaction userTransaction;

     
     @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
     @TransactionTimeout(1000000000)
     public void generateRecipeShowStat() {
               
          entityManager.joinTransaction();
          
          // Loop...
          while (condition) {
               
               Query query = entityManager.createNativeQuery(
                         "INSERT INTO bla bla");     
               
               // Save to database. Create manual transaction since the built in only works for 5 minutes.
               try {
                    userTransaction.begin();                    
                    query.executeUpdate();
                    userTransaction.commit();
               }
               catch (Exception e) {
                    try {
                         log.error("Transaction failed when updating stat. Try rolling back.", e);
                         userTransaction.rollback();                         
                    }
                    catch (Exception exp) {
                         log.error("Transaction failed roling back.", exp);
                    }                    
               }     
               
               
               try { // Test timeout 5 minutes to see that transaction does not fail
                    Thread.sleep(1000*60*5);
               }
               catch (InterruptedException e) {
                    System.out.println(e);
               }
          }
     }



Environment: JBoss 4.2.3 Seam 2.1.1.GA, EJB3 with transactionmanagement configured with


<transaction:ejb-transaction  />


in components.xml

  • 1. Re: Seam UserTransaction Timout problem
    Susanne Jarl Newbie

    Doesn't anyone have any idea? I hope you understand what I'm looking for, or else please ask me. I would like to be able to run batch jobs that updates the database which take more than 5 minutes. I have read about how to change the timeout and as you can see in the code I added


    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @TransactionTimeout(1000000000)



    both on class and method level but none of it seems to work. The timeout is still 5 minutes.


    Does it have something to do with that I inject the EntityManager which I use to run the update query?


    If there is another way of doing this please let me know. I really want to find a solution for this BatchJobBean. This must be a quite normal usecase, so there is got to be a solution to this, or?


    (And I do not want to change the transaction timeout in the jboss config file, just for this session bean.)


    Thanks in advance!

  • 2. Re: Seam UserTransaction Timout problem
    Stefano Travelli Newbie

    Since you have a bean managed transaction I think you should set the timeout before userTransaction.begin() and not with @TransactionTimeout annotation.



    userTransaction.setTransactionTimeout(100000000);
    userTransaction.begin();
    
    ...
    userTransaction.commit();



    I did this way in another JBoss application (not with Seam).


    stefano

  • 3. Re: Seam UserTransaction Timout problem
    Susanne Jarl Newbie

    Thank you for your answer!
    If do as you proposed, I get the following error message:


    It is when the query.executeUpdate() is run the exception is thrown.
    But I think this timeout setting is only for the userTransaction and I believe the entityManager uses its own seam transaction.
    So I guess the question is: How do I change the timeout of the entityManagers seam transaction?



    javax.persistence.TransactionRequiredException: Executing an update/delete query
         at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:46)
         at myapp.StatServiceBean.generateRecipeShowStat(StatServiceBean.java:187)
         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.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
         at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
         at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
         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.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
         at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:29)
         at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
         at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:31)
         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.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
         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.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
         at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.tx.BMTInterceptor.handleStateless(BMTInterceptor.java:71)
         at org.jboss.ejb3.tx.BMTInterceptor.invoke(BMTInterceptor.java:131)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
         at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
         at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
         at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:240)
         at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:210)
         at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:84)
         at $Proxy184.generateRecipeShowStat(Unknown Source)
         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:31)
         at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76)
         at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
         at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
         at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
         at org.javassist.tmp.java.lang.Object_$$_javassist_4.generateRecipeShowStat(Object_$$_javassist_4.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:329)
         at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
         at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
         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:321)
         at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
         at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
         at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
         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.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
         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 org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
         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 org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
         at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
         at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
         at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
         at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
         at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
         at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
         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:230)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
         at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
         at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
         at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
         at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
         at java.lang.Thread.run(Thread.java:619)
    
    java.lang.IllegalStateException: Could not commit transaction
         at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListener.java:626)
         at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListener.java:605)
         at org.jboss.seam.jsf.SeamPhaseListener.handleTransactionsAfterPhase(SeamPhaseListener.java:343)
         at org.jboss.seam.jsf.SeamPhaseListener.afterServletPhase(SeamPhaseListener.java:243)
         at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.java:194)
         at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:175)
         at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:114)
         at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:104)
         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.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
         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 org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
         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 org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
         at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
         at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
         at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
         at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
         at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
         at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
         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:230)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
         at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
         at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
         at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
         at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
         at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.IllegalStateException: [com.arjuna.ats.internal.jta.transaction.arjunacore.inactive] [com.arjuna.ats.internal.jta.transaction.arjunacore.inactive] The transaction is not active!
         at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.rollbackAndDisassociate(TransactionImple.java:1473)
         at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.rollback(BaseTransaction.java:163)
         at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.rollback(BaseTransactionManagerDelegate.java:126)
         at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.rollback(ServerVMClientUserTransaction.java:148)
         at org.jboss.seam.transaction.UTTransaction.rollback(UTTransaction.java:66)
         at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListener.java:622)
         ... 47 more
    


  • 4. Re: Seam UserTransaction Timout problem
    Stefano Travelli Newbie

    It could be that you have to join the entity manager inside begin/commit block.


    Something like:




    userTransaction.setTransactionTimeout(1000000);
    userTransaction.begin();
    
    entityManager.joinTransaction();
    ...
    userTransaction.commit();
    



  • 5. Re: Seam UserTransaction Timout problem
    Susanne Jarl Newbie

    Thank you so very much, it solved my problem!!! :-)


    Just to summerize in case someone else have the same problem. Here is the working code:


    @Name("statService")
    @Stateless
    @TransactionManagement(TransactionManagementType.BEAN)
    public class StatServiceBean implements StatServiceLocal {
    
         @Logger
         private Log log;
    
         @In
         EntityManager entityManager;
    
         @Resource
         UserTransaction userTransaction;
    
         public void generateRecipeShowStat() {
                             
              // Loop...
              while (condition) {
                   
                   Query query = entityManager.createNativeQuery(
                             "INSERT INTO bla bla");     
                   
                   // Save to database. Create manual transaction since the built in only works for 5 minutes.
                   try {
                                    userTransaction.setTransactionTimeout(1000000);
                        userTransaction.begin();
                                    entityManager.joinTransaction();
                        query.executeUpdate();
                        userTransaction.commit();
                   }
                   catch (Exception e) {
                        try {
                             log.error("Transaction failed when updating stat. Try rolling back.", e);
                             userTransaction.rollback();                         
                        }
                        catch (Exception exp) {
                             log.error("Transaction failed roling back.", exp);
                        }                    
                   }     
                   
                   
                   try { // Test timeout 5 minutes to see that transaction does not fail
                        Thread.sleep(1000*60*5);
                   }
                   catch (InterruptedException e) {
                        System.out.println(e);
                   }
              }
         }