我對使用事務處理異常有疑問。爲了表述清楚我的問題,我想表明我的配置:Spring 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;
}
強行我已經刪除從客戶表中的列上創建一個例外,但例外是不是在try-catch塊來抓,相反,我可以從託管的bean中調用addNewCustomer
方法來捕獲該異常。
強制我創建了一個異常,從客戶表中刪除一列,但是這個異常沒有在try-catch塊中被捕獲,而是我可以從託管bean中捕獲那個我調用了'addNewCustomer'方法的異常。 –
實際上,在具體的情況下,rollbackFor = Throwable.class是沒有意義的,因爲addNewCustomer不能拋出任何checked異常,因爲它沒有聲明任何異常,並且RuntimeExceptions被默認捕獲。 –
謝謝你的解釋。請告訴我,春天的XML配置好嗎? –