当前位置:网站首页>@Transactional transaction methods contain multiple transaction methods of the same kind. These transaction methods themselves are set to fail. There are two solutions
@Transactional transaction methods contain multiple transaction methods of the same kind. These transaction methods themselves are set to fail. There are two solutions
2022-07-23 07:54:00 【Time is an antidote】
@Transactional The transaction method contains multiple transaction methods of the same kind , There are two solutions to the failure of these transaction methods
The problem background
1 @Transactional Call similar methods , Different settings @Transactional There are two solutions to failure
2 Pseudo code illustrates the problem
- a,b,c Three methods are in the same class , Call for this method
- a,b,c The transaction settings of the three methods are different
- Similar method calls make a,b Of Transactional Transaction settings are invalid , Unified follow c Method , This is not the result we want
public class TransactionServiceImpl implements TransactionService {
@Transactional(propagation = Propagation.MANDATORY)
public void a(){
}
@Transactional(isolation = Isolation.READ_COMMITTED)
public void b(){
}
@Transactional
public void c(){
//1. a,b,c Three methods are in the same class , Call for this method
//2. a,b,c The transaction settings of the three methods are different
//3. Similar method calls make a,b Of Transactional Transaction settings are invalid , Unified follow c Method , This is not the result we want
a();
b();
}
}
Solution 1
1 Because the transaction annotation is through spring Of IOC The control inversion of the container is realized , Call this class method directly , Not used spring Dynamic proxy for , So you can change it to other methods to call methods of other classes , Is the dynamic proxy effective
@Service
public class AbServiceImpl implements AbService {
@Transactional(propagation = Propagation.MANDATORY)
public void a(){
}
@Transactional(isolation = Isolation.READ_COMMITTED)
public void b(){
}
}
@Service
public class TransactionServiceImpl implements TransactionService {
// from IOC Get the proxy object in the container , Make dynamic proxy effective
@Autowired
AbService abService;
@Transactional
public void c(){
//a,b,c Three methods in different classes , Use dynamic proxy to call , In this way, three kinds of Transactional The transaction settings take effect
abService.a();
abService.b();
}
}
Solution 2
1 Use spring The dependency to obtain the dynamic proxy object is provided
<!-- introduce aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2 Use... In the startup class @EnableAspectJAutoProxy(exposeProxy = true) annotation , Turn on aspect Dynamic proxy model , Expose proxy objects , You can delegate the current object , send @Asycn and @Transactional take effect , All dynamic agents use aspectj establish , Than JDK More benefits are : You can proxy without opening the interface
If you don't open this annotation , By default JDK Dynamic proxy for
package com.lanran.transactional;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAspectJAutoProxy(exposeProxy = true) // Open the aspect Dynamic proxy model , Expose proxy objects , You can delegate the current object , send @Asycn and @Transactional take effect
@MapperScan("com.lanran.transactional.dao")
@SpringBootApplication
public class TransactionalApplication {
public static void main(String[] args) {
SpringApplication.run(TransactionalApplication.class, args);
}
}
3 Get the proxy object and call the method
package com.lanran.transactional.service.impl;
import com.lanran.transactional.service.TransactionService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/** * @Author suolong * @Date 2022/7/21 11:21 * @Version 2.0 */
@Service
public class TransactionServiceImpl implements TransactionService {
@Transactional(propagation = Propagation.MANDATORY)
public void a() {
}
@Transactional(isolation = Isolation.READ_COMMITTED)
public void b() {
}
@Transactional
public void c() {
// Get the proxy object from the context
TransactionServiceImpl transactionService = (TransactionServiceImpl) AopContext.currentProxy();
transactionService.a();
transactionService.b();
}
}
summary
This question reminds me of @Async This asynchronous annotation , The principle is the same , The same method calls the method of asynchronous annotation , Asynchrony will fail , Become synchronized , Only call other classes @Async The method works
As a programmer 209 An article , Write one line of lyrics at a time and record , Look at how many songs there are in life ,wahahaha …


Lyric: With the melody of the right hand
边栏推荐
- 工作流引擎在vivo营销自动化中的应用实践
- Classes et objets (1)
- RN底层原理 -- 1. Component和PureComponent解析
- Mysql的索引为什么用B+树而不是跳表?
- 一次 MySQL 误操作导致的事故,「高可用」都顶不住了
- 我在京东使用工厂模式,一文说清楚工厂模式
- How to use the order flow analysis tool (in)
- ROS based navigation framework
- File upload, server file name Chinese garbled file upload, server file name Chinese garbled
- 6-13漏洞利用-smtp暴力破解
猜你喜欢

Classes et objets (1)

Understand the domestic open source Magnolia license series agreement in simple terms

ROS based navigation framework

Talk about 12 business scenarios of concurrent programming

Why does MySQL index use b+ tree instead of jump table?

FastAPI学习(二)——FastAPI+Jinjia2模板渲染网页(跳转返回渲染页面)

With 130 new services and functions a year, this storage "family bucket" has grown again

Qt文档阅读笔记-QAudioInput&QAudioFormat解析与实例

牛客小白月赛53

21 -- 除自身以外数组的乘积
随机推荐
百度搜索打击盗版网文站点:互联网内容侵权现象为何屡禁不止
1.10 API 和字符串
Fastapi learning (II) -- fastapi+jinjia2 template rendering web page (jump back to the rendering page)
Squid proxy service +ip proxy pool
成功解决:error: src refspec master does not match any
Inside the hard core of LAN SDN technology - evpn implementation of 16 three from thing to person user roaming in the park
局域网SDN技术硬核内幕 7 从二层到大二层
局域网SDN技术硬核内幕 4 从计算虚拟化到网络虚拟化
程序员最想干的三件事 |漫画
How to use the order flow analysis tool (in)
Copytexture, copytoresolvetarget of UE4 engine
毕业设计-----基于STM32的物联网环境检测系统
Interpretation of URL structure
6-15漏洞利用-smb-RCE远程命令执行
002_Kubernetes安装配置
Scala 获取指定目录下的所有文件
局域网SDN技术硬核内幕 9 从软件Overlay到硬件Overlay
Alibaba Cloud Security Center's best practices for vulnerability repair
Information system project managers must recite the core examination points (49) contract law
Mysql的索引为什么用B+树而不是跳表?