This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Transaction rollback in application exception

Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume a Stateless Session bean method mA() is having transaction attribute as �RequiresNew�. A Client has called this method.

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?

Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

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

which won't commit the database changes.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic