当前位置:网站首页>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
边栏推荐
- Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
- First NFT platform in dfinity Ecology: impossible thoughts
- Reading notes of the fifth cultivation
- Common tree summary
- markdown_ Picture side by side scheme
- Explication du principe d'appariement le plus à gauche de MySQL
- How to do industry analysis
- 极速搭建元宇宙画廊 #oncyber.io
- Introduction to on-line circuit simulation and open source electronic hardware design
- Pandorabox uses firewall rules to define non internet time
猜你喜欢

tp6调试(trace)

Tp6 debugging (trace)

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000

【926. 将字符串翻转到单调递增】

Halcon combined with C # to detect surface defects -- affine transformation (III)
![[cloud native | kubernetes] kubernetes networkpolicy](/img/8b/9260fc39d3f595cdc2689a3ab26bd7.png)
[cloud native | kubernetes] kubernetes networkpolicy

How to do industry analysis

在线电路仿真以及开源电子硬件设计介绍

行业分析怎么做
随机推荐
Jetpack架构组件学习(3)——Activity Results API使用
tp6调试(trace)
Li Yang, a scientific and technological innovator and CIO of the world's top 500 group: the success of digital transformation depends on people. Decision makers should always focus on "firewood"
C break continue return
2022 pole technology communication - the dispute over anmou technology is settled, and the cornerstone of the local semiconductor industry is more stable
Canal ha mode configuration
2022 pole technology communication - anmou technology ushers in new opportunities for development
Docker compose integrates redis, MySQL and microservices, and services are containerized
True north reading notes
GNU EFI development environment settings
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
7-5 zhe zhe playing games
FPGA VGA display based on de2-115 platform
IoT简介
OpenCV中CLAHE用于16位图像增强显示
Auto. JS debugging: use the network mode of lightning simulator for debugging
六月集训(第12天) —— 链表
[cloud native | kubernetes] kubernetes networkpolicy
[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)
Reading notes of the fifth cultivation