当前位置:网站首页>@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 .
TransactionInterceptorClassinvoke()The actual call inside the method isTransactionAspectSupportClassinvokeWithinTransaction()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
@TransactionalAnnotations only apply to public The transaction takes effect on the method , It is not recommended to use ;- Avoid calling in the same class
@TransactionalMethod of annotation , This will cause the transaction to fail ; - The right settings
@TransactionalOfrollbackForandpropagationattribute , Otherwise, the transaction may fail to roll back ; - By
@TransactionalThe 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 ;
边栏推荐
- [small bug diary] Navicat failed to connect to MySQL | MySQL service disappeared | mysqld installation failed (this application cannot run on your computer)
- "Method not allowed", 405 problem analysis and solution
- Review of categories 1-4
- Table custom style row class name in elemenui
- Develop effective Tao spell
- Geth installation
- Redis learning notes
- PHP语言基础知识(超详细)
- curl (7) Failed connect to localhost8080; Connection refused
- Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect
猜你喜欢

Compilation principle research study topic 2 -- recursive descent syntax analysis design principle and Implementation

Detailed explanation of 9 common reasons for MySQL index failure

Sword finger offer 41. median in data flow

Attack and defense world web master advanced area web_ php_ include

Leetcode64. Minimum path sum

【微服务~Nacos】Nacos服务提供者和服务消费者

Attack and defense world web master advanced area PHP_ rce

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

Event extraction and documentation (2008-2017)

Okaleido ecological core equity Oka, all in fusion mining mode
随机推荐
Advanced area of attack and defense world web masters training www robots
Event extraction and documentation (2018)
Interpretation of ISO 13400 (doip) standard
ES6 operation tutorial
mysql中exists的用法详解
Real time data warehouse: meituan reviews Flink's real-time data warehouse application sharing
Laravel permission control
Sword finger offer 41. median in data flow
[applet project development -- JD mall] uni app commodity classification page (first)
Applet editor rich text editing and rich text parsing
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
MySQL installation and configuration tutorial (super detailed, nanny level)
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
PHP语言基础知识(超详细)
What is in word?:^ p
Attack and defense world web master advanced area php2
递归/回溯刷题(下)
Cmake basic learning
@Detailed explanation of the use of transactional annotation
Develop effective Tao spell