spring:org.springframework.transaction.UnexpectedRollbackException解决

来源:互联网 发布:手机组态软件 编辑:程序博客网 时间:2024/04/29 09:08
@Transactionalpublic boolean copyCustomerInfo(Customer customer){    ……    return intsertCustomer(customer);    ……}@Transactionalpublic boolean intsertCustomer(Customer customer){    ……    try{        intsert1();        intsert2();    }catch (Exception e){        logger.error(e);        return false;    }    ……}

当intsertCustomer里面抛出异常但被捕获时,理论上在copyCustomerInfo里应该返回false,但是结果并没有,页面报如下错误:
这里写图片描述

网上找了很多资料,总结主要有以下几种解决方法:
1、copyCustomerInfo方法加:

@Transactional(readOnly = true, rollbackFor = RuntimeException.class)

2、intsertCustomer方法加

@Transactional(noRollbackFor = {RuntimeException.class})

3、改配置文件

<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /><property name="globalRollbackOnParticipationFailure" value="false" /> <!--加入该代码,指定此参数为false--></bean> 

第2种办法是绝对不能做的,因为内嵌的事务不是我写的,我只是调用了人家的方法,不方便去改人家的代码。
第3种办法,在实际项目中用也不是很方便,要是这么改了,不确定对项目别的地方是否会产生影响,结果用了第1种办法,很不幸,返回不了我想要的结果。
最后的解决办法是:

@Transactional(propagation = Propagation.NOT_SUPPORTED)public boolean copyCustomerInfo(Customer customer){    ……    return intsertCustomer(customer);    ……}

Propagation.NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

转载请标明原址:http://blog.csdn.net/zheng911209/article/details/50130869

0 0
原创粉丝点击