Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When will I use the setRollbackOnly() method ? 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
0
0
IL
Hello,

I am a bit confused.
Can someonle please explain me when will I use the setRollbackOnly() method of an EntityContext? What is the case that I will need to state that the only possible outcome of the transaction is to roll back the transaction? And if it is rolled back why creating it at first place?

 
When you want the EJB Container to rollback the transaction (assuming Container Managed Transactions). You would normally stop whatever process you're doing and exit the method when you set this, because any further activity, say to an Entity Bean, will cause the container to complain.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
But the setRollBackOnly will do a rollback no matter what, so why create a transaction if it is marked as rolled back...?
 
If you're using CMT then the container will create the transaction for you. The setRollBackOnly() informs the container that it should rollback that transaction when the operation exits.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
But gain: a rollback cancel the transaction, why creating a transaction and then cancel it no matter what?
 
You only call setRollbackOnly if something you're doing fails ( maybe the operation cannot be completed because a resource is missing or something blew up and you caught a checked exception ). In this case, you know you can't go on, so you tell the container it can roll the transaction back.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
99% of the time, that's where it is used (in my code at least).

There's always the chance that tests done in normal code will flag the case that you cannot proceed, so you could call it there. But usually as a result of catching an exception.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top