Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why we cannot call getRollbackOnly() within a method marked Support?

 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Buddies

In the book, HF EJB, it is said that you can't call getRollbackOnly()
or setRollbackOnly() within a method marked NotSupported, Never, or Supports.

I can understand we cannot call them in the methods marked with
NotSupported and Never. However, for the methods marked with Supports, it may run in a certain transaction, why we still cannot call getRollbackOnly() in it?

Thanks a lot.
 
Ionut Bucurescu
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We cannot call set/getRollbackOnly in a method which has "Supports" transaction attribute because the method can be called outside any transaction. In that case there is nothing to rollback. For instance the same method of the bean can be called by a client in a transaction and another client can call it without any transaction. For the second client would be invalid to call setRollbackOnly. Also, as far as I know you cannot determine at runtime if the current executing method marked with "Supports" transaction attribute is called inside a transaction.

Regards,
Ionut
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ionut, Thank you for your kind explanation.

What the "cannot" means here? It means the programme cannot be compiled,
or the programme can be compiled but it will throw exception when running?
 
P Chittaranjan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steven,

From my understanding , you can call the method getRollbackOnly() but it will throw exception at run time.
You have already given the answere.
For the methods marked with Supports, it may run in a certain transaction.

If a transaction is available , it will run with out a problem.
If transaction is not there , we will get the exception.

To avoid this problem getRollbackOnly() should not be called within a method marked Supports.

Thanks,
Chittaranjan
 
Ionut Bucurescu
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be no compilation problem. set/getRollbackOnly will throw IllegalStateException at runtime if the method marked with the transaction attribute 'Supports' does not exectute inside a transaction. Otherwise it will run fine.
 
Rahul Toaikani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler only knows-about the syntax, so it can complain if you dont catch a checked exception etc., but it can no way know, the symatics/businesslogic behind the ejb's.

It knows the getRollbackOnly is a legit method in the context class,so it allows you to call and wont complain.But at runtime, J2ee engine (or container in this case) knows there is no transaction here to rollback, so throws runtime exception.(IllegalStateException).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic