Java Reference
In-Depth Information
In Spring's transaction management API, the isolation level can be specified in a
DefaultTransactionDefinition object and then passed to a transaction manager's getTransaction()
method or a transaction template's constructor.
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setIsolationLevel( TransactionDefinition.ISOLATION_REPEATABLE_READ);
4-9. Setting the Rollback Transaction Attribute
Problem
By default, only unchecked exceptions (i.e., of type RuntimeException and Error ) will cause a transaction
to roll back, while checked exceptions will not. Sometimes you may wish to break this rule and set your
own exceptions for rolling back.
Solution
The exceptions that cause a transaction to roll back or not can be specified by the rollback transaction
attribute. Any exceptions not explicitly specified in this attribute will be handled by the default rollback
rule (i.e., rolling back for unchecked exceptions and not rolling back for checked exceptions).
How It Works
A transaction's rollback rule can be defined in the @Transactional annotation via the rollbackFor and
noRollbackFor attributes. These two attributes are declared as Class[] , so you can specify more than one
exception for each attribute.
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
public class JdbcBookShop extends JdbcDaoSupport implements BookShop {
...
@Transactional(
propagation = Propagation.REQUIRES_NEW,
rollbackFor = IOException.class,
noRollbackFor = ArithmeticException.class)
public void purchase(String isbn, String username) throws Exception{
throw new ArithmeticException();
//throw new IOException();
}
}
In a Spring transaction advice, the rollback rule can be specified in the <tx:method> element. You
can separate the exceptions with commas if there's more than one exception.
 
Search WWH ::




Custom Search