Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Handle Transaction ?  RSS feed

 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Made Transaction to work,is it required/mandatory to throw a EJBException or is it OK to throw just an java.lang.Exception.
The container dosent rollback if we throw an java.lang.Exception.
We are using webshpere 4.0.
The sample code is attached below.'
public void transferToSaving(double amount) throws
InsufficientBalanceException {
try {
updateChecking(checkingBalance);
if (checkingBalance < 0.00) {
context.setRollbackOnly();
throw new InsufficientBalanceException();
}
updateSaving(savingBalance);
} catch (SQLException ex) {
throw new EJBException
("Transaction failed due to SQLException: "
+ ex.getMessage());
}
}
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the EJB Spec, an EJBException (and all other subclass of RuntimeException) are called "System Exceptions". The container is required to log these exceptions and rollback the transaction if an EJB throws one of these.
All checked exceptions (subclass of Exception, other than RuntimeException) are called "Application Exceptions". These appear in the "throws" clause of your component interface. When the EJB throws one of these, the container will not do anything with your transaction. The assumption is that you (or the calling application) will deal with the transcation (since you have to throw/catch the exception).
You should throw whatever kind of exception makes sense to your application. If you also need to rollback the transaction, you can call EJBContext.setRollbackOnly().
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the spec :
An exception inherited from java.lang.Exception does not rollback the transaction. It is an application exception and YOU have to deal with it. To force the rollback (wich is not always required), use :EJBContext.setRollbackOnly().
The application exception is received "as-is" by the caller
An EJBException is a system exception. When you throw this exception the CONTAINER is responsible for the rollback. Then the container rethrows a RemoteException to the caller
 
shiva shankar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the recommendad way to roll back a transactions?Throw a system exception or EntityContext.setRollBackOnly()?Which is the prefered and recommended one
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!