aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Johnson Tmmet
Greenhorn

Joined: Oct 27, 2005
Posts: 8
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,
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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:



Entia non sunt multiplicanda praeter necessitatem
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
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.


I think, therefore I exist -- Rene Descartes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception