Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB 3.0 - Nested Transaction != Requires New?

I just read the Transactions Chapter (10) of "Mastering EJB 3.0" and now I'm confused about nested transactions.

The book says

"The EJB-defined transaction manager does not support nested transactions; it requires support for only flat transactions." (Site 278, Note)

This fact is described not only by this book, I found this statement in other books / websites.

But if I call a "Requires New" annotated Method from a, let's say "Required" annotated Methode, what I have is a nested transaction, isn't it? I can roll back the inner transaction or commit it, without affecting the outer transaction. And if I want the outer Transaction to be aborted, I throw an EJBException back and the whole transaction will be rolled back.

So is it just that this behavior is not required by the EJB 3.0 specification or have i misunderstood something? I just can't get the difference between nested transactions and the described behavior.

Regards Norman

like image 973
NorRen Avatar asked May 30 '12 13:05

NorRen


People also ask

Is nested transaction supported by EJB?

The EJB specification allows for flat transactions only. Transactions cannot be nested.

What is EJB transaction?

EJB Container/Servers are transaction servers and handles transactions context propagation and distributed transactions. Transactions can be managed by the container or by custom code handling in bean's code. Container Managed Transactions − In this type, the container manages the transaction states.


2 Answers

RequiresNew does not create a nested transaction because the first transaction is suspended while the second transaction is running. A nested transaction looks like this:

Nested transaction example
> method1 - begin tran1
  > method2 - begin tran2
    workA
  < method2 - commit tran2
< method1 - rollback tran1 (tran2 also rolled back because it's nested)

Instead, RequiresNew looks like this:

EJB RequiresNew example
> method1 - begin tran1
  > method2 - suspend tran1, begin tran2
    workA
  < method2 - commit tran2, resume tran1
< method1 - rollback tran1 (tran2 remains committed)
like image 174
Brett Kail Avatar answered Oct 16 '22 19:10

Brett Kail


Simple answer is the "outer" transaction is suspended before the new transaction is started. The fates of the two transactions are not in any way linked, so by all intents and purposes one is not nested into another.

If the REQUIRES_NEW method throws an EJBException it is the new transaction it created that will be rolled back, not the "outer" transaction.

like image 20
David Blevins Avatar answered Oct 16 '22 19:10

David Blevins