Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stateful session bean with Bean Managed Transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Stateful session bean with Bean Managed Transaction" Watch "Stateful session bean with Bean Managed Transaction" New topic
Author

Stateful session bean with Bean Managed Transaction

Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264

A stateful session bean instance may, but is not required to, commit a started transaction before a business method returns. If a transaction has not been completed by the end of a business method, the container retains the association between the transaction and the instance across multiple client calls until the instance eventually completes the transaction.


so what happens, if we start a new transaction(by invoking userTransaction.begin()) in the subsequent method. will the old one be discarded.


when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

I guess that if you begin a new transaction T2 in another method, then the transaction T1 from the calling method will be suspended until you commit transaction T2.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
No. You'll get an IllegalStateException.
You have to end a transaction before you start a new one. EJB 2.0 doesn't support nested transactions.


SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Originally posted by warren li:
No. You'll get an IllegalStateException.
You have to end a transaction before you start a new one. EJB 2.0 doesn't support nested transactions.


It depends. If you use the container to specify the way your beans behave regarding transaction, for instance RequiresNew for a method bar():
If the calling method foo() is running in a transaction T1 and calls bar(), then a new transaction T2 is started and that transaction T1 will be suspended until the method bar() is completed.

EJB 2.0 does not support nested transactions but that does not mean you have to commit or rollback a transaction in order to start a new one. You can "suspend" it.
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
mates, this is about Bean managed transaction.

Assume i hava a stateful session bean.

i have started a transaction by issuing userTransaction.begin() inside method A and it returns without commiting it.And the client now calls the method B which is again invoking the userTransaction.begin().

what will happen?.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Okay fellas well I tested this finally - for Stateless Session Bean.
Here is the code for it:




And for class Bean2


Interfaces defined are remote.
So what emerged from the above testing is as following:

1.From a BMT you cannot call a CMT Bean.
2. But from one BMT (Tx1) you can call another BMT (Tx2)
Bean1 method1 calls Bean2 method2
3. But you cannot have one instance being involved in more than one Tx.
Bean1 method1 calling Bean1 method3 which starts its own Tx. In this case
Client gets the exception:

javax.ejb.EJBException: Application error: BMT stateless bean Bean1 should complete transactions before returning (ejb1.1 spec, 11.6.1); CausedByException is:
Transaction already active, cannot nest transactions.

Actual Exception is: Caused by: javax.transaction.NotSupportedException: Transaction already active, cannot nest transactions.


I think that clears it up now...
Only now need to test the following cases:

1. If Bean2 does not do commit then?
2. If Bean2 calls CMT which is of a stateful session bean that does not do commit immediately then?

Any more guidance on this?
Regards,
Shivani
[ June 25, 2007: Message edited by: Shivani Chandna ]

/** Code speaks louder than words */
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Okay here is the remaining answer....


1. If Bean2 does not do commit then?
2. If Bean2 calls CMT which is of a stateful session bean that does not do commit immediately then?


If Bean2 is defined as stateless then it is mandatory for it to commit or rollback before returning back to Bean1 method call.

But if Bean2 is defined as stateful then it is permitted to allow the transaction to remain open. So lets see the code below:





Assume i hava a stateful session bean.
i have started a transaction by issuing userTransaction.begin() inside method A and it returns without commiting it.And the client now calls the method B which is again invoking the userTransaction.begin().
what will happen?.


If you invoke it on the SAME instance � the method1 (starting Tx1) and method2 (starting Tx2) then you get the Exception (Both for stateful and stateless)
javax.transaction.NotSupportedException: Transaction already active, cannot nest transactions.


Hmm.... any other scenarios?...
I am still unsure abt CMT calling stateful BMT's.

Regards,
Shivani
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Shivani Chandna : well, i am afraid the behaviour is different whether you use ejb 2.0 or ejb 3.0 (in your case).

Has anyone tried that same code, but adapted to EJB 2.0 ?
It would be interesting to know, i will try later today or this week when i have some time off.
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
i reckon the behavior of transactions would not be different if you use a different versions.

rest i will confirm once i find it myself.
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

http://www.coderanch.com/t/162522/java-EJB-SCBCD/certification/Nested-Transactions-supported-EJB

EJB 3.0 as well as EJB 2.0 do not support nested transaction.
EJB 3.0 core spec section 13.1.2.

And to illustrate what I wrote earlier (that applies to CMP beans only of course) :

For clarification, an EJB that
declares its transactional behavior with RequiresNew called within another transaction
is not an example of nested transactions. The containing transaction is suspended
while the new one executes, and resumes when the new transaction
completes. The �inside� transaction does not affect the first transaction. For EJBs,
transactions are either suspended or propagated.


so, to resume it all:
1)in the case of BMP beans, if you open a new transaction T2 while transaction T1 is not commited/rollbacked then you get an illegalStateException (nested transactions not allowed)
2) in the case of CMP beans, if you open a new transaction T2 while transaction T1 is not commited/rollbacked then it depends on what you have put in the DD regarding the transaction attribute.

ok ?
[ June 26, 2007: Message edited by: Celinio Fernandes ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful session bean with Bean Managed Transaction
 
Similar Threads
Regarding Tx and Security Context for Interceptor method
non-trans method question;
Long Locking Transaction's
Wrong Enthuware Answer
Enthuware questions