Tuesday, September 14, 2010

Rolling back transaction (Seam managed transaction)

JSF+Seam is what I'm learning at the moment. And the case is I need to rollback the transaction due to business exception but I don't want to not catch the transaction (when a runtime exception is uncaught the transaction will be rollback automatically). The business exception here is a new exception class extending RuntimeException.

At first try, I add ApplicationException annotation with rollback = true in my BusinessException class. The controller class which invokes the business service class and under certain condition an exception of BusinessException type is thrown. In my scenario my transaction should be rolled back, the thing is... it was not rolled back.

This forced me to dive into seam's code (that's why I love open source) and find out how to handle the transaction. And I came into Transaction class, which I can use to do what I want.
Here's how to use it to mark the transaction as rollback:

Transaction.instance().rollback();

Actually all I wish is when the exception is thrown by the service class (already registered to seam) the transaction will be rolled back automatically, unfortunately for me it doesn't work :(

No comments: