当前位置:网站首页>Propagation of transactions
Propagation of transactions
2022-06-12 10:10:00 【Wang Rushuang】
List of articles
1.REQUIRED( The default mode )
Method is called to start the transaction automatically , If it is used within a transaction scope, the same transaction will be used , Otherwise, start a new transaction That is, business methods need to run in a container , If the method runs , Already in a business , So join in the business , Otherwise, create a new transaction by yourself .
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.REQUIRED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.REQUIRED)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : No matter what addStudent() still addTeacher(), If only one exception is thrown , Then all are rolled back ; Don't throw exceptions , Data is submitted normally
2.REQUIRES_NEW
Whether there is a transaction or not , The method always initiates a new transaction for itself . If the method is already running in a transaction , The original transaction is suspended , New transactions are created
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Student data cannot be submitted correctly , Teacher information is correctly submitted . Explain that these two operations are run in two independent transactions , And start the transaction as soon as the method is called .
3.Supports
Does not open a transaction by itself , The same transaction is used within the transaction scope , Otherwise, no transactions are used , That is, the method is called within a transaction scope , The method becomes part of the transaction , If the method is not called in a transaction , This method is executed in a transaction free environment , because Supports By default, the transaction is not opened
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.SUPPORTS)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.SUPPORTS)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Student data and teacher data are correctly submitted . This indicates that these two operations have not been spring Manage and open transactions , Instead, local transactions are used , Since local transactions are automatically committed by default, all data are submitted successfully , But they do not use the same transaction , Once an exception occurs, the data will be inconsistent
4.NOT_SUPPORTED
Declaring methods does not require transactions , That is, if the method is not associated with a transaction , The container won't open the business for him , If the method is called in a transaction , The transaction will be suspended , After call , The original transaction will resume execution
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Student and teacher data are submitted correctly . This indicates that these two operations have not been spring Manage and open transactions , Instead, local transactions are used , Since local transactions are automatically committed by default, all data are submitted successfully
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.REQUIRED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Student data submission failed , Teacher data submitted successfully . Explain that the student operation has started the transaction , The teacher did not open the transaction , Instead, local transactions are used
5.MANDATORY
This method can only be executed in an existing transaction , Business methods cannot initiate their own transactions . If it is called in an environment without transactions , The container throws an exception
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.REQUIRED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
After testing : Code error ,org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation ‘mandatory’, Transaction environment not found
6.NEVER
This method must not be executed within the scope of a transaction . If you're there, leave the exception . Only this method is not associated with any transactions , To execute normally
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.REQUIRED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.NEVER)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Code error , Because the students started the business , When calling the teacher method, its propagation characteristic is never, Therefore, a transaction error is reported :org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation ‘never’
7.NESTED
If an active transaction exists , Run in a nested transaction . If there are no active transactions , Then press REQUIRED Property execution . It uses a single transaction , This transaction has multiple savepoints that can be rolled back savePoint. Rollback of internal transaction will not affect external transaction . It's only right. DataSourceTransactionManager Transaction manager works ( need JDBC3.0 Above support )
Nested transactions : Inner transactions depend on outer transactions . When outer affairs fail , It will roll back the actions done by the inner layer transaction . The failure of inner transaction operation does not cause the rollback of outer transaction .
@Test
public void test{
addStudent();
}
@Transactional(propagation = Propagation.NESTED)
public void addStudent(){
String sql = "insert into student(name) values(" Wang Hua ")";
jdbcTemplate.execute(sql);
addTeacher();
throw new RuntimeException(); // Throw exceptions
}
@Transactional(propagation = Propagation.NESTED)
public void addTeacher(){
String sql = "insert into teacher(name) values (" Li Liang ")";
jdbcTemplate.execute(sql);
}
After testing : Code error , Teacher data and student data were not submitted successfully . State that it follows REQUIRED Characteristic operation . For nested transactions , You can simulate two data sources , The failure of one party will not affect the other
PROPAGATION_NESTED And PROPAGATION_REQUIRES_NEW difference
They are very similar , It's like a nested transaction , If there is no active transaction , Will open a new business .
- Use PROPAGATION_REQUIRES_NEW when , Inner and outer affairs are like two separate affairs , Once the inner transaction has been committed , Outer transactions cannot be rolled back . The two transactions do not affect each other . Two transactions are not a real nested transaction . At the same time, it needs JTA Transaction manager support .
- Use PROPAGATION_NESTED when , The rollback of outer transaction can cause the rollback of inner transaction . The exception of inner transaction does not cause the rollback of outer transaction , It's a real nested transaction .DataSourceTransactionManager Use savepoint Support PROPAGATION_NESTED when , need JDBC 3.0 The above drive and 1.4 The above JDK Versioning support . The rest of the JTA TrasactionManager Implementations can be supported in different ways .
PROPAGATION_REQUIRES_NEW Start a new , Not dependent on the environment “ Inside ” Business . This business will be completely commited or rolled back It doesn't depend on external affairs , It has its own quarantine area , My own lock , wait . When internal transactions begin to execute , External transactions will be suspended , At the end of the house business , External affairs will continue .
On the other hand , PROPAGATION_NESTED Start a “ Nested ” Business , It is a real sub transaction of an existing transaction . When a latent transaction begins to execute , It will get a savepoint. If this nested transaction fails , We'll roll back to savepoint. A hidden transaction is part of an external transaction , It will be committed only after the external transaction has ended .
thus it can be seen : PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED The biggest difference is , PROPAGATION_REQUIRES_NEW It's a whole new business , and PROPAGATION_NESTED External transactions are external transactions , If external affairs commit, Nested transactions will also be commit, This rule also applies to roll back.
Personal tasks : If transaction propagation is used , Most of them are overused transactions , ha-ha
边栏推荐
- 7-5 zhe zhe playing games
- redis学习记录:字典(dict)源码分析
- Storage R & D Engineer Recruitment
- [DDS] ddsi-rtps specification
- C#入门系列(十二) -- 字符串
- 2021-01-13
- Papaya Mobile: cross border marketing has entered the era of "information flow", allowing independent station sellers to share opportunities to go to sea
- 【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
- SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
- [Wayland] Wayland agreement description
猜你喜欢

Explanation of the principle of MySQL's leftmost matching principle

HALCON联合C#检测表面缺陷——仿射变换(三)

7-13 underground maze exploration (adjacency table)

Explication du principe d'appariement le plus à gauche de MySQL

哈希表的理论讲解

Basic use of scratch

【clickhouse专栏】基础数据类型说明

Transport layer protocol -- TCP protocol

MySQL 7 affair

JVM (III) Virtual machine performance monitoring & fault handling tool
随机推荐
【926. 将字符串翻转到单调递增】
Overview of software definition storage (one article is enough)
MySQL优化之慢日志查询
CEPH performance optimization and enhancement
004:aws data Lake solution
【系统分析师之路】第十八章 复盘系统安全分析与设计
FPGA VGA display based on de2-115 platform
Jetpack architecture component learning (3) -- activity results API usage
用于图像处理的高性能计算框架
C language recursive folder code
CentOS 7 installing MySQL 8
Common tree summary
FPGA基于DE2-115平台的VGA显示
C break continue return
7-13 地下迷宫探索(邻接表)
古董级MFC/GDI+框架LCD显示控件
Explication du principe d'appariement le plus à gauche de MySQL
C # getting started series (12) -- string
Pandorabox uses firewall rules to define non internet time
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)