• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove method inside transaction for stateful bean

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

I got it clear now..
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic