Spring Transaction中是否需要异常处理?

时间:2012-12-14 18:01:29

标签: java spring exception-handling transactions

我对使用Transaction进行异常处理有疑问。为了清楚说明我的问题,我想展示我的配置:

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionInterceptor" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributeSource">
        <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
    </property>
</bean>

<bean id="baseService" abstract="true">
    <property name="daoProvider" ref="daoProvider" />
</bean>

<bean id="customerService" parent="transactionInterceptor">
    <property name="target">
        <bean class="com.edfx.adb.service.CustomerService" parent="baseService" />
    </property>
</bean>

<bean id="daoProvider" class="com.edfx.adb.dao.provider.DaoProvider">   
    <property name="customerDao" ref="customerDao" />
</bean>

<bean id="customerDao" class="com.edfx.adb.dao.CustomerDao">
    <constructor-arg value="#{T(com.edfx.adb.persist.entity.Customer)}" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

活动交易类是:

@Transactional
public class CustomerService extends BaseService implements ICustomerService {

    @Transactional(readOnly = true)
    public Customer getCustomerById(String id) {
        return getDaoProvider().getCustomerDao().getCustomerById(id);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
    public void addNewCustomer(CustomerDTO customerDTO) {
        Customer customer = new Customer();

        customer.setCustomerId(customerDTO.getCustomerId());
        customer.setCustomerName(customerDTO.getCustomerName());
        customer.setActive(customerDTO.isActive());

        getDaoProvider().getCustomerDao().save(customer);
    }
}

我的疑问在于方法addNewCustomer。我已设置rollbackFor = { Throwable.class }

它是如何运作的?

我还需要显式处理异常,如:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
public boolean addNewCustomer(CustomerDTO customerDTO) {
    Customer customer = new Customer();

    customer.setCustomerId(customerDTO.getCustomerId());
    customer.setCustomerName(customerDTO.getCustomerName());
    customer.setActive(customerDTO.isActive());

    try {
        getDaoProvider().getCustomerDao().save(customer);
    } catch (Throwable throwable) {
        return false;
    }

    return true;
}

强制我通过删除customer表中的列来创建异常,但是try-catch块中没有捕获异常,而是我可以从我调用{{1}的托管bean中捕获该异常方法。

2 个答案:

答案 0 :(得分:8)

这是Spring docs的摘录

  

在其默认配置中,Spring Framework的事务   基础结构代码仅标记用于回滚的事务   运行时,未经检查的异常;也就是抛出异常的时候   是RuntimeException的实例或子类。 (错误也将 - 通过   default - 导致回滚)。检查抛出的异常   从事务方法不会导致默认回滚   配置。

你设置rollbackFor = Throwable.class,现在Spring将回滚任何异常/错误。默认情况下,无论我们是否喜欢,Spring将仅针对RuintimeException进行回滚,否则提交

答案 1 :(得分:6)

Spring框架只抛出RuntimeExceptions,从技术上讲,你永远不必捕获任何异常。