Nested Transactions in EJBs

Discussions

EJB programming & troubleshooting: Nested Transactions in EJBs

  1. Nested Transactions in EJBs (6 messages)

    Hi.. I have a method1 in ejb1 - with transaction attribute set to Requires. and method2 in ejb2 - with transaction attr again set to Requires. I call method2 from method1. As both methods are tightly coupled, rollback in method2 will result in rollback of sequences in method1. Now, I want to introduce a retry mechanism in method2 such that transaction rollback in method2 should not result in rollback of operations done in method1 when I am retrying the sequences in method2. However, once retry is unsuccessful, I want to rollback both method2 and method1. Does any one has idea how it can be done? Thank you, Smita

    Threaded Messages (6)

  2. Nested Transactions in EJBs[ Go to top ]

    I'm using setrollbackonly to rollback the transaction in method2 before retrying, which results in rollback of both method1 and method2, thats what my dilemma is!
  3. Nested Transactions in EJBs[ Go to top ]

    Your present transaction setting do not constitute a nested transaction, which is not supported in Weblogic, its a single, normal transaction. If you use RequiresNew in method2, that will give you 2 transactions which you can then rollback independently of one another. Rollback method2 with setrollbackonly(). To indicate to method1 that it ought to rollback, throw an exception up the call stack.
  4. Nested Transaction in EJBs[ Go to top ]

    This can be solved using the custom built logic. You can throw an exception which is not an EJBException when you get an error for the first time and second time you call the method with a parameter identifying that its a second call and if this also fails then you throw the EJBException which will rollbak both the transactions.

    Hope this answers your question

    Anand
  5. Nested Transactions in EJBs[ Go to top ]

    Hi,

    Declare method2 with "requires new", and calls setRollbackOnly() on failure. Plus throw exception on failure. The method1 will then have the possiblity to catch the exception and retry methods2. If method2 fails (for example) three time, you can call setRollbackOnly from method1 too.

    /Tomas
  6. Nested Transactions in EJBs[ Go to top ]

    When you specify ur ejb2 as "Requires", what it happens is, it will participate in the transaction of ejb1. Hence when you roll back the transaction of ejb2, which is indirectly ur rolling back the transaction of ejb1 (Since ejb1 and ejb2 are in same transaction). Therefore for your scenario, you should always use the transaction attribute as "Requires New" in ejb2, what it happens is irrespective of the caller transaction is there or not it will always create a new transaction. If you rollback the transaction in ejb2, it will not effect the ejb1 transaction, since they both are differnt transactions. In order for retrying again in ejb2, you can call from ejb1 the business method. But i feel it is not a good design to do that, some times it may lead to unpredicted results.
  7. Nested Transactions in EJBs[ Go to top ]

    When you specify ur ejb2 as "Requires", what it happens is, it will participate in the transaction of ejb1. Hence when you roll back the transaction of ejb2, which is indirectly ur rolling back the transaction of ejb1 (Since ejb1 and ejb2 are in same transaction). Therefore for your scenario, you should always use the transaction attribute as "Requires New" in ejb2, what it happens is irrespective of the caller transaction is there or not it will always create a new transaction. If you rollback the transaction in ejb2, it will not effect the ejb1 transaction, since they both are differnt transactions. In order for retrying again in ejb2, you can call from ejb1 the business method. But i feel it is not a good design to do that, some times it may lead to unpredicted results.

    Cheers :)
    Rafi