spring事物的隔离等级

spring事物的隔离等级

servcie 1 


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author  
 * @version 1.0 * 
 * @description 测试@Tansational 注解是否生效 * 
 * @since 2020-06-23 16:51
 */
@Servicepublic
class TestTransactionalSub1Impl {
    @Autowired
    private TDicMapper tDicMapper;
    @Autowired
    private TDicDescMapper tDicDescMapper;
    @Autowired
    private TestTransactionalSub2Impl testTransactionalSub2;

    @Transactional(propagation = Propagation.REQUIRED)
    public void it(TDic tDic) {
//        TDicDesc tDicDesc = new TDicDesc();//        tDicDesc.setDicCode("001");//        tDicDesc.setDisDesc("001");//        tDicDescMapper.insert(tDicDesc);
        tDicMapper.insert(tDic);
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
//                try {//                    int num = 1/0;//                }catch (Exception ex){//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//                    ex.printStackTrace();//                }                testTransactionalSub2.it222(tDic,true);
            } else {
                testTransactionalSub2.it222(tDic, false);
            }
        }
    }
}

service 2




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @author 
 * @version 1.0 *
 * @description 测试
 * @Tansational 注解是否生效 * 
 * @since 2020-06-23 16:54
 */
@Servicepublic
class TestTransactionalSub2Impl {
    @Autowired
    private TDicMapper tDicMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void it222(TDic tDic, boolean exceptionFlag) {
        tDic.setDicKey("002");
        tDicMapper.insert(tDic);
        try {
            if (exceptionFlag) {
                int i = 1 / 0;
            }
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ex.printStackTrace();
        }
    }
}

测试类:

@Test@Rollback(false)
public void testTransational(){
    TDic tDic = new TDic();
    tDic.setDicCode("industryInfo");
    tDic.setDicKey("001");
    tDic.setDicValue("001");
   testTransactionalSub1.it(tDic);
}

 

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » spring事物的隔离等级