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


    事物作用的impl类这样写的

    @Override
        public int updateReturnAll(int item, int status, int idUser) {
            // TODO Auto-generated method stub
            try {
                int updateReturnAll = itemMapper.update****();
                if(updateReturnAll>0){
                    Map<String, Object> map=new HashMap<String,Object>();
                    map.put("idpatient", item);
                    map.put("ChargeDealWith", 0);
                    Object callUpdatePPFIReturn = itemMapper.callUpdate****(map);
                    System.out.println(callUpdatePPFIReturn+"==================");
                    System.out.println(map.get("RetMeg")+"----------");
                    if(!map.get("RetMeg").equals("")){
                        throw new Exception("callUpdatePPFIReturn异常!");
                    }
                    return updateReturnAll;
                }else{
                    return 0;
                }
                
            } catch (Exception e) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                logger.error("updateReturnAll异常!",e); 
                return 0;
            }
        }

    如果没有

    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();指定异常回滚,update是会提交的
    此时,为了防止提交,是需要将异常抛出的,以便让aop捕获异常再去回滚
    @官网文档
    1.4.3. Rolling Back a Declarative Transaction
    The 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.
    
    The 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.
    
    In 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.
    
    You 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:
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
        <tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
        <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    If 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:
    
    <tx:advice id="txAdvice">
        <tx:attributes>
        <tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
        <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    When 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:
    
    <tx:advice id="txAdvice">
        <tx:attributes>
        <tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
        </tx:attributes>
    </tx:advice>
    You 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:
    
    public void resolvePosition() {
        try {
            // some business logic...
        } catch (NoProductInStockException ex) {
            // trigger rollback programmatically
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
    You 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
    
    
    
     
  • 相关阅读:
    vue项目webpack配置terser-webpack-plugin 去掉项目中多余的debugger
    difference between count(1) and count(*)
    为什么PostgreSQL WAL归档很慢
    mysql_reset_connection()
    Oracle使用audit跟踪登录失败的连接信息
    .NET Standard 版本
    ASP.NET Web API版本
    我是如何用go-zero 实现一个中台系统的
    JAVA中文件写入的6种方法
    MySql 常用语句
  • 原文地址:https://www.cnblogs.com/yanan7890/p/8871935.html
Copyright © 2020-2023  润新知