wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Remove method inside transaction for stateful bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Remove method inside transaction for stateful bean" Watch "Remove method inside transaction for stateful bean" New topic
Author

Remove method inside transaction for stateful bean

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Hi

I have a question about the Remove method in the Stateful bean in EJB3.

I know in EJB2 a client can not remove the sesison bean when it is in transaction.

But how about in EJB3?

In EJB3 remove method is another business method that is marked with Remove annotation and as per default this method runs within trasaction context,so that means when i call a method marked as Remove a new transaction will be started and when it completes the transaction ends and the bean is deleted?

So the bean instance will always be in the transaction unless the transaction attribute is overriden to run the method within the transaction and how the bean is deleted when we call the method(remove) in transaction context?


Could somebody please help to understand this concept?....what am i getting wrong in this ?


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi Siva,


From ejb core specification, 4.4.4 Restrictions for Transactions:


If a session bean instance is participating in a transaction, it is an error for a client to invoke the
remove method on the session object’s home or component interface object. The container
must detect such an attempt and throw the javax.ejb.RemoveException to the client.
The container should not mark the client’s transaction for rollback, thus allowing the client to
recover.


You can't remove a stateful session bean when the bean is participating in a transaction. Participating in a transaction not means that is on a new transaction opened in @Remove method. Stateful session beans can open a transaction in one method and close the transaction in another, it is posible only with BMT stateful beans, with CMT beans the transaction ends when the method ends and for that the bean no retain the transaction with it instance.

For example, see the next pseudo code, is a stateful session bean BMT that retain the transaction across two client methods calls




If the client do this secuences of call

refMySessionBean.method1()
refMySessionBean.remove() <--- throw a javax.ejb.RemoveException because the bean is participating on a transaction, that not is finished

This secuence must run fine:

refMySessionBean.method1()
refMySessionBean.method2()
refMySessionBean.remove()


Regards


SCJP 6 - SCWCD 5 - SCBCD 5
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks.

But that rises another question.

lets consider your pseudocode with CMT.

Since the transaction end at the end of each method and by defaut container set the transaction attribute as Required when the user calls the method marked as remove then what happens as it will put that bean instance inside the transaction?

So it means that we need to mark the remove with the transaction attibute such that it will never run in a transaction?
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi again Siva,

If the bean is using CMT there is no problem because the bean instance no is in a transaction opened before when you call the remove method. (Keep in mind that the previous pseudocode not run with CMT because is using UserTransaction interface for mark the begin and the end of a transaction, you can't use this interface in CMT, if you call ut.begin() you get a IllegalStateException).

The container throw a javax.ejb.RemoveException if you call the remove method in a bean instance that retain a transaction because it has been open before in another method and the transaction has not been finished (with commit or rollback). In CMT, when a method has finished its work the transaction is finished too, the bean instance no mantain the transaction between methods call and for that you can call to the remove method. The remove method can open a new transaction, do it work and finish correctly.

Excuse my mistakes with the english, I hope you could help
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks

I got it clear now..
Marcin Strzyz
Greenhorn

Joined: Feb 25, 2012
Posts: 5
According to EJB3.1 spec (4.6.4) you can remove a stateful session bean when the bean is participating in a transaction but only through a method annotated with @Remove.
If a session bean instance is participating in a transaction, it is an error for a client to invoke the
remove method on the session object’s home or component interface object. The container
must detect such an attempt and throw the javax.ejb.RemoveException to the client.
The container should not mark the client’s transaction for rollback, thus allowing the client to
recover. Note that this restriction only applies to the remove method on the session object's
home or component interface, not to the invocation of @Remove methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove method inside transaction for stateful bean