[英]Is Exception handling required in Spring Transaction?
I am having a doubt in exception handling with a Transaction. 我对使用Transaction进行异常处理有疑问。 To state clearly my problem I would like to show my configuration: 为了清楚说明我的问题,我想展示我的配置:
<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>
The active transaction class is: 活动事务类是:
@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);
}
}
My doubts lies in the method addNewCustomer
. 我的疑惑在于addNewCustomer
方法。 I have set rollbackFor = { Throwable.class }
. 我已经设置了rollbackFor = { Throwable.class }
。
How does it work? 它是如何工作的?
Also do I need to explicitly handle exception like: 我还需要显式处理异常,如:
@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;
}
Forcefully I have created an exception by deleting a column from the customer table, BUT that exception wasn't catch in the try-catch block, rather I can catch that exception from the managed bean where I have invoked the addNewCustomer
method. 强制我通过从customer表中删除一个列来创建一个异常,但是在try-catch块中没有捕获异常,而是我可以从我调用了addNewCustomer
方法的托管bean中捕获该异常。
This is an excerpt from Spring docs 这是Spring文档的摘录
In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; 在其默认配置中,Spring Framework的事务基础结构代码仅在运行时未经检查的异常情况下标记用于回滚的事务; that is, when the thrown exception is an instance or subclass of RuntimeException. 也就是说,抛出的异常是RuntimeException的实例或子类。 (Errors will also - by default - result in a rollback). (错误也将 - 默认情况下 - 导致回滚)。 Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration. 从事务方法抛出的已检查异常不会导致在默认配置中回滚。
You set rollbackFor = Throwable.class, now Spring will rollback for any Exception / Error. 你设置rollbackFor = Throwable.class,现在Spring会回滚任何Exception / Error。 By default, whether we like it or not, Spring will rollback only for RuintimeException, and commit otherwise 默认情况下,无论我们是否喜欢,Spring将仅针对RuintimeException进行回滚,否则进行提交
Spring框架只抛出RuntimeExceptions,从技术上讲,你永远不必捕获任何异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.