WO2010139260A1 - 数据库事务锁机制的死锁检测方法及装置 - Google Patents

数据库事务锁机制的死锁检测方法及装置 Download PDF

Info

Publication number
WO2010139260A1
WO2010139260A1 PCT/CN2010/073392 CN2010073392W WO2010139260A1 WO 2010139260 A1 WO2010139260 A1 WO 2010139260A1 CN 2010073392 W CN2010073392 W CN 2010073392W WO 2010139260 A1 WO2010139260 A1 WO 2010139260A1
Authority
WO
WIPO (PCT)
Prior art keywords
thread
lock
deadlock
adjacency matrix
information
Prior art date
Application number
PCT/CN2010/073392
Other languages
English (en)
French (fr)
Inventor
陈河堆
常二鹏
卢勤元
Original Assignee
中兴通讯股份有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 中兴通讯股份有限公司 filed Critical 中兴通讯股份有限公司
Priority to EP10782964.0A priority Critical patent/EP2439640A4/en
Priority to BRPI1011087A priority patent/BRPI1011087A2/pt
Publication of WO2010139260A1 publication Critical patent/WO2010139260A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2308Concurrency control
    • G06F16/2336Pessimistic concurrency control approaches, e.g. locking or multiple versions without time stamps

Definitions

  • the present invention relates to the field of database technologies, and in particular, to a deadlock detection method and apparatus for a database transaction lock mechanism. Background technique
  • Transaction is one of the basic and most important functions provided by the database management system (DBMS), and the implementation of transaction functions depends on two core infrastructure technologies: lock mechanism and REDO/UNDO log function.
  • lock mechanism is the core technology of transaction implementation. It is related to whether the transaction function can be realized, the performance and throughput of the system, and the stability of the system.
  • Deadlock detection is a key technology in the lock mechanism. Although some methods can be used to avoid and reduce deadlocks in certain scenarios, deadlocks are sometimes difficult to avoid, so deadlock detection is an integral part of the lock mechanism. Deadlock detection requires a lot of computing resources, and its efficiency has a significant impact on the overall performance of the system.
  • the deadlock detection method of MS SQL Server 2008 is as follows: When the lock monitor initiates a deadlock search for a particular thread, it identifies the resource that the thread is waiting for. The lock monitor then looks up the owner of the particular resource and recursively continues the deadlock search for those threads until a loop is found. Loops identified in this way form a deadlock. This is a post hoc test.
  • InnoDB's deadlock detection also has a similar recursive algorithm, that is, transaction T1 (requestor thread) locks an object obj (table or record), when it needs to wait, first create A lock object wait_lock, and then test if a deadlock occurs beforehand. It calls a self-recursive function lock-deadlock-recursive (Tl, wait-lock) to indicate whether the locker (transaction) before the wait-lock waits for Tl.
  • Tl self-recursive function lock-deadlock-recursive
  • the specific deadlock detection algorithm is as follows: 1) If the previous lock added to the object obj does not exist, there is no deadlock and exit.
  • Both MS SQL Server and InnoDB use recursive methods to detect deadlocks, especially InnoDB, which uses a doubly linked list to organize the list of lock objects that the protected object has granted, and a list of lock objects owned by the transaction.
  • the deadlock detection algorithm traverses these linked lists to detect Wait for the ring to exist. This method is less efficient, and only one wait loop can be detected in one test, and the detection process has no small hindrance to the ongoing thread (transaction).
  • the present invention is directed to a deadlock detection method and apparatus for a database transaction lock mechanism, which solves the problem of tedious deadlock detection and waste of computing resources existing in the prior art.
  • the invention provides a deadlock detection method for a database transaction lock mechanism, which is preset to store an adjacency matrix of waiting relationship information between threads; the method includes:
  • Step A The locking thread generates the inter-thread waiting relationship information generated when the lock is locked in the neighbor In the matrix;
  • Step B The unlocking thread updates the corresponding waiting relationship information in the adjacency matrix as needed when unlocking;
  • Step C The deadlock detection thread performs detection and calculation on the thread according to the adjacency matrix and the AOV network principle, and determines whether there is a deadlock.
  • step A specifically includes:
  • the lock thread waits for the wait relationship information of other threads to be recorded.
  • the lock thread hangs waiting for the relevant thread to unlock.
  • the related thread refers to: all threads that have been granted this lock, in other words, those threads that have applied for locking successfully.
  • step B specifically includes:
  • Step B1 When the unlocking thread releases the lock a that was previously granted, it checks each thread t recorded in the adjacency matrix to apply for a lock, and determines whether the lock type to be applied by the thread t and the lock owned by the unlocking thread Type compatible, if not, proceed to step B2; if compatible, perform step B3;
  • Step B2 Keep the waiting relationship information unchanged; end the current process
  • Step B3 Continue to determine whether the lock type to be applied by the thread t is incompatible with the lock type owned by at least one other thread, and if yes, perform a lock wait relationship migration, and update the corresponding wait relationship information in the adjacency matrix; otherwise , only clear the original inter-thread wait relationship information; end the current process.
  • step C specifically includes:
  • Step C1 A deadlock detection thread copies a backup of the adjacency matrix
  • Step C2 Perform detection detection on the thread according to the inter-thread waiting relationship information in the backup adjacency matrix and the topology sorting manner in the AOV network principle, and detect whether there is a dead between the threads.
  • the lock loop when present, selects a thread in the deadlock loop as a victim thread according to a predetermined policy, and the victim thread fails to return, and the deadlock state is broken.
  • step C2 specifically includes:
  • Step C21 Calculate an in-order array and an out-out array of the adjacency matrix, and determine whether the in-order array is equal to the out-of-order array. If yes, execute step C22; otherwise, execute step C23; step C22, whether the in-degree array is 0 vector, if yes, the calculation ends; otherwise, a thread is selected as the victim thread in the deadlock loop according to a predetermined strategy, and the victim thread fails to return;
  • Step C23 Clear all elements whose degree of exit is not 0 and the degree of entry is 0, and go to step C21.
  • the embodiment of the present invention further provides a deadlock detection apparatus for a database transaction lock mechanism, and the apparatus includes: an information storage module, a deadlock detection module, an information recording module and an information update module included in each thread;
  • the information recording module is configured to record the inter-thread waiting relationship information generated when the lock is locked into the information storage module;
  • the information update module is configured to update corresponding waiting relationship information in the adjacency matrix as needed when unlocking;
  • the information storage module is configured to save inter-thread waiting relationship information in a preset adjacency matrix
  • the deadlock detection module is configured to detect and calculate a thread according to the inter-thread waiting relationship information and the AOV network principle stored in the information storage module, and determine whether there is a deadlock.
  • the information recording module is further configured to: when the lock is applied, when the lock type to be applied by the lock thread is incompatible with the lock type that the lock object has been granted to other threads, the information record module The locking thread waits for the waiting relationship information of other threads to be recorded in the information storage module, and then suspends waiting for the relevant thread to unlock.
  • the information update module is further configured to: when the related thread performs unlocking, Determining whether the type of the lock to be applied for each thread to be applied for the current lock in the information storage module is compatible with the lock type owned by the unlocked thread; if the lock is incompatible, the wait relationship information remains unchanged; And continuing to determine whether the lock type to be applied by each thread currently locked by the application is incompatible with the lock type owned by at least one other thread, and if the incompatibility is performed, performing a lock wait relationship migration, updating the adjacency matrix The corresponding waiting relationship information; when compatible, only the original inter-thread waiting relationship information is cleared.
  • the deadlock detection module is further configured to copy the adjacency matrix in the information storage module as a backup, according to the inter-thread waiting relationship information in the backup adjacency matrix and the topological sorting manner in the AOV network principle. Performing detection and calculation on the thread, detecting whether there is a deadlock loop between the threads, and confirming that there is a deadlock loop, selecting one thread as the victim thread in the deadlock loop according to a predetermined policy, sacrificing the thread failure return, breaking the deadlock state .
  • the technical solution proposed by the invention has a very high deadlock detection speed, and can fully utilize the useful information obtained by the unlocking process to help the subsequent detection of deadlock, thereby saving computing resources.
  • FIG. 1 is a schematic flowchart of a main body of a deadlock detecting method according to an embodiment of the present invention
  • FIG. 2 is a schematic flowchart of a deadlock detection information update in a method according to an embodiment of the present invention
  • FIG. 3 is a schematic flowchart of a deadlock detection algorithm in a method according to an embodiment of the present invention
  • FIG. 5 is a schematic diagram of the method according to the embodiment of the present invention, where the algorithm described in FIG. 3 is used to detect a deadlock neighbor. State diagram of the matrix;
  • FIG. 6 is a state diagram of a method for detecting a deadlocked adjacency matrix by using the algorithm described in FIG. 3 in the method according to the embodiment of the present invention
  • FIG. 7 is a schematic structural diagram of an apparatus according to an embodiment of the present invention.
  • the thread generates and maintains deadlock detection information during the locking and unlocking process, and then uses the deadlock detection information and the AOV network principle to perform topology sorting on the thread to determine whether there is a deadlock.
  • FIG. 1 is a schematic diagram of a main flow of a deadlock detection method according to an embodiment of the present invention, which mainly includes two phases, namely: collection of deadlock detection information and detection of deadlock status by using deadlock detection information.
  • the deadlock detection information refers to waiting relationship information between threads, and the waiting relationship information is all stored in an adjacency matrix called aov, which records waiting relationship information between threads.
  • the locking thread is responsible for generating the waiting relationship information between the threads
  • the unlocking thread is responsible for updating the waiting relationship information between the threads as needed during the unlocking process
  • the deadlock detecting thread is responsible for the waiting relationship information collected according to the adding and unlocking process, and utilizing the aov network principle. , topologically sorting threads to determine if there is a deadlock, and the existence of a loop waiting loop.
  • Step 101 During the locking process, if the locking thread needs to wait for other threads, the waiting relationship information between the threads is recorded in the adjacency matrix aov.
  • Step 102 During the unlocking process, the unlocking thread updates the corresponding data in the aov adjacency matrix by updating the waiting relationship information between the threads as needed.
  • Step 103 The deadlock detection thread first copies the aov adjacency matrix (the aov adjacency matrix constitutes one A backup of the directed graph G), then the aov network topology sorting method is used to calculate the backup data, and find the loop waiting loop (deadlock loop): remove all the nodes without the precursor and all the arcs it emits. If all nodes can be removed, it means that no deadlock occurs, and the detection ends; otherwise, there is a deadlock loop.
  • the predetermined strategy for example, it can be formulated according to the user's needs, such as the minimum cost strategy, that is, the thread with the smallest workload is selected.
  • deadlock detection can be performed periodically by a background independent thread (deadlock detection thread) (post-detection), or it can be detected immediately (pre-detection) during the locking process.
  • deadlock detection thread post-detection
  • pre-detection pre-detection
  • the aov network is mainly used to evaluate the feasibility of the project schedule. It uses vertices to represent activities and arcs to indicate priority relationships between activities. The main reason is to use the characteristics of the aov network as a powerful tool for deadlock detection: 1) The arc in the aov network indicates a certain constraint relationship between activities; 2) The loop cannot occur in the aov network.
  • the present invention uses a vertex to represent the thread itself, and an arc to indicate a waiting relationship between threads. When there is no loop in the aov network, it indicates that there is no deadlock, otherwise there is at least one deadlock.
  • the process of generating deadlock detection information (ie, step 101) specifically includes:
  • Thread i applies for locking a certain type of lock on the object lm, when the type lock and lm have been granted to the thread j
  • the lock type is incompatible, it indicates that the thread i needs to wait for the thread j to release the lock of the corresponding type in order to successfully obtain the lock type of the application.
  • there is a partial order relationship (waiting relationship) between thread i and thread j which is represented by an arc of node i to node j in the directed graph, and aov in the aov adjacency matrix.
  • i] j] The unit records a pointer to the notification event event that thread i is waiting for.
  • FIG. 2 is a schematic flowchart of a deadlock detection information update (step 102) in the method according to the embodiment of the present invention, which may specifically include:
  • Step 201 Thread j checks the first thread t waiting for the lock recorded in the jth column of the aov adjacency matrix during the process of unlocking a lock object lm.
  • Step 202 Determine whether the type of the lock to be applied by the thread t is compatible with the type of the lock owned by the thread j. If yes, go to step 203; otherwise, go to step 205.
  • Step 203 Determine whether there is another thread k that owns the lock, and the type of lock it possesses is incompatible with the type of lock to be applied by the thread t. If so, step 204 is performed in sequence, otherwise it proceeds to step 205.
  • Step 205 Clear aov[t] j] is empty.
  • Step 206 Determine whether there is a next thread applying for the lock in the jth column of the aov adjacency matrix, and if yes, go to step 202, otherwise the unlocking process is ended.
  • thread j releases a lock of a certain type of lock object lm, this will cause other threads that were originally blocked by thread j to have the opportunity to obtain the type of lock applied.
  • thread t blocked by thread j there are 2 possibilities at this time.
  • the first possibility is that the lock type owned by thread j is incompatible with the lock type that thread t is applying for, then the wait between the two threads The relationship remains the same; the second possibility is that the lock type owned by thread j is compatible with the lock type that thread t is applying for, and thread t has the opportunity to obtain the type of lock applied, and the wait relationship between threads may need to be changed.
  • the above thread locking process and the unlocking process jointly complete the collection process of the deadlock detection information, and they prepare the waiting relationship information between all the threads of the global for the deadlock detection thread.
  • FIG. 3 is a schematic flowchart of a deadlock detection algorithm, which describes an algorithm for detecting a deadlock by using the graph theory aov network principle.
  • a deadlock detection algorithm which describes an algorithm for detecting a deadlock by using the graph theory aov network principle.
  • the data in the aov adjacency matrix is dynamically changed, it is very accurate to use the backup data of the aov adjacency matrix to detect the deadlock that has occurred. This is because for threads that have entered the deadlock state, they are all in a blocked state. In the absence of external intervention, the deadlock state cannot be automatically released. Therefore, although static data is used during the detection of deadlock, However, it is still possible to accurately detect the deadlock that has occurred. However, during the detection of deadlocks, new deadlocks are not ruled out and they need to wait until the next deadlock detection process to detect them.
  • Step 301 Copy a backup of aov for calculation, because there is no ambiguity, so this backup is also called aov.
  • Step 302 Calculate the inovity array of aov, and store the result in in_degree.
  • Step 303 Calculate the out array of aov, and store the result in out_degree.
  • Step 304 Is in_degree equal to out_degree? If yes, go to step 305; otherwise, go to step 307.
  • Step 305 Is in_degree a 0 vector? If yes, it indicates that no loop occurs, the calculation ends (the only function exit), otherwise go to step 306.
  • Step 306 According to a predetermined policy, select a non-zero subscript k from in_degree, and the thread k is to roll back the thread number of the transaction. Find a non-zero element aov[k] j] from aov, which is a pointer to the event object. According to the lock lm it finds, find the thread k lock information table tli, set the status field of tli to -1, and then Wake up the thread k. Clear aov[k] j] to 0, and go to step 302.
  • Step 307 Clear all aov[i][O..MAX_THREADS] to 0 for all subscripts i whose degree is not 0 and the degree of entry is 0. Jump step 2).
  • MAX_THREADS represents the maximum number of threads.
  • FIG. 4 is a schematic diagram of a scene in which a deadlock phenomenon is described using a directed graph.
  • the top half of Figure 4 depicts the wait relationship between threads at a certain point in time.
  • the av network topology sorting method can detect these two loops.
  • the deadlock detection algorithm finds all vertices with only degree of out and no degree, removes the vertices and the arcs they initiate, as shown by the dashed line in Figure 4, repeating the process until There are no such vertices.
  • the thread t5 and the thread t8 are respectively selected as the victim threads for breaking the deadlock state. It is necessary to remind that any vertex has at most one arc initiated by it. This is because any thread that fails to apply for a lock will be blocked, and there is no chance to apply for another lock and it will be blocked for the second time.
  • FIG. 5 and FIG. 6 are diagrams showing state changes of the aov adjacency matrix for detecting deadlock by using the algorithm described in FIG. 3, wherein FIG. 5 is a state diagram of the aov adjacency matrix before the deadlock is detected, FIG. A state diagram for the aov adjacency matrix after the deadlock is detected. If there is an arc reaching the vertex of the vertex ⁇ , then the i-th row and the j-th column of aov record a pointer of the waiting event object, which is represented by solid dots in FIG. 5 and FIG. 6, and the unit without the solid dot is empty data. .
  • the thread number is represented by an Arabic numeral, and the thread number of the thread is i.
  • the i-th row of the aov adjacency matrix records the information of the thread waiting for other threads. As mentioned above, it is obvious that there is at most one non-empty unit in a row.
  • the jth column of the aov adjacency matrix records information about other threads waiting for the thread ⁇ , and a column may have multiple non-empty cells, indicating that multiple threads are waiting for the thread.
  • FIG. 7 is a schematic structural diagram of an apparatus according to an embodiment of the present invention, which may specifically include:
  • the information storage module the deadlock detection module, the information recording module in each thread, and the information update module are described in detail below.
  • the information storage module in the process of locking, if the locking thread needs to wait for other threads, the waiting relationship information between the threads is generated, and the waiting relationship information between the threads is stored in the information storage module; the information storage module is in the present
  • the form of the aov adjacency matrix is employed in the embodiment of the invention.
  • the information recording module can be included in the thread.
  • the thread is called a locking thread.
  • the information recording module records the waiting relationship information of the locking thread waiting for other threads in the information storage module, and then suspends waiting for the relevant thread to unlock.
  • the information recording module of the locking thread i records the waiting relationship information (aov[i][j] indicating that the thread i waits for the thread j) in the i row j column of the aov adjacency matrix, and empties after waking up and obtaining the applied lock.
  • the information update module may be included in the thread.
  • the thread When the thread unlocks, the thread is called an unlocking thread; during the unlocking process of the relevant thread, determining each thread recorded in the information storage module to apply for the lock, Whether the type of lock to be applied is compatible with the type of lock owned by the unlocking thread. If it is incompatible, the waiting relationship information remains unchanged. If it is compatible, it is further determined whether it is incompatible with the type of lock owned by at least one other thread. If yes, perform a lock wait relationship migration, update the corresponding waiting relationship information in the adjacency matrix, otherwise only clear the original inter-thread waiting relationship information.
  • the information update module of the unlocking process j determines whether the thread t is waiting.
  • the lock is lm and the type of lock to be applied by the thread t is compatible with the lock type that the thread also has. If it is incompatible, the aov[t] j] is kept unchanged. If it is compatible, the next lock is found.
  • each thread itself may include an information recording module for recording waiting relationship information between the thread and other threads in the information storage module when the thread is used as a locking thread; and may further include an information update module. And for updating the corresponding waiting relationship information in the information storage module as needed when the thread is used as the unlocking module.
  • an information recording module for recording waiting relationship information between the thread and other threads in the information storage module when the thread is used as a locking thread
  • an information update module for updating the corresponding waiting relationship information in the information storage module as needed when the thread is used as the unlocking module.
  • the deadlock detection module in the embodiment of the present invention, the deadlock detection module is periodically executed by a background independent thread (dead lock detection thread) (post-detection) or immediately detected during the locking process (pre-detection).
  • the deadlock detection thread first copies a backup of the aov adjacency matrix (the adj adjacency matrix constitutes a directed graph G), and then applies the detection algorithm shown in FIG. 3 to perform operations on the backup data to find a loop waiting loop (deadlock loop) therein. : Remove all nodes without predecessors and all arcs it emits. If all nodes can be removed, no deadlock occurs, end detection; otherwise, there is a deadlock loop, select one of the deadlock loops according to the predetermined strategy. Node as deadlock detection Sacrifice thread.
  • the embodiments of the present invention provide a deadlock detection method and apparatus for a database transaction lock mechanism, and the following advantages are as follows:
  • the adjacency matrix is used to record the waiting relationship between threads. Only the operation of the matrix can detect all the loop waiting loops between the threads. For each ring, a victim thread is selected according to a certain strategy. When it fails to return, it can break the deadlock and has extremely high detection efficiency.
  • the information obtained by the adding and unlocking process can be fully utilized to assist in detecting deadlocks, and the repeated calculation of such information during the deadlock detection process is avoided, thereby improving the utilization rate of the system computing resources.
  • many lock mechanisms completely separate the adding, unlocking process and deadlock detection process, and lose some useful information, resulting in repeated calculation of the deadlock detection process in the later stage, wasting computing resources.
  • aov adjacency matrix backup data to detect deadlocks can make the deadlock detection thread extremely small, hinder the normal thread's adding and unlocking process, and hardly block the execution of normal threads.
  • the multi-locking mechanism needs to access the data in the resource map and the lock entity during the process of detecting the deadlock, so the deadlock detection process inevitably blocks the addition and unlocking processes of other normal threads.
  • the deadlock detection phase uses the backup data for operation, and does not interfere with the normal thread's plus unlock process, thereby improving the overall efficiency of the system.

Description

数据库事务锁机制的死锁检测方法及装置 技术领域
本发明涉及数据库技术领域, 尤其涉及一种数据库事务锁机制的死锁 检测方法及装置。 背景技术
事务(Transaction )是数据库管理系统(DBMS )提供的基本也是最重 要的功能之一, 而事务功能的实现要依赖 2 个核心基础技术: 锁机制和 REDO/UNDO 日志功能。 锁机制是事务实现的核心技术, 它关系到事务功 能能否实现, 系统的性能和吞吐量, 以及系统的稳定性。
各种数据库管理系统所采用的锁机制的基本理论是一样的, 但是实现 方法各有不同。 目前主流的数据库管理系统, 如 Oracle, Sybase, MS SQL Server, MySQL ( InnoDB )等, 都实现自己的一整套锁机制, 各有其优缺 点。
死锁检测是锁机制的一个关键技术。 尽管在某些特定场景中可以使用 一些方法避免和降低死锁发生, 但是死锁有时还是难以避免, 因此死锁检 测成了锁机制中不可或缺的一部分。 死锁检测需要消耗不少计算资源, 其 效率的好坏对系统整体性能有不小的影响。
例如, MS SQL Server 2008的死锁检测方法如下: 当锁监视器对特定 线程启动死锁搜索时, 会标识线程正在等待的资源。 然后, 锁监视器查找 特定资源的所有者, 并递归地继续执行对那些线程的死锁搜索, 直到找到 一个循环。 用这种方式标识的循环形成一个死锁。 这属于事后检测。
再例如, InnoDB的死锁检测也有类似的递归算法, 即, 事务 T1 (请求 者线程)要对某个对象 obj (表或者记录)加锁, 当需要等待时, 首先创建 一个锁对象 wait_lock, 然后事先测试是否发生死锁。 它调用一个自递归函 数 lock—deadlock—recursive ( Tl , wait—lock ), 表示判断在 wait—lock之前的 加锁者 (事务)是否等待 Tl。 具体死锁检测算法如下: 1 )如果加在对象 obj上的前一个锁不存在, 则没有死锁, 退出。 2 )判断是否 waiUock需要 等待前一个锁 lock, 且 lock的拥有者 T2等于 Tl , 若是, 则表示已经发生 死锁, 退出。 3 ) 得到事务 T2 正在等待的锁 wait_lock2 , 递归调用 lock—deadlock—recursive ( Tl , wait_lock2 )。 可见, InnoDB 也是采用递归 调用的方式来测试是否出现事务之间的等待环。
MS SQL Server 和 InnoDB 都是采用递归方式来检测死锁, 特别是 InnoDB , 采用双向链表来组织保护对象已授予的锁对象列表, 以及事务拥 有的锁对象列表, 死锁检测算法遍历这些链表来检测等待环的存在。 这种 方式检测效率较低, 而且一次检测最多只能检测出一个等待环, 同时检测 过程对正在进行的线程(事务)有不小的妨碍。
综上所述, 目前大部分数据库管理系统, 不管是事先检测还是事后检 测, 主要都是利用资源图 (已分配资源图和请求资源图) 来检测死锁。 这 种方法的缺点是丟失了加解锁过程中产生的有助于死锁检测的信息, 使得 死锁检测时需要重新计算这些信息, 造成死锁检测繁瑣及计算资源的浪费。 发明内容
鉴于上述的分析, 本发明旨在提供一种数据库事务锁机制的死锁检测 方法及装置, 用以解决现有技术中存在的死锁检测繁瑣及计算资源浪费的 问题。
本发明的目的主要是通过以下技术方案实现的:
本发明提供了一种数据库事务锁机制的死锁检测方法, 预设用于存放 线程间等待关系信息的邻接矩阵; 所述方法包括:
步骤 A、 加锁线程将加锁时生成的线程间等待关系信息记录在所述邻 接矩阵中;
步骤 B、 解锁线程在解锁时根据需要更新所述邻接矩阵中相对应的等 待关系信息;
步骤 C、死锁检测线程根据所述邻接矩阵和 AOV网络原理对所述线程 进行检测计算, 判断是否存在死锁。
进一步地, 所述步骤 A具体包括:
在对锁对象进行所述加锁时, 如果加锁线程要申请的锁类型与所述锁 对象已授予其他线程的锁类型不相容, 则将加锁线程等待其他线程的等待 关系信息记录在所述邻接矩阵中; 加锁线程挂起等待相关线程进行解锁。 所述相关线程指: 已经被授予这把锁的所有线程, 换句话说, 就是那些申 请加锁成功的线程。
进一步地, 所述步骤 B具体包括:
步骤 Bl、 解锁线程释放先前它被授予的锁 a时, 检查所述邻接矩阵中 记录的要申请 a锁的每个线程 t, 判断线程 t所要申请的锁类型是否与所述 解锁线程拥有的锁类型相容, 如果不相容, 则执行步骤 B2; 如果相容, 则 执行步骤 B3;
步骤 B2、 保持等待关系信息不变; 结束当前流程;
步骤 B3、 继续判断线程 t所要申请的锁类型是否与至少一个其他线程 拥有的锁类型不相容, 如果是, 则进行锁等待关系迁移, 更新所述邻接矩 阵中相对应的等待关系信息; 否则, 仅清除原来的线程间等待关系信息; 结束当前流程。
进一步地, 所述步骤 C具体包括:
步骤 C1、 死锁检测线程拷贝所述邻接矩阵的备份;
步骤 C2、 根据备份的邻接矩阵中的线程间等待关系信息和 AOV网络 原理中的拓朴排序方式对所述线程进行检测计算, 检测线程间是否存在死 锁回路, 存在时, 根据预定策略在所述死锁回路中选择一个线程作为牺牲 线程, 牺牲线程失败返回, 破除死锁状态。
进一步地, 所述步骤 C2具体包括:
步骤 C21、 计算所述邻接矩阵的入度数组和出度数组, 并判断入度数 组是否等于出度数组, 如果是, 则执行步骤 C22; 否则, 执行步骤 C23; 步骤 C22、 入度数组是否是 0向量, 如果是, 则计算结束; 否则, 根 据预定策略在所述死锁回路中选择一个线程作为牺牲线程, 牺牲线程失败 返回;
步骤 C23、 清除所有出度不为 0且入度为 0的元素, 并转到步骤 C21。 本发明实施例还提供了一种数据库事务锁机制的死锁检测装置, 所述 装置包括: 信息存储模块、 死锁检测模块、 每个线程中包含的信息记录模 块和信息更新模块; 其中,
所述信息记录模块, 用于将加锁时生成的线程间等待关系信息记录到 所述信息存储模块中;
所述信息更新模块, 用于在解锁时根据需要更新所述邻接矩阵中相对 应的等待关系信息;
所述信息存储模块, 用于以预设邻接矩阵的形式保存线程间等待关系 信息;
所述死锁检测模块, 用于根据所述信息存储模块存放的线程间等待关 系信息和 AOV网络原理对线程进行检测计算, 判断是否存在死锁。
进一步地, 所述信息记录模块, 进一步用于在加锁的情况下, 当加锁 线程要申请的锁类型与所述锁对象已授予其他线程的锁类型不相容时, 所 述信息记录模块将加锁线程等待其他线程的等待关系信息记录在所述信息 存储模块中, 然后挂起等待相关线程进行解锁。
进一步地, 所述信息更新模块, 进一步用于在相关线程进行解锁时, 判断所述信息存储模块中记录的要申请当前加锁的每个线程所要申请的锁 类型是否与所述解锁线程拥有的锁类型相容, 不相容时, 保持等待关系信 息不变; 相容时, 继续判断所述当前申请加锁的每个线程所要申请的锁类 型是否与至少一个其他线程拥有的锁类型不相容, 不相容时, 进行锁等待 关系迁移, 更新所述邻接矩阵中相对应的等待关系信息; 相容时, 仅清除 原来的线程间等待关系信息。
进一步地, 所述死锁检测模块, 进一步用于拷贝所述信息存储模块中 的所述邻接矩阵作为备份, 根据备份的邻接矩阵中的线程间等待关系信息 和 AOV网络原理中的拓朴排序方式对所述线程进行检测计算,检测线程间 是否存在死锁回路, 确认存在死锁回路时, 根据预定策略在所述死锁回路 中选择一个线程作为牺牲线程, 牺牲线程失败返回, 破除死锁状态。
本发明有益效果如下:
本发明所提出的技术方案具有极高的死锁检测速度, 而且能够充分利 用加解锁过程获得的有用信息来帮助随后的检测死锁, 节约了计算资源。
本发明的其他特征和优点将在随后的说明书中阐述, 并且, 部分的从 说明书中变得显而易见, 或者通过实施本发明而了解。 本发明的目的和其 他优点可通过在所写的说明书、 权利要求书、 以及附图中所特别指出的结 构来实现和获得。 附图说明
图 1为本发明实施例所述死锁检测方法的主体流程示意图;
图 2为本发明实施例所述方法中死锁检测信息更新的流程示意图; 图 3为本发明实施例所述方法中死锁检测算法的流程示意图; 图 4为本发明实施例所述方法中用有向图描述的发生死锁现象的场景 示意图;
图 5为本发明实施例所述方法中应用图 3所述算法检测到死锁前邻接 矩阵的状态图;
图 6为本发明实施例所述方法中应用图 3所述算法检测到死锁后邻接 矩阵的状态图;
图 7为本发明实施例所述装置的结构示意图。 具体实施方式 本发明的主要内容为: 线程在加锁和解锁过程中产生和维护死锁检测 信息, 然后利用死锁检测信息及 AOV网络原理对线程进行拓朴排序, 从而 判断是否存在死锁。
下面结合附图来具体描述本发明的优先实施例, 其中, 附图构成本申 请一部分, 并与本发明的实施例一起用于阐释本发明的原理。
首先结合附图 1到附图 4对本发明实施例所述方法进行详细说明。 如图 1所示, 图 1为本发明实施例所述死锁检测方法的主体流程示意 图, 主要包括 2个阶段, 即: 死锁检测信息的收集和利用死锁检测信息检 测死锁状态。 所述死锁检测信息是指线程间的等待关系信息, 所述等待关 系信息全部存储在在一个邻接矩阵中, 这个矩阵叫做 aov, 它记录线程之间 的等待关系信息。 这里, 加锁线程负责生成线程间等待关系信息, 解锁线 程负责在解锁过程中根据需要更新线程间的等待关系信息, 死锁检测线程 负责根据加解锁过程收集到的等待关系信息, 利用 aov 网络原理, 对线程 进行拓朴排序, 借以判断是否存在死锁, 以及存在的循环等待环。
具体可以包括以下步骤:
步骤 101 : 在加锁过程中, 加锁线程如果需要等待其他线程, 则将这个 线程间等待关系信息记录在邻接矩阵 aov中。
步骤 102: 在解锁过程中, 解锁线程根据需要更新线程之间的这种等待 关系信息, 更改 aov邻接矩阵中的相应数据。
步骤 103: 死锁检测线程先拷贝 aov邻接矩阵(aov邻接矩阵构成一个 有向图 G ) 的一个备份, 接着应用 aov 网络拓朴排序的方法对备份数据进 行运算, 查找其中的循环等待环(死锁回路): 去掉所有没有前驱的结点以 及所有它发出的弧, 如果所有结点都可以去掉, 则说明没有发生死锁, 结 束检测; 否则说明存在死锁回路, 根据预定策略, 比如可以根据用户需要 进行制定, 比如最小代价策略, 即选择工作量最小的线程作为牺牲线程, 选取每个环中的一个结点作为死锁检测的牺牲线程。 这里, 死锁检测可以 由一个后台独立线程(死锁检测线程)定期执行(事后检测), 也可以在加 锁过程中即时检测 (事先检测)。
需要说明的是, 不是机械地照搬 aov网络理论, aov网络主要是用来评 估工程进度图的可行性, 它用顶点表示活动, 用弧表示活动间的优先关系。 而主要是借用 aov网络的特点来作为死锁检测的有力工具: 1 ) aov网络中 的弧表示活动之间存在的某种制约关系; 2 ) aov 网络中不能出现回路。 本 发明用顶点表示线程本身, 用弧表示线程之间的等待关系, 当 aov 网络中 不存在回路时表明不存在死锁, 否则至少存在一个死锁。 需要指出的是, 并不是所有线程之间都存在等待关系, 所有直接或者间接存在等待关系的 线程构成一个狭义的小 aov网络,本发明实施例所述的 aov邻接矩阵中可能 存在若干个这样的小 aov网络。
下面进一步对本发明实施例所述方法进行详细说明。
死锁检测信息生成(即步骤 101 )过程具体包括:
在对某个锁对象(例如, lm )加锁的过程中, 如果当前线程 i要申请的 锁类型与 lm已授予其他线程(任取其中一个线程 j ) 的锁类型不相容, 则 当前线程 i就需要挂起等待。 在挂起等待之前, 在 aov的 i行 j列记录这个 等待关系信息(表示线程 i等待线程 j ), 而在唤醒并且获得申请的锁之后清 空。
线程 i申请锁对象 lm的某个类型的锁, 当该类型锁与 lm已授予线程 j 的锁类型不相容时, 表明线程 i至少需要等待线程 j释放相应类型的锁才能 成功获得申请的锁类型。这样,线程 i和线程 j之间存在着某种偏序关系(等 待关系 ), 在有向图中用结点 i到结点 j的一条弧来表示, 在 aov邻接矩阵 中则是在 aov[i] j]单元中记录线程 i正在等待的通知事件 event的指针。
当线程 j释放已获得的锁类型时, 线程 i和线程 j之间的等待关系可能 需要调整。
如图 2所示, 图 2为本发明实施例所述方法中死锁检测信息更新(步 骤 102 ) 的流程示意图, 具体可以包括:
步骤 201 : 线程 j在对某个锁对象 lm解锁的过程中, 检查 aov邻接矩 阵中第 j列中记录的等待这把锁的第一个线程 t。
步骤 202: 判断线程 t所要申请的锁类型与线程 j拥有的锁类型是否相 容, 若是, 执行步骤 203 , 否则转到步骤 205。
步骤 203: 判断是否存在拥有该锁的其他线程 k, 它拥有的锁类型与线 程 t所要申请的锁类型不相容? 若是, 则顺序执行步骤 204, 否则转到步骤 205。
步骤 204: 进行锁等待关系迁移, 即令 aov[t] [k] = aov[t] j]。
步骤 205: 清除 aov[t] j]为空。
步骤 206: 判断 aov邻接矩阵第 j列中是否还有申请这把锁的下一个线 程, 若是, 转到步骤 202, 否者结束解锁过程。
这里, 由于线程 j释放了锁对象 lm的某个类型的锁, 这将导致原来被 线程 j阻塞的其他线程有机会获得所申请的锁类型。 对于被线程 j阻塞的其 他线程 t, 此时存在 2中可能, 第一种可能是线程 j拥有的锁类型与线程 t 正要申请的锁类型不相容, 那么这 2个线程之间的等待关系保持不变; 第 二种可能是线程 j拥有的锁类型与线程 t正要申请的锁类型相容, 则线程 t 有机会获得所申请的锁类型, 线程之间的等待关系可能需要改变, 这取决 于锁对象 lm已授予别的线程的锁类型是否与线程 t正要申请的锁类型相容。 假设有某个线程 k拥有的锁类型与线程 t正要申请的锁类型不相容,那么线 程 t还是无法获得所申请的锁类型,在这种情况下, 线程之间的等待关系发 生了变化: 线程 t现在不再等待线程 j , 而是等待线程 k。 要求把这种等待 关系的变化反映到邻接矩阵 aov 中, 这个过程称为线程间等待关系迁移。 线程解锁过程的任务就是要维护线程间等待关系迁移的信息。
以上线程加锁过程和解锁过程共同完成了死锁检测信息的收集过程, 它们为死锁检测线程准备了全局的所有线程之间的等待关系信息。
如图 3所示, 图 3为死锁检测算法的流程示意图, 其描述了利用图论 aov网络原理检测死锁的算法。 这里要说明的是, 虽然 aov邻接矩阵中的数 据是动态变化着的, 但是使用 aov邻接矩阵的备份数据来检测已经发生的 死锁现象还是非常准确的。 这是因为对于已经进入死锁状态的线程而言, 它们都处于阻塞状态, 在没有外部介入的情况下, 死锁状态不可能自动解 除, 所以, 虽然检测死锁过程中使用的是静态数据, 但是还是可以准确无 误地检测到业已发生的死锁。 不过, 在检测死锁期间不排除有新的死锁现 象产生, 它们需要等到下一次死锁检测过程才能检测得到。
具体包括以下步骤:
步骤 301 : 拷贝 aov的一个备份以便计算, 因为没有什么歧义, 所以这 个备份还称为 aov。
步骤 302: 计算 aov的入度数组, 结果存放在 in_degree。
步骤 303: 计算 aov的出度数组, 结果存放在 out_degree。
步骤 304: in_degree是否等于 out_degree? 如果是, 执行步骤 305; 否则 执行步骤 307。
步骤 305: in_degree是否是 0向量? 如果是, 表明没有出现回路, 计算 结束(唯一函数出口), 否则转到步骤 306。 步骤 306: 根据预定策略, 从 in_degree中选取一个非 0的下标 k, 线程 k 就是要回滚事务所在的线程号。 从 aov中找到非 0元素 aov[k] j] , 它是一个等 待事件对象指针, 根据它找到所在的锁 lm, 找到线程 k加锁信息表 tli, 将 tli 的 status字段置成- 1 , 然后唤醒线程 k。 将 aov[k] j]清 0, 跳转步骤 302。
步骤 307: 对于所有 出度不为 0且入度为 0的下标 i , 将 aov[i][O..MAX_THREADS]清 0。 跳转步骤 2 )。 这里 MAX_THREADS表示最 大线程数。
如图 4所示, 图 4为用有向图描述的发生死锁现象的场景示意图。 图 4 的上半图描述了该系统在某一个时刻线程之间的等待关系, 顶点 表示线程, 若顶点 ^ ( ί=1、 2...10, i≠j )存在一条到达顶点 ( j=l、 2...10, j≠i ) 的弧, 则表示线程 等待 从图中可以看到存在 2个死锁回路, 应 用 aov网络拓朴排序方法可以检测到这 2个回路。
如图 4 的下半图所示, 死锁检测算法找到所有只有出度、 没有入度的 顶点, 去掉这些顶点和它们发起的弧, 如图 4 中的虚线所示, 不断重复该 过程, 直到没有任何这样的顶点。 最后就剩下下半图所示的 2个回路(死 锁), 根据预定策略, 分别选定线程 t5和线程 t8作为破除死锁状态的牺牲 线程。 这里有必要提醒的是, 任何顶点都最多只有一条由它发起的弧, 这 是因为任何线程申请锁失败之后就会被阻塞, 没有机会再申请其他锁而发 生第二次被阻塞的情形。
如图 5和图 6所示, 图 5和图 6为应用图 3所述算法检测死锁的 aov 邻接矩阵状态变化图, 其中图 5为检测到死锁前 aov邻接矩阵的状态图, 图 6为检测到死锁后 aov邻接矩阵的状态图。 若顶点 ^存在一条到达顶点 的弧, 则 aov的第 i行第 j列就记录一个等待事件对象指针, 在图 5和图 6 中用实心圓点表示, 没有实心圓点的单元都是空数据。 为了筒化表述, 假 设总共只有 10个线程, 线程号用阿拉伯数字表示, 线程 的线程号就是 i。 aov邻接矩阵的第 i行记录着线程 ^等待其他线程的信息,如前所述,显然, 一行最多只有一个非空单元。 aov邻接矩阵的第 j列记录着其他线程等待线 程 ^的信息, 一列可以有多个非空单元, 表示多个线程在等待该线程。应用 图 3 的算法, 只需经过筒单的矩阵运算: 计算入度向量和出度向量, 以及 向量之间的逻辑比较, 就可以对 aov邻接矩阵进行化筒, 从而得到图 6所 示的矩阵状态, 空心圓点所在行对应的线程就是选定的牺牲线程。 由于矩 阵运算就是筒单的加法运算和逻辑比较, 所以性能极高。
下面结合附图 7对本发明实施例所述装置进行详细说明。
如图 7所示, 图 7为本发明实施例所述装置的结构示意图, 具体可以 包括:
信息存储模块、 死锁检测模块、 每个线程中的信息记录模块和信息更 新模块, 下面对各个模块分别进行详细说明。
信息存储模块, 在加锁过程中, 加锁线程如果需要等待其他线程, 就 会生成线程间等待关系信息, 这些线程间的等待关系信息就存放到信息存 储模块中; 所述信息存储模块在本发明实施例中采用的是 aov邻接矩阵的 形式。
信息记录模块, 可以包含于于线程中, 当线程进行加锁时该线程称为 加锁线程; 在加锁过程中, 当加锁线程要申请的锁类型与该锁对象已授予 其他线程的锁类型不相容时, 所述信息记录模块将加锁线程等待其他线程 的等待关系信息记录在所述信息存储模块中, 然后挂起等待相关线程进行 解锁。 比如, 在对某个锁对象(例如, lm )加锁的过程中, 如果加锁线程 i 要申请的锁类型与 lm已授予其他线程(任取其中一个线程 j ) 的锁类型不 相容, 则加锁线程 i的信息记录模块在 aov邻接矩阵的 i行 j列记录这个等 待关系信息 (aov[i][j] , 表示线程 i等待线程 j ), 而在唤醒并且获得申请的 锁之后清空。 信息更新模块, 可以包含于于线程中, 当线程进行解锁时该线程称为 解锁线程; 在相关线程进行解锁过程中, 判断所述信息存储模块中记录的 要申请这把锁的每个线程, 所要申请的锁类型是否与该解锁线程拥有的锁 类型相容, 若不相容, 则保持等待关系信息不变, 若相容, 则进一步判断 是否与至少一个其他线程拥有的锁类型不相容, 如果是, 则进行锁等待关 系迁移, 更新所述邻接矩阵中的相应等待关系信息, 否则仅仅清除原来的 线程间等待关系信息。 比如, 当解锁程 j在对某个锁对象 lm解锁的过程中, 对于 aov邻接矩阵第 j列中每一个非空位置对应的线程 t, 解锁程 j的信息 更新模块判断是否线程 t正在等待的锁是 lm且线程 t所要申请的锁类型与 本线程还拥有的锁类型相容, 若不相容, 则保持 aov[t] j]不变, 若相容, 则 查找下一个已拥有的锁类型与线程 t要申请的锁类型不相容的线程 k, 令 aov[t][k] = aov[t] j] , 清除 aov[t] j]为空; 否则, 仅仅清空 aov[t] j]为空。
需要注意的是, 每个线程本身既可以包括信息记录模块, 用于在该线 程作为加锁线程时记录其与其他线程间的等待关系信息到所述信息存储模 块中; 还可以包括信息更新模块, 用于在该线程作为解锁模块时根据需要 更新所述信息存储模块中的相应等待关系信息。 本发明实施例中仅以两个 线程作为示例, 但本领域技术人员应该知道, 数据库管理系统中具有多个 线程, 自然就会产生不止一个的线程间等待关系信息。
死锁检测模块,本发明实施例中死锁检测模块由一个后台独立线程(死 锁检测线程) 定期执行(事后检测)或者在加锁过程中即时检测 (事先检 测)。 死锁检测线程先拷贝 aov邻接矩阵( aov邻接矩阵构成一个有向图 G ) 的一个备份, 接着应用图 3所示的检测算法对备份数据进行运算, 查找其 中的循环等待环(死锁回路): 去掉所有没有前驱的结点以及所有它发出的 弧, 如果所有结点都可以去掉, 则说明没有发生死锁, 结束检测; 否则说 明存在死锁回路, 根据预定策略选取死锁回路中的一个结点作为死锁检测 的牺牲线程。
对于本发明实施例所述装置的具体实现过程, 由于本发明实施例所述 方法中已进行了详细说明, 故此处不再赘述。
综上所述, 本发明实施例提供了一种数据库事务锁机制的死锁检测方 法及装置, 具体以下优点:
利用邻接矩阵来记录线程之间的等待关系, 只需对矩阵进行筒单的运 算就可以检测出线程之间的所有循环等待环, 对每个环, 根据某个策略选 定一个牺牲线程, 让其失败返回, 就可以破除死锁, 具有极高的检测效率。
可以充分利用加、 解锁过程已获得的信息来协助检测死锁, 避免真正 执行死锁检测过程中重复计算这些信息, 从而提高了系统计算资源的利用 率。 而目前很多锁机制把加、 解锁过程和死锁检测过程完全分开, 丟失了 一些有用的信息, 造成后期执行死锁检测过程的重复计算, 浪费了计算资 源。
利用 aov邻接矩阵备份数据来检测死锁, 可以使得死锁检测线程极小 妨碍正常线程的加、 解锁过程, 几乎不会阻塞正常线程的执行。 而目前 ^艮 多锁机制因为检测死锁过程中需要访问资源图和锁实体中的数据, 所以死 锁检测过程不可避免地会瞬时阻塞其他正常线程的加、 解锁过程。
死锁检测阶段使用备份数据进行运算, 不会干扰正常线程的加解锁过 程, 从而提高了系统的整体效率。
以上所述, 仅为本发明较佳的具体实施方式, 但本发明的保护范围并 不局限于此, 任何熟悉本技术领域的技术人员在本发明揭露的技术范围内, 可轻易想到的变化或替换, 都应涵盖在本发明的保护范围之内。 因此, 本 发明的保护范围应该以权利要求书的保护范围为准。

Claims

权利要求书
1、 一种数据库事务锁机制的死锁检测方法, 其特征在于, 预设用于存 放线程间等待关系信息的邻接矩阵; 所述方法包括:
A、加锁线程将加锁时生成的线程间等待关系信息记录在所述邻接矩阵 中;
B、解锁线程在解锁时根据需要更新所述邻接矩阵中相对应的等待关系 信息;
C、 死锁检测线程根据所述邻接矩阵和 AOV网络原理对线程进行检测 计算, 判断是否存在死锁。
2、 根据权利要求 1所述的方法, 其特征在于, 所述步骤 A具体包括: 在对锁对象进行所述加锁时, 如果加锁线程要申请的锁类型与所述锁 对象已授予其他线程的锁类型不相容, 则将加锁线程等待其他线程的等待 关系信息记录在所述邻接矩阵中; 加锁线程挂起等待相关线程进行解锁。
3、 根据权利要求 2所述的方法, 其特征在于, 所述步骤 B具体包括: Bl、 解锁线程释放先前它被授予的锁 a时, 检查所述邻接矩阵中记录 的要申请 a锁的每个线程 t, 判断线程 t所要申请的锁类型是否与所述解锁 线程拥有的锁类型相容, 如果不相容, 则执行步骤 B2; 如果相容, 则执行 步骤 B3;
B2、 保持等待关系信息不变; 结束当前流程;
B3、 继续判断所述当前加锁的每个线程所要申请的锁类型是否与至少 一个其他线程拥有的锁类型不相容, 如果是, 则进行锁等待关系迁移, 更 新所述邻接矩阵中相对应的等待关系信息; 否则, 仅清除原来的线程间等 待关系信息; 结束当前流程。
4、 根据权利要求 1至 3任一项所述的方法, 其特征在于, 所述步骤 C 具体包括: CI、 死锁检测线程拷贝所述邻接矩阵的备份;
C2、根据备份的邻接矩阵中的线程间等待关系信息和 AOV网络原理中 的拓朴排序方式对所述线程进行检测计算, 检测线程间是否存在死锁回路, 存在时, 根据预定策略在所述死锁回路中选择一个线程作为牺牲线程, 牺 牲线程失败返回, 破除死锁状态。
5、 根据权利要求 4所述的方法, 其特征在于, 所述步骤 C2具体包括: C21、 计算所述邻接矩阵的入度数组和出度数组, 并判断入度数组是否 等于出度数组, 如果是, 则执行步骤 C22; 否则, 执行步骤 C23;
C22、 入度数组是否是 0向量, 如果是, 则计算结束; 否则, 根据预定 策略在所述死锁回路中选择一个线程作为牺牲线程, 牺牲线程失败返回;
C23、 清除所有出度不为 0且入度为 0的元素, 并转到步骤 C21。
6、 一种数据库事务锁机制的死锁检测装置, 其特征在于, 所述装置包 括: 信息存储模块、 死锁检测模块、 每个线程中包含的信息记录模块和信 息更新模块; 其中,
所述信息记录模块, 用于将加锁时生成的线程间等待关系信息记录到 所述信息存储模块中;
所述信息更新模块, 用于在解锁时根据需要更新所述邻接矩阵中相对 应的等待关系信息;
所述信息存储模块, 用于以预设邻接矩阵的形式保存线程间等待关系 信息;
所述死锁检测模块, 用于根据所述信息存储模块存放的线程间等待关 系信息和 AOV网络原理对线程进行检测计算, 判断是否存在死锁。
7、 根据权利要求 6所述的装置, 其特征在于, 所述信息记录模块, 进 一步用于在加锁的情况下, 当加锁线程要申请的锁类型与所述锁对象已授 予其他线程的锁类型不相容时, 所述信息记录模块将加锁线程等待其他线 程的等待关系信息记录在所述信息存储模块中, 然后挂起等待相关线程进 行解锁。
8、 根据权利要求 6所述的装置, 其特征在于, 所述信息更新模块, 进 一步用于在相关线程进行解锁时, 判断所述信息存储模块中记录的要申请 当前加锁的每个线程所要申请的锁类型是否与所述解锁线程拥有的锁类型 相容, 不相容时, 保持等待关系信息不变; 相容时, 继续判断所述当前加 锁的每个线程所要申请的锁类型是否与至少一个其他线程拥有的锁类型不 相容, 不相容时, 进行锁等待关系迁移, 更新所述邻接矩阵中相对应的等 待关系信息; 相容时, 仅清除原来的线程间等待关系信息。
9、 根据权利要求 6所述的装置, 其特征在于, 所述死锁检测模块, 进 一步用于拷贝所述信息存储模块中的所述邻接矩阵作为备份, 根据备份的 邻接矩阵中的线程间等待关系信息和 AOV 网络原理中的拓朴排序方式对 所述线程进行检测计算, 检测线程间是否存在死锁回路, 确认存在死锁回 路时, 根据预定策略在所述死锁回路中选择一个线程作为牺牲线程, 牺牲 线程失败返回, 破除死锁状态。
PCT/CN2010/073392 2009-06-04 2010-05-31 数据库事务锁机制的死锁检测方法及装置 WO2010139260A1 (zh)

Priority Applications (2)

Application Number Priority Date Filing Date Title
EP10782964.0A EP2439640A4 (en) 2009-06-04 2010-05-31 METHOD AND DEVICE FOR DETECTING FATAL STOP OF DATA BASE TRANSACTION LOCKING MECHANISM
BRPI1011087A BRPI1011087A2 (pt) 2009-06-04 2010-05-31 método e dispositivo para detecção de bloqueio de mecanismo de trava de transação de banco de dados.

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN200910143961.7 2009-06-04
CN2009101439617A CN101576830B (zh) 2009-06-04 2009-06-04 数据库事务锁机制的死锁检测方法及装置

Publications (1)

Publication Number Publication Date
WO2010139260A1 true WO2010139260A1 (zh) 2010-12-09

Family

ID=41271767

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2010/073392 WO2010139260A1 (zh) 2009-06-04 2010-05-31 数据库事务锁机制的死锁检测方法及装置

Country Status (4)

Country Link
EP (1) EP2439640A4 (zh)
CN (1) CN101576830B (zh)
BR (1) BRPI1011087A2 (zh)
WO (1) WO2010139260A1 (zh)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111522730A (zh) * 2020-03-09 2020-08-11 平安科技(深圳)有限公司 程序测试方法及装置、计算机装置、计算机可读介质
CN112099960A (zh) * 2020-09-21 2020-12-18 天津神舟通用数据技术有限公司 一种基于路径推进的分布式死锁检测方法
CN113742091A (zh) * 2021-08-31 2021-12-03 深圳Tcl新技术有限公司 一种线程运行方法、装置、计算机设备和存储介质
CN116089100A (zh) * 2023-01-12 2023-05-09 北京万里开源软件有限公司 一种数据库中锁资源监控方法及装置
WO2024046089A1 (zh) * 2022-08-31 2024-03-07 华为技术有限公司 持锁进程检测方法及相关设备

Families Citing this family (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101576830B (zh) * 2009-06-04 2011-11-30 中兴通讯股份有限公司 数据库事务锁机制的死锁检测方法及装置
CN102222015B (zh) * 2010-04-13 2016-04-27 三星电子(中国)研发中心 检测多线程程序中的死锁的方法及系统
CN102156928A (zh) * 2011-04-29 2011-08-17 浪潮通信信息系统有限公司 一种通过业务逻辑锁进行系统并发控制的方法
CN102521033B (zh) * 2011-12-14 2014-06-25 华南理工大学 一种监测系统调度的死锁检测与解除方法
CN105468509A (zh) * 2014-09-04 2016-04-06 阿里巴巴集团控股有限公司 代码稳定性检测方法及装置
CN105468508B (zh) * 2014-09-04 2018-07-03 阿里巴巴集团控股有限公司 代码检测方法及装置
EP3278239B1 (en) 2015-04-01 2019-08-14 AB Initio Technology LLC Processing database transactions in a distributed computing system
CN106959895B (zh) * 2016-01-12 2021-01-01 创新先进技术有限公司 快速释放线程的资源调度方法和系统
CN107305507B (zh) * 2016-04-25 2020-05-01 北京京东尚科信息技术有限公司 死锁控制方法和装置
EP3557459B1 (en) * 2018-04-17 2022-05-25 Panasonic Intellectual Property Corporation of America Method, information processing device, management system, and program to control locking and unlocking of storage
CN109669858B (zh) * 2018-11-22 2022-04-12 新华三技术有限公司合肥分公司 程序死锁的测试方法、装置和设备
CN111221658B (zh) * 2019-05-31 2023-08-01 烟台职业学院 系统死锁的检测方法及系统
CN112256442A (zh) * 2019-07-22 2021-01-22 中兴通讯股份有限公司 一种数据库死锁检测方法及装置
CN110909012B (zh) * 2019-12-04 2020-09-04 上海达梦数据库有限公司 数据库对象的封锁方法、装置、设备和存储介质
CN111752718B (zh) * 2020-05-28 2022-07-12 西安深信科创信息技术有限公司 一种低开销的死锁预测方法、装置及电子设备
CN112100192A (zh) * 2020-09-27 2020-12-18 中国建设银行股份有限公司 数据库锁等待处理方法及装置
CN112363846B (zh) * 2021-01-11 2021-04-13 北京金山云网络技术有限公司 数据库事务的死锁检测方法、装置及电子设备

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0433882A2 (en) * 1989-12-22 1991-06-26 International Business Machines Corporation Accelerated deadlock detection in congested data transactions
CN1987796A (zh) * 2005-12-22 2007-06-27 国际商业机器公司 检测多线程程序中的死锁的方法和装置
CN101576830A (zh) * 2009-06-04 2009-11-11 中兴通讯股份有限公司 数据库事务锁机制的死锁检测方法及装置

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6128706A (en) * 1998-02-03 2000-10-03 Institute For The Development Of Emerging Architectures, L.L.C. Apparatus and method for a load bias--load with intent to semaphore
CN100344125C (zh) * 2003-11-26 2007-10-17 华为技术有限公司 一种数据传输死锁的解决方法
ATE366959T1 (de) * 2004-02-26 2007-08-15 Research In Motion Ltd Verfahren und system zur erkennung von möglichen blockierungen in computerprogrammen
CN101295266B (zh) * 2008-06-05 2010-06-16 成都市华为赛门铁克科技有限公司 检测和解除死锁的方法、装置和系统

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0433882A2 (en) * 1989-12-22 1991-06-26 International Business Machines Corporation Accelerated deadlock detection in congested data transactions
CN1987796A (zh) * 2005-12-22 2007-06-27 国际商业机器公司 检测多线程程序中的死锁的方法和装置
CN101576830A (zh) * 2009-06-04 2009-11-11 中兴通讯股份有限公司 数据库事务锁机制的死锁检测方法及装置

Cited By (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111522730A (zh) * 2020-03-09 2020-08-11 平安科技(深圳)有限公司 程序测试方法及装置、计算机装置、计算机可读介质
CN111522730B (zh) * 2020-03-09 2024-03-29 平安科技(深圳)有限公司 程序测试方法及装置、计算机装置、计算机可读介质
CN112099960A (zh) * 2020-09-21 2020-12-18 天津神舟通用数据技术有限公司 一种基于路径推进的分布式死锁检测方法
CN113742091A (zh) * 2021-08-31 2021-12-03 深圳Tcl新技术有限公司 一种线程运行方法、装置、计算机设备和存储介质
WO2024046089A1 (zh) * 2022-08-31 2024-03-07 华为技术有限公司 持锁进程检测方法及相关设备
CN116089100A (zh) * 2023-01-12 2023-05-09 北京万里开源软件有限公司 一种数据库中锁资源监控方法及装置
CN116089100B (zh) * 2023-01-12 2023-10-20 北京万里开源软件有限公司 一种数据库中锁资源监控方法及装置

Also Published As

Publication number Publication date
CN101576830A (zh) 2009-11-11
CN101576830B (zh) 2011-11-30
EP2439640A4 (en) 2015-01-21
EP2439640A1 (en) 2012-04-11
BRPI1011087A2 (pt) 2018-05-29

Similar Documents

Publication Publication Date Title
WO2010139260A1 (zh) 数据库事务锁机制的死锁检测方法及装置
KR102103063B1 (ko) 분산 컴퓨팅 시스템에서 데이터베이스 트랜잭션들을 프로세싱하는 방법
JP4726416B2 (ja) コンピュータ・クラスタを操作するための方法
US7284151B2 (en) Conditional data access after database system failure
US7240054B2 (en) Techniques to preserve data constraints and referential integrity in asynchronous transactional replication of relational tables
JP2531881B2 (ja) 並行制御方法
Wang et al. Scaling multicore databases via constrained parallel execution
CN102355473B (zh) 分布式计算环境下的锁定控制系统和方法
CA2375376A1 (en) Collision avoidance in bidirectional database replication
JP2023509035A (ja) トランザクション処理方法、装置、コンピュータデバイス及びコンピュータプログラム
CN112241400A (zh) 一种基于数据库实现分布式锁的方法
CN108431808A (zh) 对分区的数据存储空间当中的结构化数据记录的提醒处理
CN109783578A (zh) 数据读取方法、装置、电子设备以及存储介质
Chen et al. Achieving low tail-latency and high scalability for serializable transactions in edge computing
US20140040219A1 (en) Methods and systems for a deadlock resolution engine
US20230072976A1 (en) Database system with transactional commit protocol based on safe conjunction of majorities
JPH0277868A (ja) 共用データの管理方法
JP2019101518A (ja) 情報処理プログラム、情報処理装置及び情報処理方法
Gupta Deadlock detection techniques in distributed database system
JP4604032B2 (ja) 非共有データベースシステムにおける1段階コミット
Waudby et al. A performance study of epoch-based commit protocols in distributed OLTP databases
Shao et al. Read Consistency in Distributed Database Based on DMVCC
CN112667652B (zh) 基于区块链的模拟交易方法、装置、设备和可读存储介质
Makni et al. Performance evaluation of an optimistic concurrency control algorithm for temporal databases
CN111221658B (zh) 系统死锁的检测方法及系统

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 10782964

Country of ref document: EP

Kind code of ref document: A1

REEP Request for entry into the european phase

Ref document number: 2010782964

Country of ref document: EP

WWE Wipo information: entry into national phase

Ref document number: 2010782964

Country of ref document: EP

NENP Non-entry into the national phase

Ref country code: DE

WWE Wipo information: entry into national phase

Ref document number: 10431/DELNP/2011

Country of ref document: IN

WWE Wipo information: entry into national phase

Ref document number: A20111844

Country of ref document: BY

REG Reference to national code

Ref country code: BR

Ref legal event code: B01A

Ref document number: PI1011087

Country of ref document: BR

REG Reference to national code

Ref country code: BR

Ref legal event code: B01E

Ref document number: PI1011087

Country of ref document: BR

ENP Entry into the national phase

Ref document number: PI1011087

Country of ref document: BR

Kind code of ref document: A2

Effective date: 20111202