Novel database deadlock detection method and system based on source code scanning

By using source code scanning and loop detection methods, database deadlocks are accurately identified and optimized, solving the problems of lag and misjudgment in existing deadlock detection technologies, and achieving efficient deadlock detection and repair.

CN121579231AInactive Publication Date: 2026-02-27ZHEJIANG UNIV OF SCI & TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511775103.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-28
Publication Date
2026-02-27
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Existing deadlock detection mechanisms rely on code execution, which can lead to false positives, false negatives, and delays, resulting in wasted resources and system performance instability issues.

Method used

By scanning the source code, tracing the function call chain, recording the SQL statements and operation sequence, and using the first and second loops to traverse the transaction table operations, deadlock situations can be accurately detected and optimized.

Benefits of technology

Efficiently detect deadlock issues during the code development phase, reduce resource waste and repair costs, and improve detection timeliness.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure FT_1
    Figure FT_1
  • Figure FT_2
    Figure FT_2
  • Figure FT_3
    Figure FT_3
Patent Text Reader

Abstract

The invention discloses a novel database deadlock detection method and system based on source code scanning, and relates to the field of database management.The method comprises the steps that source codes are scanned, and call chains of different functions in the source codes are tracked; for the call chain, identifying database operations involved in the function one by one according to the call sequence, recording the content and the operation sequence of the SQL statement, and storing all the scanned content into a transaction-table-table operation sequence table; reading the transaction-table-table operation sequence table, traversing all tables of all transactions and corresponding operation sequences based on the first cycle and the second cycle, searching for the situation that the operation sequences of the tables in different transactions are reversed, and creating a deadlock condition record table; and optimizing and adjusting the source code according to the deadlock condition record table. According to the method, the deadlock condition of the code can be effectively detected before the code falls to the ground, so that the timeliness of discovering the deadlock problem is further improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of database management, in particular to a novel database deadlock detection method and system based on source code scanning. BACKGROUND

[0002] Deadlock generally refers to a stalemate caused by two or more processes competing for resources during execution. When a process is in this state, they are all waiting for a process to release its occupied resources, but the waited process cannot release its occupied resources because it is waiting for other resources, resulting in an infinite loop of waiting. Deadlock not only blocks process execution, but also causes waste of system resources, seriously affecting system performance and stability. Therefore, detection and prevention of deadlock is an important part of system design and database management.

[0003] Currently, common deadlock detection mechanisms include resource allocation graph detection, waiting graph algorithm, log checking, etc. The implementation of these detection mechanisms mostly relies on the actual running of the code to complete, and the behavior of relying on code running to find deadlock not only has the possibility of misjudgment and omission, but also has serious lag in the discovery mechanism. These problems will eventually directly lead to a large amount of resource waste. SUMMARY

[0004] The purpose of the present application is to provide a novel database deadlock detection method and system based on source code scanning, which can effectively detect the deadlock condition of the code before it is put into use, further improving the timeliness of finding deadlock problems.

[0005] To achieve the above purpose, the present application provides the following solutions.

[0006] In a first aspect, the application provides a new database deadlock detection method based on source code scanning, which comprises: scanning the source code, and tracing the call chain of different functions in the source code; identifying the database operations involved in the functions in the call chain one by one in the order of calling, recording the content and operation order of the SQL statements, and storing all the scanning contents in a transaction-table-table operation order table; the transaction-table-table operation order table records the tables, table operations and table operation orders contained in different transactions; different transactions correspond to different transaction numbers, and different tables correspond to different operation order numbers; reading the transaction-table-table operation order table, traversing all the tables and corresponding operation orders of all transactions based on a first loop and a second loop, finding the operation order reversal of the tables in different transactions, and creating a deadlock condition record table; the first loop is used to query whether there is a transaction that also operates the current table of the current transaction in the remaining transactions; the second loop is used to determine whether the operation order of the current table in different transactions is consistent in a reverse order based on the transactions queried by the first loop; and the source code is optimized and adjusted according to the deadlock condition record table.

[0007] In a second aspect, the application also provides a computer system, which comprises a memory, a processor, and a computer program stored on the memory and executable on the processor, and the processor executes the computer program to implement the new database deadlock detection method based on source code scanning of the first aspect.

[0008] According to the specific embodiments provided by the application, the following technical effects are disclosed.

[0009] In the development stage of the code, the application effectively organizes the content related to the database operation into a transaction-table-table operation order table through scanning of the source code and tracing of the call chain, which facilitates efficient detection of the subsequent deadlock conditions. Meanwhile, the application also designs a first loop and a second loop process according to the deadlock characteristics, respectively queries from the transactions with the same table and the operation order of the table in different transactions to traverse all the operation tables of all transactions, so as to accurately find all the deadlock conditions contained in the source code and record them. Based on the above technical features, the detection method of the application can make the discovery mechanism of the deadlock no longer lag behind, and can maximize the reduction of the harm and repair cost caused by the database deadlock problem. BRIEF DESCRIPTION OF DRAWINGS

[0010] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related art, the accompanying drawings needed in the embodiments will be briefly introduced. Obviously, the accompanying drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other drawings can be obtained based on these drawings without creative labor.

[0011] Figure 1 Flow chart of the new database deadlock detection method based on source code scanning in the embodiments of the present application.

[0012] Figure 2 Flow chart of the source code transaction identification storage in the embodiments of the present application.

[0013] Figure 3 Flow chart of the deadlock detection judgment in the embodiments of the present application.

[0014] Figure 4 Internal structure diagram of the computer system in the embodiments of the present application. DETAILED DESCRIPTION

[0015] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the protection scope of the present application.

[0016] The purpose of the present application is to provide a new database deadlock detection method and system based on source code scanning, which can effectively detect the deadlock condition of the code before it is implemented, and further improve the timeliness of discovering deadlock problems.

[0017] In order to make the above-mentioned purposes, features and advantages of the present application more apparent and easy to understand, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0018] In an exemplary embodiment, a new database deadlock detection method based on source code scanning is provided, as shown in Figure 1 The new database deadlock detection method based on source code scanning includes the following steps.

[0019] Step S1: scanning the source code and tracing the call chain of different functions in the source code.

[0020] In this embodiment, the service list at the design time of the source code is obtained; and the call chain of different functions (or algorithms) is analyzed for each service according to the service list (and saved to a data object).

[0021] Step S2: For the call chain, identify the database operations involved in each function in the call order, record the content and operation order of the SQL statements, and store the scanned content in the transaction-table-table operation order table.

[0022] In this embodiment, for each service call chain, each function on the call chain is analyzed in sequence, the structured query language (SQL) in each function (or algorithm) is parsed, each SQL statement is sequentially numbered according to the reading identification order of the call chain, and a data entity object is created to save the identified SQL statement; after the data entity object is created, the scanned content is stored in the "transaction-table-table operation order table".

[0023] Among them, the key attributes in the data entity object include the serial number (which is monotonically increasing according to the reading order), the table name, the transaction ID, the transaction operation order number, etc.; the "transaction-table-table operation order table" records the tables, table operations and table operation orders contained in different transactions; different transactions correspond to different transaction numbers, and different tables correspond to different operation order numbers; since the tables and table operations in different transactions are in a one-to-one correspondence, the table, table operation and table operation order can share a number (i.e. the operation order number of the table) when stored, which can simplify the query logic and make the subsequent loop check only involve two numbers, i.e. the transaction number and the operation order number.

[0024] Step S3: Read the transaction-table-table operation order table, traverse all tables and corresponding operation orders of all transactions based on the first loop and the second loop, find the operation order reversal of the tables in different transactions, and create a deadlock record table.

[0025] In this embodiment, the first loop is used to query whether there is a transaction that also operates on the current table of the current transaction in the remaining transactions; the second loop is used to determine whether the operation order of the current table in different transactions is consistent in reverse order based on the transactions queried by the first loop. The essence of the database deadlock logic is that two tables in different transactions have opposite operation orders, causing mutual waiting and thus falling into a permanently blocked state. Based on this, by obtaining the operation content from the source code, two loops are designed to traverse all operation tables and operation orders of all transactions, and if the operation order of the tables in two transactions is reversed, it proves that a deadlock has occurred.

[0026] Taking transaction Tn (n is the transaction number) as an example, when checking Tnm (m is the operation sequence number of the table), first, the remaining transactions are queried to observe whether there is a transaction that also has an operation on the m table; when Txm is found, all tables in the transaction Tx are queried in reverse order based on the operation sequence of the m table in the transaction Tx, to observe whether the table name with an operation sequence less than m in Tx is the same as the table name with an operation sequence after m in Tn, if so, it is proved that in Tn and Tx, there is exactly opposite operation sequence between the two tables, that is, a deadlock situation occurs.

[0027] Specifically, the first loop process is as follows.

[0028] Read m transactions; start from transaction Ta (a is the transaction number), query in turn according to the table operation sequence number; find whether there is a table in the remaining transaction table that is the same as Tax (x is the operation sequence number of the table); if so, continue to execute the second loop; if not, update Tax, so that x+1 in Tax, until x=n (n is the number of tables contained in the current transaction).

[0029] The second loop process is as follows.

[0030] According to the table operation sequence number, find whether there is a table in the transaction queried in the first loop that is the same as Tay (x

[0031] As can be seen, the first loop and the second loop are in a nested relationship, which can effectively traverse all tables in all transactions, and further realize detection of all deadlock situations in the source code.

[0032] Step S4: According to the deadlock situation record table, the source code is optimized and adjusted.

[0033] In this embodiment, the deadlock situation record table records the transaction number, table name and corresponding operation sequence number of the transaction number, table name and corresponding operation sequence number of the transaction number. The programmer can adjust the function / algorithm according to the table to solve the deadlock problem in the source code until there is no problem of inconsistent operation sequence of tables in different transactions.

[0034] Based on the above analysis, the embodiment analyzes the source code, extracts transaction statements, and comprehensively analyzes the deadlock situation of the source code through the built-in judgment logic, repairs and avoids the deadlock before the code is landed, further improves the timeliness of finding the deadlock problem, and effectively reduces the harm degree caused by the deadlock and the solution cost required for later repair.

[0035] In another exemplary embodiment, in order to verify the effectiveness of the new database deadlock detection method based on source code scanning, the embodiment provides an actual application scenario of the new database deadlock detection method based on source code scanning.

[0036] As shown in Figure 2 and Figure 3 , the new database deadlock detection method based on source code scanning mainly includes two parts of source code transaction identification storage and deadlock detection judgment. As shown in Figure 2 , the source code transaction identification storage process is as follows.

[0037] (1) Scan the source code in the development stage: read all the code content to be detected.

[0038] (2) Trace the call chain of functions / algorithms: locate the target code by tracing the call chain.

[0039] (3) Identify the database operations involved in the functions / algorithms in the call chain in the order of calling: identify and judge the code involving database operations, and retain the code related to database operations.

[0040] (4) Give an incremental number to the identified database operation language (data manipulation language, dml), and record the table name and SQL statement: sequentially number according to the reading identification order of the call chain.

[0041] (5) Number the transactions, and the writing method of the number is as follows: the nth transaction can be represented as Tn; number the table operation order as n, and the nth table in the Tn transaction can be represented as Tnn.

[0042] (6) Identify the start of the transaction, and record the operation order number as 1, and the subsequent operation order number is incremented by 1, repeat this step until all statements are identified and numbered. In the numbering process of the transaction and the table operation order, all numbered contents are stored in the “transaction, table and table operation order table”.

[0043] (7) Identify the end of the transaction: at this time, the source code has been scanned, and all standby transactions and tables, table operations, and operation order numbers have been stored in the “transaction, table and table operation order table” as follows.

[0044] Table 1 Transaction, Table and Table Operation Order Table

[0045] At this point, Figure 2 all steps contained in

[0046] Next, enter the deadlock detection decision flow, as shown in Figure 3 The flow is as follows.

[0047] (1) Read "transaction, table and table operation sequence table": read the data content obtained in Figure 2 .

[0048] (2) Read m transactions, i.e. T1, T2, T3...Tm: read the transaction data to determine the existence of deadlock.

[0049] (3) Check the operation sequence number from transaction Ta (initial a = 1, repeat a + 1 until x = m) according to the operation sequence number, i.e. Ta1, Ta2, Ta3...Tan: according to the operation sequence number, check the transaction in order to ensure that all transaction contents are traversed and no deadlock problem is missed.

[0050] (4) Find out if there is a table name in the remaining all transactions that is the same as Tax (initial x = 1, repeat x + 1 until x = n): determine whether there is one here, and then decide whether to enter the next operation or return to the loop in (3) according to the result.

[0051] (5) According to the operation sequence number of the table, find out if there is a table in this transaction that is the same as Tay (x < y ≤ n, y is a positive integer): if there is a same table name, it proves that there are different operation sequences of two tables in different transactions, i.e. deadlock occurs, which is recorded in the following table; if there is no same table name, it proves that there is no sequence inversion of two tables in different transactions, i.e. no deadlock.

[0052] Table 2 deadlock situation record table

[0053] (6) Repeat (4) and (5) above until the last table Tan in transaction Ta and its operation sequence number are checked.

[0054] (7) Repeat (4), (5) and (6) above until the last transaction Tm is judged.

[0055] (8) End of judgment: at this time, all deadlock situations have been stored in the "deadlock situation record table".

[0056] As described above, the present application comprehensively detects the deadlock situation at the source code level, has high timeliness in the problem discovery level, and can effectively reduce the resource waste and high solution cost problems caused by the lag of the traditional deadlock detection scheme.

[0057] In another exemplary embodiment, a computer system, which can be a server or a terminal, is provided, and an internal structure diagram of the computer system can be as shown in Figure 4 The computer system includes a processor, a memory, an input / output interface (I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer system is configured to provide computing and control capabilities. The memory of the computer system includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for running the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer system is configured to exchange information between the processor and external devices. The communication interface of the computer system is configured to communicate with external terminals through a network connection. The computer program is executed by the processor to implement the above-mentioned novel database deadlock detection method based on source code scanning.

[0058] Those skilled in the art can understand that Figure 4 The structure shown in the above-mentioned embodiment is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer system to which the scheme of the present application is applied. The specific computer system can include more or less components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0059] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of the related data need to comply with relevant regulations.

[0060] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium, and when the computer program is executed, the processes of the above-mentioned embodiments of the methods can be included. Any reference to memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical storage, high-density embedded non-volatile memory, resistive memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration but not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.

[0061] The database involved in the embodiments provided in the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a blockchain, etc., without being limited thereto. The processor involved in the embodiments provided in the present application can be a general processor, a central processor, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.

[0062] The embodiments in the present specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other.

[0063] The principles and implementation modes of the present application are described by applying specific examples in the present text. The above description of the embodiments is only used to help understand the method of the present application and its core idea; at the same time, for those skilled in the art, according to the idea of the present application, the specific implementation mode and application range will be changed. In conclusion, the content of the present specification should not be understood as a limitation of the present application.

Claims

1. A novel database deadlock detection method based on source code scanning, characterized in that, The new database deadlock detection method based on source code scanning comprises: scanning the source code, tracing the call chain of different functions in the source code; for the call chain, identifying the database operation involved in each function in turn according to the calling order, recording the content and operation order of the SQL statement, and storing all the scanned contents in a transaction-table-table operation order table; the transaction-table-table operation order table records the tables, table operations and table operation orders contained in different transactions; different transactions correspond to different transaction numbers, and different tables correspond to different operation order numbers; reading the transaction-table-table operation order table, traversing all the tables and corresponding operation orders of all transactions based on a first loop and a second loop, finding the operation order reversal of the tables in different transactions, and creating a deadlock condition record table; the first loop is used to query whether there is a transaction that also operates the current table of the current transaction in the remaining transactions; the second loop is used to determine whether the operation order of the current table in different transactions is consistent in reverse order based on the transaction queried in the first loop; optimizing and adjusting the source code according to the deadlock condition record table.

2. The novel database deadlock detection method based on source code scanning according to claim 1, characterized in that, Tracing the call chain of different functions in the source code specifically comprises: obtaining the service list when the source code is designed; analyzing the call chain of different functions for each service according to the service list.

3. The novel database deadlock detection method based on source code scanning according to claim 2, characterized in that, For the call chain, identifying the database operation involved in each function in turn according to the calling order, recording the content and operation order of the SQL statement, and storing all the scanned contents in a transaction-table-table operation order table, specifically comprising: for the call chain of each service, sequentially analyze each function on the call chain, parse the SQL statement in each function, sequentially number each SQL statement according to the reading order of the call chain, and create a data entity object to save the identified SQL statement; after the data entity object is created, store all the scanned contents in the transaction-table-table operation order table.

4. The novel database deadlock detection method based on source code scanning according to claim 3, characterized in that, The key attributes in the data entity object at least include the sequential number, table name and transaction ID.

5. The novel database deadlock detection method based on source code scanning according to claim 1, characterized in that, The tables and table operations in different transactions are in a one-to-one correspondence, and the tables, table operations and table operation orders can share a number when stored.

6. The novel database deadlock detection method based on source code scanning according to claim 1, characterized in that, The first loop specifically comprises: reading m transactions; starting from transaction Ta, sequentially querying according to the table operation order number; a is the transaction number; finding whether there is a table in the remaining transaction tables that is the same as Tax; x is the table operation order number; if yes, continue to execute the second loop; if no, update Tax so that x+1 in Tax, until x=n; n is the number of tables contained in the current transaction.

7. The novel database deadlock detection method based on source code scanning according to claim 6, characterized in that, The second loop specifically comprises: according to the table operation order number, inversely find whether there is a table in the transaction queried in the first loop that is the same as Tay; x if yes, save the same table to the deadlock condition record table, and update Tax so that x+1 in Tax, until x=n; If not, it is determined that the transaction Ta does not have a deadlock condition, and the transaction Ta is updated to make a+1 in Ta until a=m.

8. The novel database deadlock detection method based on source code scanning according to claim 1, characterized in that, The deadlock condition record table records the transaction number, table name and corresponding operation sequence number of the transaction having the deadlock condition according to the deadlock serial number.

9. The novel database deadlock detection method based on source code scanning according to claim 8, characterized in that, The deadlock condition refers to the state that two tables in different transactions are in a permanent blocking state due to opposite operation sequences.

10. A computer system comprising: Memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that the processor executes the computer program to implement the novel database deadlock detection method based on source code scanning according to any one of claims 1-9.