问题描述
我对使用事务的异常处理有疑问。要清楚地说明我的问题,我想显示我的配置: < bean id =transactionManagerclass =org。 springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< tx:注释驱动的transaction-manager =transactionManager/>
< bean id =transactionInterceptorabstract =trueclass =org.springframework.transaction.interceptor.TransactionProxyFactoryBean>
< property name =transactionManagerref =transactionManager/>
< property name =transactionAttributeSource>
< bean class =org.springframework.transaction.annotation.AnnotationTransactionAttributeSource/>
< / property>
< / bean>
< bean id =baseServiceabstract =true>
< property name =daoProviderref =daoProvider/>
< / bean>
< bean id =customerServiceparent =transactionInterceptor>
< property name =target>
< bean class =com.edfx.adb.service.CustomerServiceparent =baseService/>
< / property>
< / bean>
< bean id =daoProviderclass =com.edfx.adb.dao.provider.DaoProvider>
< property name =customerDaoref =customerDao/>
< / bean>
< bean id =customerDaoclass =com.edfx.adb.dao.CustomerDao>
< constructor-arg value =#{T(com.edfx.adb.persist.entity.Customer)}/>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
活动的交易类是:
@Transactional
public class CustomerService extends BaseService实现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.setCustomerId(customerDTO.getCustomerId());
customer.setCustomerName(customerDTO.getCustomerName());
customer.setActive(customerDTO.isActive());
getDaoProvider()。getCustomerDao()。save(customer);
}
}
我的疑惑在于 addNewCustomer
。我设置了 rollbackFor = {Throwable.class}
。
它是如何工作的?
我还需要明确处理异常,如:
@Transactional(readOnly = false,传播= 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;
}
返回true;
}
强制我通过从客户表中删除列创建了一个例外,但是那个异常没有在try-catch块中捕获,而是可以从被调用的$ code> addNewCustomer 方法的托管bean中捕获该异常。
这是Spring文档摘录
你设置rollbackFor = Throwable.class,现在Spring将回滚任何异常/错误。默认情况下,无论我们喜欢还是不喜欢,Spring将仅针对RuintimeException进行回滚,否则
I am having a doubt in exception handling with a 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
. I have set 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.
This is an excerpt from Spring docs
You set rollbackFor = Throwable.class, now Spring will rollback for any Exception / Error. By default, whether we like it or not, Spring will rollback only for RuintimeException, and commit otherwise
这篇关于Spring Transaction中是否需要异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!