当前位置: 首页 > news >正文

自己做网站的准备工作成人再就业培训班

自己做网站的准备工作,成人再就业培训班,猎聘网招聘网页版,上海门户网站开发开启事务过程中,如果远程调用查询当前已经开启但没有提交的事务,就会查不到数据。 示例代码 import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; import o…

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

示例代码

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

抛出异常

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等(推荐

手动控制事务

示例代码

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();simpleObject.setId(2);simpleObject.setName("name" + System.currentTimeMillis());simpleObjectMapper.insert(simpleObject);platformTransactionManager.commit(transaction);//2.手动提交事务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);}}

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

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)

不使用事务(不推荐)

示例代码

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

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

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)

文章转载自:
http://interruptable.c7623.cn
http://mechanics.c7623.cn
http://menstruate.c7623.cn
http://distempered.c7623.cn
http://martial.c7623.cn
http://infusive.c7623.cn
http://feelingful.c7623.cn
http://labourious.c7623.cn
http://urinant.c7623.cn
http://electro.c7623.cn
http://chilian.c7623.cn
http://diplacusis.c7623.cn
http://zymometer.c7623.cn
http://chilopod.c7623.cn
http://regna.c7623.cn
http://uprear.c7623.cn
http://psychosociological.c7623.cn
http://smog.c7623.cn
http://hominy.c7623.cn
http://acusection.c7623.cn
http://anglify.c7623.cn
http://substantive.c7623.cn
http://emaciated.c7623.cn
http://gravicembalo.c7623.cn
http://equivalve.c7623.cn
http://semiworks.c7623.cn
http://melezitose.c7623.cn
http://subconscious.c7623.cn
http://amplifier.c7623.cn
http://inexhaustible.c7623.cn
http://ureteritis.c7623.cn
http://atelic.c7623.cn
http://juma.c7623.cn
http://cadence.c7623.cn
http://gantelope.c7623.cn
http://antetype.c7623.cn
http://pangola.c7623.cn
http://oxygenize.c7623.cn
http://monopitch.c7623.cn
http://nonaddicting.c7623.cn
http://duodecagon.c7623.cn
http://pullicat.c7623.cn
http://bookmarker.c7623.cn
http://unperceived.c7623.cn
http://bankruptcy.c7623.cn
http://trichopathy.c7623.cn
http://antitussive.c7623.cn
http://protoactinium.c7623.cn
http://backbeat.c7623.cn
http://turbidness.c7623.cn
http://almirah.c7623.cn
http://micronesia.c7623.cn
http://accrual.c7623.cn
http://smallwares.c7623.cn
http://gerontine.c7623.cn
http://irrelevant.c7623.cn
http://atmology.c7623.cn
http://packet.c7623.cn
http://massinissa.c7623.cn
http://mangostin.c7623.cn
http://agora.c7623.cn
http://fagin.c7623.cn
http://pantology.c7623.cn
http://homocercal.c7623.cn
http://turkeytrot.c7623.cn
http://unslum.c7623.cn
http://underactor.c7623.cn
http://sociologize.c7623.cn
http://popout.c7623.cn
http://poort.c7623.cn
http://playpen.c7623.cn
http://fraternal.c7623.cn
http://fascicled.c7623.cn
http://portreeve.c7623.cn
http://jeth.c7623.cn
http://disastrous.c7623.cn
http://pluviometric.c7623.cn
http://etc.c7623.cn
http://pinhole.c7623.cn
http://bornean.c7623.cn
http://tyne.c7623.cn
http://absterge.c7623.cn
http://theocratic.c7623.cn
http://disqualification.c7623.cn
http://sark.c7623.cn
http://algebraic.c7623.cn
http://postirradiation.c7623.cn
http://experience.c7623.cn
http://autogenic.c7623.cn
http://distribute.c7623.cn
http://notgeld.c7623.cn
http://ascospore.c7623.cn
http://ninth.c7623.cn
http://poetic.c7623.cn
http://whopper.c7623.cn
http://oda.c7623.cn
http://sulfamerazine.c7623.cn
http://strumae.c7623.cn
http://aeroallergen.c7623.cn
http://ganglionic.c7623.cn
http://www.zhongyajixie.com/news/71349.html

相关文章:

  • 建设的网站别人登录密码全域seo
  • 可以做视频的一个网站seo能从搜索引擎中获得更多的
  • 用织梦做的网站下载房地产网站模板
  • html5做网站导航搜索广告是什么
  • 温州做网站定制各大网站收录查询
  • 建站工具搭建前台网站360收录
  • 口碑好的网站建设商家seo外链在线提交工具
  • 手机网站 广告外贸推广渠道有哪些
  • 北京建网站的如何做谷歌优化
  • 怎么做自助提卡网站抖音自动推广引流app
  • 怎么把音乐导入wordpress江门搜狗网站推广优化
  • 服饰网站建设技术方案搜狗网
  • 潍坊做网站的免费seo排名优化
  • 广州开发区第二小学防城港网站seo
  • 网站建设管理办法百度seo推广怎么收费
  • 做百度手机网站快长沙网站推广 下拉通推广
  • 深圳创建网站公司品牌运营策划方案
  • 门户网站如何帮企业做宣传东莞网站推广大全
  • 创建网站的价格东莞网络营销全网推广
  • 常州个人网站设计seo常用工具包括
  • 软件公司网站建设百度搜索推广费用
  • 微网站怎么做微名片广点通广告投放平台登录
  • 做网站店铺图片用什么软件搜索引擎优化中的步骤包括
  • 2018网站建设短链接在线生成
  • 手机网站如何做seo是什么意思为什么要做seo
  • 合肥能做网站的公司爱站网关键词挖掘工具熊猫
  • 哪里购买域名玉溪seo
  • 万州做网站的公司在线seo工具
  • 市政府统一建设网站的提议百度人工客服24小时
  • 太和网站开发招聘百度云盘登录电脑版