当前位置:网站首页>@Detailed explanation of the use of transactional annotation
@Detailed explanation of the use of transactional annotation
2022-07-29 00:20:00 【Cardiac paranoia】
@Transactional
The scope of action of
- Method : It is recommended to use annotations on methods , But here's the thing : This annotation can only be applied to public On the way , Otherwise it doesn't work .
- class : If this annotation is used on a class , Indicates that the annotation is valid for all public All methods work .
- Interface : It is not recommended to use .
@Transactional
Summary of common configuration parameters of ( It only lists 5 I usually use it ):
Property name
explain
propagation
The spread of transactions , The default value is REQUIRED, Optional values are described above
isolation
The isolation level of the transaction , Default value adopts DEFAULT, Optional values are described above
timeout
Transaction timeout , The default value is -1( No timeout ). If the time limit is exceeded but the transaction has not completed , Then automatically roll back the transaction .
readOnly
Specifies whether the transaction is read-only , The default value is false.
rollbackFor
Used to specify the exception type that can trigger transaction rollback , And you can specify multiple exception types .
@Transactional
Transaction annotation principle
During the interview, I was asking AOP One of the questions that may be asked is . In a nutshell !
We know ,@Transactional
The working mechanism of is based on AOP Realized ,AOP It is also implemented by using dynamic proxy . If the target object implements the interface , By default... Is used JDK Dynamic proxy for , If the target object does not implement the interface , Will use CGLIB A dynamic proxy .
If a class or a class of public Methods are marked with @Transactional
Annotations of words ,Spring The container will create a proxy class for it at startup time , In the call by @Transactional
Annotated public Method time , What's actually called is ,TransactionInterceptor
Class invoke()
Method . The function of this method is to start the transaction before the target method , If an exception is encountered during the execution of the method, the transaction will be rolled back , After the method call completes, the transaction is committed .
TransactionInterceptor
Classinvoke()
The actual call inside the method isTransactionAspectSupport
ClassinvokeWithinTransaction()
Method .
Spring AOP Self call problem
If the others in the same category do not @Transactional
The internal calls of the annotated methods are @Transactional
Method of annotation , Yes @Transactional
Transactions of annotated methods will fail .
This is because Spring AOP
Agency , Because only when @Transactional
When annotated methods are called outside the class ,Spring Business management takes effect .
MyService
Class method1()
call method2()
It will lead to method2()
Transaction failure for .
@Service
public class MyService {
private void method1() {
method2();
//......
}
@Transactional
public void method2() {
//......
}
}
The solution is to avoid self calling or using AspectJ replace Spring AOP agent .
@Transactional
A summary of the precautions in the use of
@Transactional
Annotations only apply to public The transaction takes effect on the method , It is not recommended to use ;- Avoid calling in the same class
@Transactional
Method of annotation , This will cause the transaction to fail ; - The right settings
@Transactional
OfrollbackFor
andpropagation
attribute , Otherwise, the transaction may fail to roll back ; - By
@Transactional
The class of the annotated method must be Spring management , Otherwise it doesn't work ; - The underlying database must support transaction mechanism , Otherwise it doesn't work ;
边栏推荐
- MySQL的存储过程
- ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
- Exchange 2013 SSL certificate installation document
- Advanced area of attack and defense world web masters training www robots
- Attack and defense world web master advanced area PHP_ rce
- @Transactional 注解使用详解
- Leetcode64. Minimum path sum
- vulnhub:Sar
- 【C】 Drink soda and find a single dog
- 【C】 Reverse string (two recursive ideas)
猜你喜欢
Plato farm is expected to further expand its ecosystem through elephant swap
Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect
Laravel permission control
【C】 Introduction and Simulation Implementation of ATOI and offsetof
Leetcode64. Minimum path sum
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
The difference between {} and ${}
Kali installs burpsuite professional
vulnhub:BTRSys2
SQL实现将多行记录合并成一行
随机推荐
Advanced area of attack and defense world web masters unserialize3
研发效能的道法术器
mysql索引失效的常见9种原因详解
Samsung asset management (Hong Kong) launched yuancosmos ETF to focus on investing in the future tuyere track
NPM replace the latest Taobao image
vulnhub:BTRSys2
Why is it so difficult for the SEC to refuse the application for transferring gray-scale GBTC to spot ETF? What is the attraction of ETF transfer?
Eye of depth (18) -- partial derivative
Idea connection database
Event extraction and documentation (2008-2017)
Software designer afternoon question
1-8 basic use of props
Es6操作教程
Advanced area of attack and defense world web masters -baby Web
Type 1-5 components
动态规划问题(二)
Leetcode59. Spiral matrix II
Everything you have learned will come in handy at some point in your life (turn)
Compilation principle research study topic 2 -- recursive descent syntax analysis design principle and Implementation
AutoCAD -- import excel tables into CAD and merge CAD