4 Replies Latest reply on Dec 23, 2008 8:07 PM by dcardon

    asynchronous invocations and transactions

    jghayward.ice.0136.gmail.com

      I wonder if anyone can please provide some assistance with an explanation of asynchronous activity and transactions.


      We are attempting to write a variety of cron functions to be executed in seam.  We are using the quartz scheduler.  Some of these crons may run for 30 minutes to an hour.  Often they need to iterate over many items in the database (perhaps 10,000 to 100,000), and for each item perform some type of transactional activity.  We're looking to perform two types of transactional flows:


      1.  When the cron executes, the entire thing is in a transaction, it all completes or it all rolls back.  The transaction timeout needs to be configurable per cron.


      2.  The cron should not be transactional, -or- the nested invocations within the cron must be nested and committed transactions, so that if an individual iteration fails, the overall cron is not rolled back, and if the overall cron fails, the individual iterations that have been completed remain committed.


      We're having numerous problems getting these scenario's to work.  Here's a few items I have questions upon.


      a.  I'd wanted to go pojo, but it seems for item 1 you can only set a transaction timeout with an EJB annotation.  It seems that for item 2 you can only perform nested transactions within EJB's.  Can any of this be done with the pojo architecture or does it all require EJB's?


      b.  If I follow the EJB path, I attempt to use     @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)  on a few EJB methods.  When doing so, I notice none of the 'save' operations take effect until the entire Event context completes and the entire transaction is committed.  It does not seem like the REQUIRES_NEW method is operating within it's own transaction.


      c.  Regardless of whether the Asynchronous method is marked with @Transactional or not... it always operates in a transaction, even if I don't want it to.


      A related question is... how can I tell when I'm operating in a transaction or not?  I've tried Transaction.instance().isActive(), but this does not seem to accurately reflect when a transaction is in effect.  For example, omitting the @Transactional annotation from the asynchronous method the isActive method returns false; yet the 'save' operations performed by the method are in fact persisted to the database, and they are not persisted if I force an exception.  I've also tried session.getTransaction()  (we use hibernate persistence), and it's isActive method returns false; while it too is clearly operating within a transaction.  Aside from watching the database itself,  what's the best way to know when a transaction is active or not, and if it's a nested or new transaction?  Is there some logging that can be enabled?


      Thanks in advance... lots to ponder here.


      -John

        • 1. Re: asynchronous invocations and transactions
          jghayward.ice.0136.gmail.com

          I should have mentioned:  Seam 2.0.2.GA, jboss 4.2.2.GA, Jboss JTA transactions, mysql XA database connections to Innodb tables, java 5 and java 6 (same issues with both).   Relevant parts of components.xml:



          <async:quartz-dispatcher />
          
          <persistence:managed-hibernate-session name="session" 
                  auto-create="true" session-factory="#{hibernateSessionFactory}" />
          
          <spring:spring-transaction  platform-transaction-manager="#{transactionManager}"  conversation-context-required="false" />


          • 2. Re: asynchronous invocations and transactions
            nynymike

            John,


            Did you ever get this resolved? I have a cron job that adds users. If 1 user throws an error, I want the cron job to keep going with the subsequent users. Bad things happen... if you have any ideas, please let me know! I've been stuck on this for two days.


            - Mike Schwartz
            ID-Vault
            http://www.id-vault.us

            • 3. Re: asynchronous invocations and transactions
              vladimir.kovalyuk

              did you manage to find any solution?

              • 4. Re: asynchronous invocations and transactions
                dcardon

                My use case is an email application that A) updates the status of a row in the DB,  B) renders and email, and C) sets the final status of the row.  I ran into several issues related to this and finally landed on a reasonable solution.


                Here's the solution that I used:



                1. I call a Method-scoped component for each of the transactional items. (A, B and C)  Each one of these items is transactional, because rendering and sending the email might throw an exception, which closes the transaction for that item.  Initially, I was trying to run A, B and C in the same method call--this failed.

                2. The asynchronous method call calls the methods on the Method-scoped component.  So, a new transaction is created for each method call in the Method-scoped component.



                Hope this helps!


                --Dave