5

我对事务的异常处理有疑问。为了清楚地说明我的问题,我想展示我的配置:

<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;
}

我通过从客户表中删除一列强制创建了一个异常,但是该异常没有在 try-catch 块中捕获,而是我可以从调用该addNewCustomer方法的托管 bean 中捕获该异常。

4

2 回答 2

8

这是 Spring 文档的摘录

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

您设置了 rollbackFor = Throwable.class,现在 Spring 将为任何异常/错误回滚。默认情况下,不管我们喜不喜欢,Spring 只会对 RuintimeException 进行回滚,否则提交

于 2012-12-14T18:26:55.777 回答
6

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

于 2012-12-14T18:03:31.323 回答