• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stateful session bean with Bean Managed Transaction

 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic