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 map=new HashMap();
                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:


    
    for="NoProductInStockException"/>
    
    

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:


    
    for="InstrumentNotFoundException"/>
    
    

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:


    
    for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
    

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
 
   

 

 

转载于:https://www.cnblogs.com/yanan7890/p/8871935.html

你可能感兴趣的:(springmvc+mybaits一个事物同时update和调用存储过程异常回滚)