• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction rollback in application exception

 
Satya P
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume a Stateless Session bean method mA() is having transaction attribute as �RequiresNew�. A Client has called this method.

@TransactionAttribute(REQUIRES_NEW)
public void mA() throws myApplicationException
{
//update some row in database
// perform some business logic
throw myApplicationException;
}

In mA(), Container starts a new Transaction. Within that transaction, database update is done and then business logic is executed. While performing businesslogic assume that the method encountered some error and the method planned to throw the ApplicationException.
In such cases the Container does not rollback the transaction. Does it mean that the updated row is Commited to the DB at the end of the method? Is a commit performed by the container at the end of the method?

Regards,
Satya
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it would get committed. In order to avoid the update in this scenario you have to mark the Application Exception as rollback=true since you know that you are going to throw it after a database update.

However if that application exception is not directly related to database errors then it is better that you use setRollBackOnly() in this way

try{
//do database update
//do business logic
}catch(Exception e){
ejbContext.setRollBackOnly();
throw new myApplicationException();
}

which won't commit the database changes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic