当前位置:网站首页>@Detailed explanation of the use of transactional annotation
@Detailed explanation of the use of transactional annotation
2022-07-29 00:04:00 【Goose goose goose】
@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 ;
边栏推荐
- Type 1-5 components
- YOLOV5学习笔记(一)——原理概述
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- 【C】 Introduction and Simulation Implementation of ATOI and offsetof
- pycharm新建项目
- Word中的\n是什么?:^p
- 实时数仓:美团点评Flink的实时数仓应用分享
- 【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
- NAT如何配置地址转换
- Where is the DP interface of desktop computer (what if the host has no DP interface)
猜你喜欢

迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables

失败率高达80%,数字化转型如何正确完成战略规划?

【C】 Reverse string (two recursive ideas)

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

Pycharm configuring the running environment

curl (7) Failed connect to localhost8080; Connection refused

PowerCL 批量创建及管理虚拟交换机

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

How NAT configures address translation

Yolov5 learning notes (I) -- principle overview
随机推荐
Review of categories 1-4
基于 FPGA 实现数字时钟详细原理讲解及验证结果
迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
数据中台的那些“经验与陷阱”
SAP 临时表空间错误处理
Leetcode64. 最小路径和
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Powercl batch creates and manages virtual switches
Leetcode60. permutation sequence
EN 1935建筑五金.单轴铰链—CE认证
【C】喝汽水,找单身狗问题
Leetcode64. Minimum path sum
Leetcode59. 螺旋矩阵 II
SQL left connection, internal connection writing method "recommended collection"
EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
Summary of wrong questions of software designers
GhostNets on Heterogeneous Devices via Cheap Operations
E-commerce data model design
Leetcode61. 旋转链表
laptop外接显示器