views:

211

answers:

1

In Spring, I have declared a method to be transactional. I use HibernateTransactionManager. Now, I would like to throw an exception from this method, but I do not want hibernate to rollback the transaction. Is it possible to specify wich exceptions cause the rollback ?

+3  A: 

Spring's default rollback behaviour is this:

[...] the Spring Framework's transaction infrastructure code will, by default, only mark a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback.) Checked exceptions that are thrown from a transactional method will not result in the transaction being rolled back.

You can override this, though. It depends on what mechanism you've used for demarcating your transactions (e.g. annotations, XML, etc), but the gist of the exception-rollback stuff is in the spring docs here.

For example, if you use the @Transactional annotation, then you can specify the rollbackFor attribute to the annotation to specify which exceptions cause a rollback.

skaffman