8 Replies Latest reply on May 22, 2006 5:35 AM by Sajid Moinuddin

    difference between userTransaction.rollback() & userTransact

    Sajid Moinuddin Newbie

      Hi all,
      I am using stateful session bean and BMT to manage my transaction. I am keeping an open transaction in one of my method. I am expecting the user to call a second method to commit/rollback the transaction. Now, If the user doesn't call the second method, I am setting the transaction timeout. I want the transaction to be discarded if the user doesn't call the second method by 5 seconds. I am expecting the same behavior as the rollback(). Now, though my transaction times out, it doesn't release the locks on db / other resources like it does in case of roll back. Now, What is the difference between timeout and rollback? How can I discard my transaction if user doesn't call the second method??(should i use timer service!!)

      regards,

        • 1. Re: difference between userTransaction.rollback() & userTran
          Mark Little Master

          Assuming your datasource was registered with a transaction, then calling rollback should work as you expect. Are you using JBossTS or the old transaction service (the one that ships with JBoss3/4 by default)?

          • 2. Re: difference between userTransaction.rollback() & userTran
            Sajid Moinuddin Newbie

            Hi mark,
            here is my code.

            methodA(){
             UserTransaction ut = sessionContext.getUserTransaction();
             ut.setTimeout(5);
             ut.begin();
            
            //do some work here
            
            }
            
            methodB(){
             UserTransaction ut = sessionContext.getUserTransaction();
            
             //do some work here
            
            
             ut.commit(); / ut.rollback();
            }
            

            In the stateful session bean, in my method A , the transaction begins & remains open. In method B, the transaction is supposed to rollback/commit. I am keeping an open transaction across methods. That's not a good idea, I know. But I have to do this.
            I am expecting if the user doesn't call method B & commits/rollbacks, withing 5 seconds, the transaction be rolled back. But the transaction is not rolling back & holds the lock on database & other resource if user doesn't call method B. In my jmx console, I can see that the transaction is alive long after 5 seconds.

            I am using Jboss4.0.3spi & mysql. I am using the default tx manager shipped with jboss. My database Isolation level is read commited.
            Is this some bug of jboss?? Or my concept is wrong??
            regards,
            Sajid

            • 3. Re: difference between userTransaction.rollback() & userTran
              Mark Little Master

              In general you shouldn't span your transactions across multiple method invocations like you have in the example. For a start, in some cases it may be a different thread that invokes A and B, which would mean that you could potentially be committing an entirely different transaction in B than the one you expect. If you really need to do this, scope the transaction outside of the methods and invoke them within the scope of that transaction. For example:

              main
              {
              UserTransaction ut = ...;

              ut.begin();

              methodA();
              methodB();

              ut.commit(); // ut.rollback();
              }

              • 4. Re: difference between userTransaction.rollback() & userTran
                Sajid Moinuddin Newbie

                Thnx mark,
                But whats the problem if the method calls of Stateful bean are from different thread. Does the Stateful Bean have any dependency on the calling thread ??! In the ejb spec, i could not find any thing that says the methods of stateful session bean has to be called from same thread. If that were the case, How can I implement the shopping cart ! The add Item will always be from different thread. Am I missing something here?
                Regards,
                Sajid

                • 5. Re: difference between userTransaction.rollback() & userTran
                  Sajid Moinuddin Newbie

                   


                  For a start, in some cases it may be a different thread that invokes A and B, which would mean that you could potentially be committing an entirely different transaction in B than the one you expect


                  Again, how come I commit an entirely different transaction in B ?? I am calling method on the same stateful session bean. isn't the getSessionContext().getUserTransaction() always supposed to give me the same transaction for a particular session bean instance?? I mean, can the userTransaction I get in A and userTransaction in B vary if I call from different thread? In that case , Should I keep the user transaction as a member variable in my session bean when I started the transaction? Please let me know if I am missing something or have got a very wrong concept....

                  regards,
                  Sajid

                  • 6. Re: difference between userTransaction.rollback() & userTran
                    Sajid Moinuddin Newbie

                    mark,
                    I did check the spec again and tested my code from different thread( Calling A and B from different thread sequentially) and they works fine...But I guess you have something very important to say...please let me know if I got the whole concept of stateful session bean wrong....

                    regards,
                    Sajid

                    • 7. Re: difference between userTransaction.rollback() & userTran
                      Mark Little Master

                      No, I forgot that you'd said you were using stateful session beans. In that case, the transaction information is encoded in the context available to each method invocation.

                      • 8. Re: difference between userTransaction.rollback() & userTran
                        Sajid Moinuddin Newbie

                        thnx mark....but u really got me confused :) . I designed a complex transactional service based on this strategy. However, I can't keep open tx in stateless session beans. Thats a complete violation of spec. thnx again for your help ( suggesting to switch to JbossTx in another post). That really solved my problem.
                        regards,
                        Sajid