• spring 只读事务 设置异常回滚事务


    @Transactional(readOnly = true)

    将当前事务设置为只读事务,在该事务执行过程中只允许select语句执行,delete insert update均不可执行。

    该特性的作用是:启动spring的优化策略。提高select语句执行效率。

    @Transactional(rollbackFor = RuntimeException.class)

    表示只有发生RuntimeException异常或该异常的子类异常才回滚

    @Transactional(noRollbackFor = NullPointerException.class)

    表示发生NullPointerException或该异常的子类异常不回滚,其他异常则回滚

    1. package com.service;
    2. import com.dao.AccountDao;
    3. import com.pojo.Account;
    4. import jakarta.annotation.Resource;
    5. import org.springframework.stereotype.Service;
    6. import org.springframework.transaction.annotation.Transactional;
    7. /**
    8. * @program: spring_learn
    9. * @description:
    10. * @author: Mr.Wang
    11. * @create: 2023-06-09 07:09
    12. **/
    13. @Service
    14. public class Service02 {
    15. @Resource(name="accountDao")
    16. public AccountDao accountDao;
    17. @Transactional(noRollbackFor = NullPointerException.class)
    18. public void update(Account act) {
    19. accountDao.update(act);
    20. throw new NullPointerException();
    21. }
    22. }

     ​​​@Transactional(noRollbackFor = NullPointerException.class)

    发生NullPointerException异常时不回滚

    test

    1. package com;
    2. import com.pojo.Account;
    3. import com.service.Service01;
    4. import com.service.Service02;
    5. import org.junit.Test;
    6. import org.springframework.context.ApplicationContext;
    7. import org.springframework.context.support.ClassPathXmlApplicationContext;
    8. /**
    9. * @program: spring_learn
    10. * @description:
    11. * @author: Mr.Wang
    12. * @create: 2023-06-09 07:47
    13. **/
    14. public class IsolationTest {
    15. @Test
    16. public void testIsolation2(){
    17. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    18. Service02 i2 = applicationContext.getBean("service02", Service02.class);
    19. Account act = new Account("004", 1000.0);
    20. i2.update(act);
    21. }
    22. }

     before testing

    after testing

     

    数据更新,未回滚, 

     

     

    不使用         ​@Transactional(noRollbackFor = NullPointerException.class)

    1. package com.service;
    2. import com.dao.AccountDao;
    3. import com.pojo.Account;
    4. import jakarta.annotation.Resource;
    5. import org.springframework.stereotype.Service;
    6. import org.springframework.transaction.annotation.Transactional;
    7. /**
    8. * @program: spring_learn
    9. * @description:
    10. * @author: Mr.Wang
    11. * @create: 2023-06-09 07:09
    12. **/
    13. @Service
    14. public class Service02 {
    15. @Resource(name="accountDao")
    16. public AccountDao accountDao;
    17. @Transactional
    18. public void update(Account act) {
    19. accountDao.update(act);
    20. throw new NullPointerException();
    21. }
    22. }

    Before testing: 

    After testing:

    事务回滚,数据未更新 

     

     

     

  • 相关阅读:
    ubuntu 22.04 编译 aosp 13 源码
    JavaScript基础知识12——运算符:算数运算符,比较运算符
    第五章:Spring细分一如何让mapper文件夹下的SQL语句对数据库生效,jdbc和mybatis-config.xml
    SAS 字符串函数-SCAN
    Three.js 绘制动态模型
    文件共享(通过git实现,提供脚本)
    Linux系统访问卡顿 慢
    开发人员都需要知道的几款优秀数据库管理工具
    争夺细分新赛道,哪十家企业「领跑」L4级自动驾驶域控制器?
    React Router 中的 exact 属性
  • 原文地址:https://blog.csdn.net/weixin_41812346/article/details/131144292