spring事物的隔离等级

原创
07/21 22:32
阅读数 31

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);
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部