• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Exception

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a question.
I have a stateless session bean.Is it good to catch Exception in my bean method and throw my own exception from the catch block.
Will the container be able to roolback the transaction automatically in this case?Or is there any drawback in handling exception like this.
Below is the code snippet.
public void getData() throws AppException{
try{
//call the method
}
catch(DataInvalidException){
//throw as ApplicationException
}
catch(Exception ex){
throw new AppException(ex);

}

}

Thanks in advance,
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most likely yes, but it could vary with the container. I recently experienced a container that didn't rollback on a FinderException, and caused inconsistent data when several entities were removed within the same transaction.

So, I would recommend something like this to ensure rollback:

 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,


I have a stateless session bean.Is it good to catch Exception in my bean method and throw my own exception from the catch block.
Will the container be able to roolback the transaction automatically in this case?Or is there any drawback in handling exception like this.


The container will never rollback the transaction automatically when application exceptions occur (it will give the client a chance to recover/retry first). The container will always rollback the current transaction if a RuntimeException occurs. Throwing RuntimeException would be one of the two solutions you have if you want the container to rollback the transaction for you. However it might not be the best choice though since business methods should rather throw application exception. As Adne pointed out the second solution you have is to catch the specific exception, call the setRollbackOnly() and re-throw an application exception.
Regards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic