首页 > 其他分享 >@Transactional 注解下,事务失效的多种场景

@Transactional 注解下,事务失效的多种场景

时间:2024-06-07 16:55:33浏览次数:20  
标签:事务 MyException public void 失效 注解 updateStudentA Transactional

package com.example.api.demo.boke;

import com.example.api.demo.config.exceptions.MyException;
import org.springframework.transaction.annotation.Transactional;

/**
 *  @Transactional
 *  注解下,事务失效的七种场景
 */
public class Transaction {

    /**
     * 1 异常被捕获后没有抛出
     * 当异常被捕获后,并且没有再抛出,那么updateStudentA是不会回滚的。
     */
    @Transactional
    public void updateStudent() {
        this.updateStudentA();
        try {
            int i = 1 / 0;
            this.updateStudentB();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 2 抛出非运行时异常
     * 异步虽然抛出了,但是抛出的是非RuntimeException类型的异常,依旧不会生效。
     */
    @Transactional
    public void updateStudent2() throws MyException{
        this.updateStudentA();
        try {
            int i = 1 / 0;
            this.updateStudentB();
        } catch (Exception e) {
            throw new MyException();
        }
    }

    /**
     * 3 方法内部直接调用
     * 如果先调用updateStudent3(),那么updateStudent3()是不会回滚的,其原因就是@Transactional根本没生成代理,
     */
    public void updateStudent3() throws MyException{
        updateStudentG();
    }
    @Transactional
    public void updateStudentG() throws MyException{
        this.updateStudentA();
        int i = 1 / 0;
    }

    /**
     * 4 新开启一个线程
     * 如下的方式updateStudentA()也不会回滚,因为spring实现事务的原理是通过ThreadLocal把数据库连接绑定到当前线程中,
     * 新开启一个线程获取到的连接就不是同一个了。
     */
    @Transactional
    public void updateStudent4() throws MyException{
        this.updateStudentA();
        try {
            Thread.sleep(1000); //休眠1秒,保证updateStudentA先执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            int i = 1/0;
        }).start();
    }

    /**
     * 5 注解到private方法上
     * idea直接会给出提示Methods annotated with ‘@Transactional’ must be overridable ,原理很简单,private修饰的方式,spring无法生成动态代理。
     */
    @Transactional
    private void updateStudent5() throws MyException {
        this.updateStudentA();
        int i = 1/0;
    }

    /**
     * 6、 据库本身不支持: mysql数据库,必须设置数据库引擎为InnoDB。
     * 7、 事务传播属性设置错误, 比如设置了:PROPAGATION_NOT_SUPPORIED(以非事务的方式执行,如果当前有事务则把当前事务挂起)
     * 8、 事务方法被final、static修饰
     * 9、 当前类没有被Spring管理
     */


    private void updateStudentB() {
        System.out.println();
    }

    private void updateStudentA() {
        System.out.println();
    }


}

标签:事务,MyException,public,void,失效,注解,updateStudentA,Transactional
From: https://www.cnblogs.com/lgg20/p/18237506

相关文章

  • SpringBoot的常用注解
    Spring知识点总结SpringBoot是否需要独立容器不需要,内置了Tomcat/Jetty等容器。运行SpringBoot几种方式打包用命令或者放到容器中运行用Maven/Gradle插件、执行main方法。SpringloC实现机制工厂模式加反射。SpringBoot配置文件几种格式之间区别.properties和.yml格式,区......
  • @Configuration注解
    @Configuration介绍@Configuration是Spring框架中的一个核心注解,它用于标记一个类为配置类,此类主要用于声明Bean以及应用的配置信息。在Spring容器中,通过扫描带有@Configuration注解的类,可以将其实例化并处理其中定义的Bean。在@Configuration注解的类中,可以使用@Bean注解的方法......
  • spring入门aop和ioc基于注解
    目录用注解代替xml文件中的部分配置请先观看链接用注解代替xml文件中的部分配置在要注册bean的地方添加注解@Component()不指定名字就是类名的首字母小写@Component("name")bean的名字就是括号中指定的值在注册完以后要开始注册扫描<!--重点是开启注解扫描-->......
  • python用于类型注解的库- typing
    一、简介动态语言的灵活性使其在做一些工具,脚本时非常方便,但是同时也给大型项目的开发带来了一些麻烦。自python3.5开始,PEP484为python引入了类型注解(typehints),虽然在pep3107定义了函数注释(functionannotation)的语法,但仍然故意留下了一些未定义的行为.现在已经拥有许......
  • 解决canvas上fillText填充后用clearRect清除失效,文字重叠问题
    最初写的demo:如下图: 文字内容未被清除掉,出现了重叠的问题,尝试了网上说的ctx.save(),ctx.restore(),beginPath()等方法都不好用,后来经过一番查找,终于解决了:改写如下: 在这里需要主要的点就是fillText的方法里参数表示的真正含义: 默认情况下,文本基线是位于文字底部,所......
  • 日志工具类之“根据标记的注解进行指定的字段日志记录-在展示方式上会美观一些”
    一、使用方法在添加、编辑等操作功能时可以使用该方案,在需要记录日志的实体类字段中进行注解标注。并标明对应的字段名二、代码1.使用LoggerUtils工具类生成日志publicJsonResultsavePrice(Priceprice){if(price.getId()!=null){String......
  • Spring-MVC注解支持Ant风格的模糊匹配和Restful风格的接收数据------Spring-MVC框架
    packagecom.alatus.mvc3.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassIndexController{......
  • java中的注解
    目录定义格式使用类型内置注解元注解自定义注解注解实现定义注解一般用于对程序的说明,想注释一样,但是区别是,注释是给人看的,注解是给程序看的让编译器进行编辑检查的作用,比如:@Override修饰的方法,如果改动了方法签名,将会编译报错格式注解是以@注解名在代码中存在,还可以添加一......
  • OpenTelemetry agent 对 Spring Boot 应用的影响:一次 SPI 失效的案例
    背景前段时间公司领导让我排查一个关于在JDK21环境中使用SpringBoot配合一个JDK18新增的一个SPI(java.net.spi.InetAddressResolverProvider)不生效的问题。但这个不生效的前置条件有点多:JDK的版本得在18+SpringBoot3.x还在额外再配合使用-javaagent:openteleme......
  • 关于vue关闭页面时去除定时器失效问题解决
    1.先去除页面缓存,这个在路由部分 2.    ......