springmvc+mybaits一个事物同时update和调用存储过程异常回滚

事物作用的impl类这样写的

1@Override 2 public int updateReturnAll(int item, int status, int idUser) { 3 // TODO Auto-generated method stub 4 try { 5 int updateReturnAll = itemMapper.update****(); 6 if(updateReturnAll>0){ 7 Map<String, Object> map=new HashMap<String,Object>(); 8 map.put("idpatient", item); 9 map.put("ChargeDealWith", 0); 10 Object callUpdatePPFIReturn = itemMapper.callUpdate****(map); 11 System.out.println(callUpdatePPFIReturn+"=================="); 12 System.out.println(map.get("RetMeg")+"----------"); 13 if(!map.get("RetMeg").equals("")){ 14 throw new Exception("callUpdatePPFIReturn异常!"); 15 } 16 return updateReturnAll; 17 }else{ 18 return 0; 19 } 20 21 } catch (Exception e) { 22 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 23 logger.error("updateReturnAll异常!",e); 24 return 0; 25 } 26 }

如果没有

1TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();指定异常回滚,update是会提交的此时,为了防止提交,是需要将异常抛出的,以便让aop捕获异常再去回滚 2 3@官网文档

11.4.3. Rolling Back a Declarative Transaction 2The previous section outlined the basics of how to specify transactional settings for classes, typically service layer classes, declaratively in your application. This section describes how you can control the rollback of transactions in a simple, declarative fashion. 3 4The recommended way to indicate to the Spring Framework’s transaction infrastructure that a transaction’s work is to be rolled back is to throw an Exception from code that is currently executing in the context of a transaction. The Spring Framework’s transaction infrastructure code catches any unhandled Exception as it bubbles up the call stack and makes a determination whether to mark the transaction for rollback. 5 6In its default configuration, the Spring Framework’s transaction infrastructure code marks a transaction for rollback only in the case of runtime, unchecked exceptions. That is, when the thrown exception is an instance or subclass of RuntimeException. ( Error instances 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. 7 8You can configure exactly which Exception types mark a transaction for rollback, including checked exceptions. The following XML snippet demonstrates how you configure rollback for a checked, application-specific Exception type: 9 10<tx:advice id="txAdvice" transaction-manager="txManager"> 11 <tx:attributes> 12 <tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/> 13 <tx:method name="*"/> 14 </tx:attributes> 15</tx:advice> 16If you do not want a transaction rolled back when an exception is thrown, you can also specify 'no rollback rules'. The following example tells the Spring Framework’s transaction infrastructure to commit the attendant transaction even in the face of an unhandled InstrumentNotFoundException: 17 18<tx:advice id="txAdvice"> 19 <tx:attributes> 20 <tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/> 21 <tx:method name="*"/> 22 </tx:attributes> 23</tx:advice> 24When the Spring Framework’s transaction infrastructure catches an exception and it consults the configured rollback rules to determine whether to mark the transaction for rollback, the strongest matching rule wins. So, in the case of the following configuration, any exception other than an InstrumentNotFoundException results in a rollback of the attendant transaction: 25 26<tx:advice id="txAdvice"> 27 <tx:attributes> 28 <tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/> 29 </tx:attributes> 30</tx:advice> 31You can also indicate a required rollback programmatically. Although simple, this process is quite invasive and tightly couples your code to the Spring Framework’s transaction infrastructure. The following example shows how to programmatically indicate a required rollback: 32 33public void resolvePosition() { 34 try { 35 // some business logic... 36 } catch (NoProductInStockException ex) { 37 // trigger rollback programmatically 38 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 39 } 40} 41You are strongly encouraged to use the declarative approach to rollback, if at all possible. Programmatic rollback is available should you absolutely need it, but its usage flies in the face of achieving a clean POJO-based architecture.

View Code

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用