解决SpringBoot使用@Transactional进行RestTemplate远程调用导致查询数据记录为null的bug

开启事务过程中,如果远程调用查询当前已经开启但没有提交的事务,就会查不到数据。

示例代码

java 复制代码
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("simple")
@RequiredArgsConstructor
public class SimpleController {
    private final SimpleObjectMapper simpleObjectMapper;
    private final RestTemplate restTemplate;

    /**
     * 开启事务过程中,如果远程调用查询当前已经开启但没有提交的事务,就会查不到数据。
     */
    @GetMapping("insert")
    @Transactional(rollbackFor = Exception.class)//1.开启事务
    public void insert() {
        SimpleObject simpleObject = new SimpleObject();
        simpleObject.setId(2);
        simpleObject.setName("name" + System.currentTimeMillis());
        simpleObjectMapper.insert(simpleObject);//2.因为开启了事务,所以这里的 insert 并没有 commit,导致下面远程调用查询数据是空的。
        SimpleObject simpleObject1 = restTemplate.getForEntity("http://localhost:8080/simple/2", SimpleObject.class, simpleObject.getId()).getBody();
        log.info("simpleObject1 (这里会输出null):{}",simpleObject1);//3.这里会输出null,因为事务没有提交,数据库不会新增数据
        if (Objects.isNull(simpleObject1)) {
            throw new RuntimeException("simpleObject1 is null");
        }
    }

    /**
     * 被远程调用的查询方法
     */
    @GetMapping("{id}")
    public SimpleObject selectById(@PathVariable Integer id) {
        return simpleObjectMapper.selectById(id);
    }

}

抛出异常

shell 复制代码
2024-05-23 22:49:44.033  INFO 8668 --- [nio-8080-exec-1] MyBatisSqlParsingPlugin     : Execute SQL:
INSERT INTO simple_object  ( id,name )  VALUES  ( 2,'name1716475783993' )
2024-05-23 22:49:44.119  INFO 8668 --- [nio-8080-exec-2] MyBatisSqlParsingPlugin     : Execute SQL:
SELECT id,name FROM simple_object WHERE id=2 
2024-05-23 22:49:44.149  INFO 8668 --- [nio-8080-exec-1] SimpleController        : simpleObject1 (这里会输出null):null
2024-05-23 22:49:44.161 ERROR 8668 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
java.lang.RuntimeException: simpleObject1 is null

解决办法

1、去掉@Transactional注解(显然不可能,除非不影响正常的业务)

2、手动控制事务,但是有些情况下可能不适用,不适用的情况可以使用分布式事务,如:seata等(推荐)

手动控制事务

示例代码

java 复制代码
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("simple")
@RequiredArgsConstructor
public class SimpleController {
    private final SimpleObjectMapper simpleObjectMapper;
    private final RestTemplate restTemplate;
    private final PlatformTransactionManager platformTransactionManager;//事务管理器
    private final TransactionDefinition transactionDefinition;// 事务的一些基础信息,如超时时间、隔离级别、传播属性等

    /**
     * 使用手动事务,远程调用可以查询到数据。
     */
    @GetMapping("insert2")
    public void insert2() {
        //手动事务不能加@Transactional注解,否则优先使用@Transactional注解的事务
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);//1、手动获取事务
        SimpleObject simpleObject = new SimpleObject();
        try {
            simpleObject.setId(2);
            simpleObject.setName("name" + System.currentTimeMillis());
            simpleObjectMapper.insert(simpleObject);
            platformTransactionManager.commit(transaction);//2.手动提交事务
        } catch (Exception e) {
            platformTransactionManager.rollback(transaction);//4.发生异常手动回滚事务
            log.error("insert2异常:", e);
            throw new RuntimeException( "insert2异常:" + e.getMessage());
        }
        SimpleObject simpleObject1 = restTemplate.getForEntity("http://localhost:8080/simple/2", SimpleObject.class, simpleObject.getId()).getBody();
        log.info("simpleObject1 (这里会输出simpleObject1):{}",simpleObject1);//3.这里会输出id = 2的 SimpleObject 对象,因为事务提交,数据库会新增数据
        if (Objects.isNull(simpleObject1)) {
            throw new RuntimeException("simpleObject1 is null");
        }
    }

    /**
     * 被远程调用的查询方法
     */
    @GetMapping("{id}")
    public SimpleObject selectById(@PathVariable Integer id) {
        return simpleObjectMapper.selectById(id);
    }

}

不抛出异常,可以正常获取数据

shell 复制代码
2024-05-23 23:02:00.506  INFO 10608 --- [nio-8080-exec-1] c.f.m.plugin.MyBatisSqlParsingPlugin     : Execute SQL:
INSERT INTO simple_object  ( id,
name )  VALUES  ( 2,
'name1716476520466' )
2024-05-23 23:02:00.592  INFO 10608 --- [nio-8080-exec-2] c.f.m.plugin.MyBatisSqlParsingPlugin     : Execute SQL:
SELECT id,name FROM simple_object WHERE id=2 
2024-05-23 23:02:00.641  INFO 10608 --- [nio-8080-exec-1] c.f.m.controller.SimpleController        : simpleObject1 (这里会输出simpleObject1):SimpleObject(id=2, name=name1716476520466)

不使用事务(不推荐)

示例代码

java 复制代码
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("simple")
@RequiredArgsConstructor
public class SimpleController {
    private final SimpleObjectMapper simpleObjectMapper;
    private final RestTemplate restTemplate;

    /**
     * 不使用事务,远程调用可以查询到数据。(不推荐)
     */
    @GetMapping("insert3")
    public void insert3() {
        SimpleObject simpleObject = new SimpleObject();
        simpleObject.setId(2);
        simpleObject.setName("name" + System.currentTimeMillis());
        simpleObjectMapper.insert(simpleObject);//1、因为没有事务,所以这里会新增数据到数据库
        SimpleObject simpleObject1 = restTemplate.getForEntity("http://localhost:8080/simple/2", SimpleObject.class, simpleObject.getId()).getBody();
        log.info("simpleObject1 (这里会输出simpleObject1):{}",simpleObject1);//2.这里会输出id = 2的 SimpleObject 对象,因为事务提交,数据库会新增数据
        if (Objects.isNull(simpleObject1)) {
            throw new RuntimeException("simpleObject1 is null");
        }
    }

    /**
     * 被远程调用的查询方法
     */
    @GetMapping("{id}")
    public SimpleObject selectById(@PathVariable Integer id) {
        return simpleObjectMapper.selectById(id);
    }

}

不抛出异常,可以正常获取数据

shell 复制代码
2024-05-23 23:04:31.889  INFO 10608 --- [nio-8080-exec-6] c.f.m.plugin.MyBatisSqlParsingPlugin     : Execute SQL:
INSERT INTO simple_object  ( id,
name )  VALUES  ( 2,
'name1716476671888' )
2024-05-23 23:04:31.895  INFO 10608 --- [nio-8080-exec-7] c.f.m.plugin.MyBatisSqlParsingPlugin     : Execute SQL:
SELECT id,name FROM simple_object WHERE id=2 
2024-05-23 23:04:31.897  INFO 10608 --- [nio-8080-exec-6] c.f.m.controller.SimpleController        : simpleObject1 (这里会输出simpleObject1):SimpleObject(id=2, name=name1716476671888)
相关推荐
Vin__5 分钟前
微信小程序客服消息接收不到微信的回调
spring boot·微信小程序·小程序
路在脚下@29 分钟前
Java使用Redisson实现布隆过滤器
java·spring boot
多敲代码防脱发2 小时前
Spring框架基本使用(Maven详解)
java·网络·后端·spring·maven
周盛欢2 小时前
Spring Boot应用开发
spring boot
Asthenia04122 小时前
Mybatis实践——Wrapper&&三表联查&&BaseMapper和Service的功能分异
后端
B站计算机毕业设计超人2 小时前
计算机毕业设计SpringBoot+Vue.jst0甘肃非物质文化网站(源码+LW文档+PPT+讲解)
java·vue.js·spring boot·后端·spring·intellij-idea·课程设计
why技术3 小时前
可以说是一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
后端·面试
m0_748254663 小时前
定时任务特辑 Quartz、xxl-job、elastic-job、Cron四个定时任务框架对比,和Spring Boot集成实战
java·spring boot·后端
diemeng11193 小时前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust