当前位置:网站首页>@Transitional step pit
@Transitional step pit
2022-07-02 07:17:00 【Software development heart】
List of articles
Step on the pit 1:@Transational LifeI writes DB Too much logic
@Transational The simplest and crudest way to use it is in a public Add this annotation to the method , Then I began to write hundreds of thousands of lines of code , Besides DB Write the operation code , It may also include interfaces / Method input parameter check 、 External system interface call 、 Business logic 、 Data calculation 、 Set conversion and other logic .
Write like this , Theoretically, there is no big problem , But in most cases, the writing is not really implemented until the last part DB The operation of , Only then do you need @Transational, And start the transaction at the beginning of the method , There are probably the following 2 In this case :
- The program has not been executed to write DB Logic , Just return 了 , here @Transational wasted .
- Program execution to writing DB Logical time , Experience too long , Lead to write DB Failure .
among , The first 2 This kind of situation is the pit that the author stepped on , Listen to the following explanation .
Step on the pit to restore
Add @Transational In the annotation method , First, the external system interface is queried , Write again DB(MySQL 5.7) The operation of .
One day , This method prints a line error journal , The contents are as follows :
Add @Transational Annotation is to rollback after throwing exceptions , It can also rollback failures ? So I checked more logs , The contents are as follows :
among ,Connection timed out The log is printed by querying the external system interface .
Search for Communications link failure( Reference resources :MySql Of Communications link failure terms of settlement ), Reason for the error :MySQL The service was disconnected after a long period of inactivity , The first request after disconnection will throw this exception , It says MySQL Two system variables of ,interactive_timeout and wait_timeout, The author found that MySQL the 2 All values are 120s, And log printing The last packet successfully received from the server was 127261 ms ago The time coincides . Besides , The difference between the logs before and after the print interface timeout is also 2 minute ( See the picture below ).
Trample hole analysis
The truth basically came out , After opening the transaction , The program calls the external system interface 2 Minutes out , Then write DB operation , More than the MySQL Of interactive_timeout/wait_timeout, Write DB Failure .
Pit verification
The verification code is as follows :
@Transactional(rollbackFor = Exception.class)
public void test1() {
// Write DB、 Throw exceptions
insertAndThrowException();
}
@Transactional(rollbackFor = Exception.class)
public void test2() {
// Let the program sleep first 121 second
log.error("begin sleep...");
try {
TimeUnit.SECONDS.sleep(121L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.error("wake up...");
// Write DB、 Throw exceptions
insertAndThrowException();
}
private void insertAndThrowException() {
// Write DB
insert();
// Throw exceptions
System.out.println(1 / 0);
}
private void insert() {
// insert into...
}
test1 In execution System.out.println(1 / 0) When the ArithmeticException, And insert The operation is rolled back , The log is as follows :
test2 In execution insert() Throw out com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure, Not implemented to System.out.println(1 / 0), The log is as follows :
Optimization Suggestions
Need to add @Transational The logic of annotation , Write non first DB The logic of the operation is finished , Call plus @Transational Method of annotation , In addition to writing DB The logic of , Better not do anything .
Step on the pit 2: The abstract class calls the... Of the concrete class public Method ,@Transational Note invalid
as everyone knows ,@Transational Notes need to be added public Method , And is called by an external class , To take effect .
Step on the pit to restore
As shown in the following code ,@Transational Annotations are invalid , The reason is that when an abstract parent class calls a concrete subclass method , Is an internal call , Not external calls , The code is as follows :
@Service
public class TestService {
@Autowired
private AbstractClass abstractClass;
public void test() {
// A That's ok
abstractClass.parent();
}
}
public abstract class AbstractClass {
public void parent() {
// B That's ok
child();
}
abstract void child();
}
@Component
@Primary
public class ChildClass extends AbstractClass {
@Override
@Transactional(rollbackFor = Exception.class)
public void child() {
// C That's ok
// Write DB、 Throw exceptions
insertAndThrowException();
}
private void insertAndThrowException() {
// Write DB
insert();
// Throw exceptions
System.out.println(1 / 0);
}
private void insert() {
// insert into...
}
}
Trample hole analysis
At first glance ,@Transational Comments are added public Method , It is also called by external classes , But the parent class calling the subclass method should actually belong to the internal call , And method internal calls @Transational Annotated public The method will fail .
adopt debuug You can also find , perform A When you go, you will enter first CglibAopProxy agent , Re execution B That's ok , but B Do it C Line time , Did not enter the agent .
In execution System.out.println(1 / 0) When the ArithmeticException, but insert The operation was not rolled back , The log is as follows :
Solution & Optimization Suggestions
take @Transational Annotation plus AbstractClass.parent() Instead of ChildClass.child() To solve , But it may violate the original intention of the code structure .
It is suggested that subclasses call other classes @Transational annotation public Method , Or abandon the structure of parent-child classes .
Other pits
hear @Transational There will be a hole when you touch the lock , Interested students can have a look When Transactional Touch lock , There's a big hole !
Summary
@Transational Use with caution !!!
author : Manning
边栏推荐
- A summary of a middle-aged programmer's study of modern Chinese history
- Principle analysis of spark
- Thinkphp5中一个字段对应多个模糊查询
- Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
- 【信息检索导论】第七章搜索系统中的评分计算
- php中计算树状结构数据中的合计
- ORACLE EBS ADI 开发步骤
- PM2 simple use and daemon
- Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
- Sqli-labs customs clearance (less2-less5)
猜你喜欢
Oracle EBs and apex integrated login and principle analysis
读《敏捷整洁之道:回归本源》后感
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
CSRF attack
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
Changes in foreign currency bookkeeping and revaluation general ledger balance table (Part 2)
Sqli - Labs Clearance (less6 - less14)
Sqli-labs customs clearance (less6-less14)
@Transational踩坑
随机推荐
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
CRP implementation methodology
读《敏捷整洁之道:回归本源》后感
Analysis of MapReduce and yarn principles
sqli-labs通關匯總-page2
CAD secondary development object
使用MAME32K进行联机游戏
Sqli labs customs clearance summary-page1
使用Matlab实现:Jacobi、Gauss-Seidel迭代
2021-07-19c CAD secondary development creates multiple line segments
中年人的认知科普
2021-07-05C#/CAD二次开发创建圆弧(4)
ORACLE EBS 和 APEX 集成登录及原理分析
矩阵的Jordan分解实例
ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用
Basic knowledge of software testing
Three principles of architecture design
Yaml file of ingress controller 0.47.0
oracle apex ajax process + dy 校验
ORACLE 11G利用 ORDS+pljson来实现json_table 效果