• 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:

    事务回滚,数据未更新 

     

     

     

  • 相关阅读:
    FineBI产品简介
    JS中ajax的原理是什么?
    南辕北辙
    docker mysql 容器中执行mysql脚本文件并解决乱码
    【老生谈算法】matlab实现遗传算法源码——遗传算法
    【c++刷题Day2】专题3栈与队列&单调栈与单调队列T1
    VB.Net读写NFC Ntag标签源码
    如何正确的万无一失的学习python?
    关于 内部类 你了解多少?(详解!!)
    Elasticsearch 集群时的内部结构是怎样的?
  • 原文地址:https://blog.csdn.net/weixin_41812346/article/details/131144292