CN112231131A - Method, device and equipment for realizing database lock and readable storage medium - Google Patents

Method, device and equipment for realizing database lock and readable storage medium Download PDF

Info

Publication number
CN112231131A
CN112231131A CN202011041096.8A CN202011041096A CN112231131A CN 112231131 A CN112231131 A CN 112231131A CN 202011041096 A CN202011041096 A CN 202011041096A CN 112231131 A CN112231131 A CN 112231131A
Authority
CN
China
Prior art keywords
target data
thread
lock
locking
database
Prior art date
Legal status (The legal status 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 status listed.)
Granted
Application number
CN202011041096.8A
Other languages
Chinese (zh)
Other versions
CN112231131B (en
Inventor
邱海港
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Kingsoft Cloud Network Technology Co Ltd
Original Assignee
Beijing Kingsoft Cloud Network Technology Co Ltd
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 Beijing Kingsoft Cloud Network Technology Co Ltd filed Critical Beijing Kingsoft Cloud Network Technology Co Ltd
Priority to CN202011041096.8A priority Critical patent/CN112231131B/en
Publication of CN112231131A publication Critical patent/CN112231131A/en
Application granted granted Critical
Publication of CN112231131B publication Critical patent/CN112231131B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/07Responding to the occurrence of a fault, e.g. fault tolerance
    • G06F11/0796Safety measures, i.e. ensuring safe condition in the event of error, e.g. for controlling element
    • 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/21Design, administration or maintenance of databases

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Quality & Reliability (AREA)
  • Data Mining & Analysis (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The disclosure relates to a method, a device and equipment for realizing a database lock and a readable storage medium. According to the method and the device, the target data are locked in the database through the identification information of the target data to be accessed, if the locking fails, the target data can be continuously locked, if the number of times of continuously locking the target data reaches the preset number of times and the target data is not successfully locked, the target data can be locked according to the preset time interval, the thread in the computer system cannot release CPU resources to enter a blocking state due to the fact that the target data cannot be locked, the state of the thread is prevented from changing frequently, and time consumption caused by thread state change and thread scheduling can be effectively reduced under the conditions of high concurrency and resource competition.

Description

Method, device and equipment for realizing database lock and readable storage medium
Technical Field
The present disclosure relates to the field of information technologies, and in particular, to a method, an apparatus, a device, and a readable storage medium for implementing a database lock.
Background
In the database, when a plurality of threads access the same resource, only one thread may be allowed to access the resource in order to avoid conflict caused by the plurality of threads accessing the resource at the same time. For example, one of the threads may lock, e.g., mutex, the resource to lock the resource to prevent other threads from accessing the resource.
When a thread needs to access a resource, if the resource is already locked by other threads, the thread will block due to the lock on the resource being not taken, and then the thread enters a blocking state after releasing a Central Processing Unit (CPU) resource. The thread can only be activated when the other thread releases the lock on the resource.
Under conditions of high concurrency and resource contention, the state change of the thread and the thread scheduling consume a large amount of time.
Disclosure of Invention
In order to solve the above technical problem or at least partially solve the above technical problem, the present disclosure provides a method, an apparatus, a device, and a readable storage medium for implementing a database lock, so as to avoid frequent changes in the state of a thread, and effectively reduce the time consumption caused by thread state changes and thread scheduling under conditions of high concurrency and resource contention.
In a first aspect, an embodiment of the present disclosure provides a method for implementing a database lock, including:
acquiring a data access request, wherein the data access request comprises identification information of target data to be accessed;
locking the target data in a database according to the identification information of the target data;
and if the continuous locking times of the target data reach preset times and the locking of the target data fails, locking the target data according to a preset time interval.
In a second aspect, an embodiment of the present disclosure provides an apparatus for implementing a database lock, including:
the system comprises an acquisition module, a storage module and a processing module, wherein the acquisition module is used for acquiring a data access request, and the data access request comprises identification information of target data to be accessed;
the locking module is used for locking the target data in a database according to the identification information of the target data; and if the continuous locking times of the target data reach preset times and the locking of the target data fails, locking the target data according to a preset time interval.
In a third aspect, an embodiment of the present disclosure provides an implementation apparatus for a database lock, including:
a memory;
a processor; and
a computer program;
wherein the computer program is stored in the memory and configured to be executed by the processor to implement the method of the first aspect.
In a fourth aspect, the present disclosure provides a computer-readable storage medium, on which a computer program is stored, the computer program being executed by a processor to implement the method of the first aspect.
According to the method, the device, the equipment and the readable storage medium for realizing the database lock, the target data are locked in the database through the identification information of the target data to be accessed, if the locking fails, the target data can be continuously locked, if the times of continuously locking the target data reach the preset times and the target data are not successfully locked, the target data can be locked according to the preset time interval, so that the thread in the computer system can not release the CPU resource to enter a blocking state because the target data cannot be locked, the frequent change of the state of the thread is avoided, and the time consumption caused by the change of the state of the thread and the scheduling of the thread can be effectively reduced under the conditions of high concurrency and resource competition.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and together with the description, serve to explain the principles of the disclosure.
In order to more clearly illustrate the embodiments or technical solutions in the prior art of the present disclosure, the drawings used in the description of the embodiments or prior art will be briefly described below, and it is obvious for those skilled in the art that other drawings can be obtained according to the drawings without inventive exercise.
FIG. 1 is a flow chart of a method for implementing a database lock according to an embodiment of the present disclosure;
fig. 2 is a schematic diagram of an application scenario provided by the embodiment of the present disclosure;
fig. 3 is a schematic structural diagram of an implementation apparatus of a database lock according to an embodiment of the present disclosure;
fig. 4 is a schematic structural diagram of an implementation device of a database lock according to an embodiment of the present disclosure.
Detailed Description
In order that the above objects, features and advantages of the present disclosure may be more clearly understood, aspects of the present disclosure will be further described below. It should be noted that the embodiments and features of the embodiments of the present disclosure may be combined with each other without conflict.
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present disclosure, but the present disclosure may be practiced in other ways than those described herein; it is to be understood that the embodiments disclosed in the specification are only a few embodiments of the present disclosure, and not all embodiments.
In general, in a database, when a plurality of threads access the same resource, only one thread may be allowed to access the resource in order to avoid conflict caused by the simultaneous access of the plurality of threads to the resource. For example, one of the threads may lock, e.g., mutex, the resource to lock the resource to prevent other threads from accessing the resource. When a thread needs to access a resource, if the resource is already locked by other threads, the thread will block due to the lock on the resource being not taken, and then the thread enters a blocking state after releasing a Central Processing Unit (CPU) resource. The thread can only be activated when the other thread releases the lock on the resource. Under conditions of high concurrency and resource contention, the state change of the thread and the thread scheduling consume a large amount of time. To solve this problem, embodiments of the present disclosure provide a method for implementing a database lock, and the method is described below with reference to specific embodiments.
Fig. 1 is a flowchart of an implementation method of a database lock according to an embodiment of the present disclosure. The method comprises the following specific steps:
s101, a data access request is obtained, and the data access request comprises identification information of target data to be accessed.
The method for implementing the database lock according to the embodiment is particularly applicable to a computer system, which may be a computer system in the server 21 shown in fig. 2. The server 21 may receive a data access request sent by the terminal device 20, where the terminal device 20 may specifically be a mobile phone, a tablet computer, a personal computer, and the like.
Further, the server 21 may access the data in the database 22 according to the data access request. For example, the data access request includes identification information of target data to be accessed. The server 21 can access the target data in the database 22 according to the identification information of the target data. The identification information of the target data may be, for example, an index (index) corresponding to the target data. Wherein the database 22 may be integrated in the server 21. Or the database 22 and the server 21 may be provided separately, and the database 22 and the server 21 may communicate with each other. The server 21 is not limited to one server, and may be a plurality of servers or a server cluster. The database 22 may in particular be a distributed database.
And S102, locking the target data in a database according to the identification information of the target data.
The computer system of the server 21 may specifically be a multithreaded computer system. That is, multiple threads in the server 21 may each access data in the database 22. For example, the server 21 includes therein a first thread for executing a data access request. In order to avoid conflict caused by accessing the target data by multiple threads simultaneously, the first thread may lock the target data in the database 22 before accessing the target data in the database 22 according to the identification information of the target data. For example, where the target data is on a row of the database 22, the first thread may apply a row lock to the row of data, e.g., the row lock is a mutually exclusive lock. If the other threads do not over-lock the target data, the first thread may successfully lock the target data.
S103, if the number of times of continuously locking the target data reaches a preset number of times and the locking of the target data fails, locking the target data according to a preset time interval.
If the second thread has locked the target data with a mutual exclusion before the first thread locks the target data, then the first thread will not be able to lock the target data. That is, the first thread will cause a lock failure when locking the target data, in which case the first thread does not release the CPU resources, but continues to tie up the CPU resources and attempts to lock the target data again. If the lock again fails, the first thread may continue to attempt to lock the target data. That is, the first thread may lock the target data multiple times in succession. In addition, the first thread or CPU may count the number of times the first thread continuously locks the target data. If the number of times that the first thread continuously locks the target data reaches the preset number of times and the first thread does not successfully lock the target data, the first thread may lock the target data at the preset time interval. That is, if the first thread does not successfully lock the target data within a short time, for example, when the number of times of locking is less than a preset number of times, the first thread may continuously lock the target data. If the first thread is still not successful in locking the target data for a long period of time, e.g., when the number of consecutive locks reaches a preset number, the first thread may lock the target data at intervals.
According to the method and the device for locking the target data, the target data are locked in the database through the identification information of the target data to be accessed, if the locking fails, the target data can be continuously locked, if the number of times of continuously locking the target data reaches the preset number of times and the target data is not successfully locked, the target data can be locked according to the preset time interval, the thread in the computer system can not release CPU resources to enter a blocking state due to the fact that the target data cannot be locked, therefore, frequent change of the state of the thread is avoided, and time consumption caused by thread state change and thread scheduling can be effectively reduced under the conditions of high concurrency and resource competition.
On the basis of the foregoing embodiment, optionally, locking the target data in the database includes: and locking the target data in a database by adopting a counting method of atomic variables.
For example, the lock corresponding to the target data may be denoted as lock _ word. lock word may be a variable. If lock word is 0, it means that the target data is not locked. If lock word is 1, it indicates that there is a lock on the target data.
For example, the initial value of lock _ word is 0. The second thread locks the target data prior to the first thread. Specifically, when the second thread locks the target data, the second thread may call a function sync _ pool _ match _ and _ swap, where the function may be represented as:
sync_bool_compare_and_swap(lock_word,old_val,new_val)
wherein, lock _ word, old _ val, and new _ val are three parameters of the function sync _ pool _ compare _ and _ swap respectively, old _ val represents the old expected value, and new _ val represents the value to be updated. Wherein lock _ word is a variable, old _ val is a fixed value, and new _ val is also a fixed value. For example, old _ val is 0 and new _ val is 1. In some embodiments, old _ val is 1 and new _ val is 0. This embodiment is schematically illustrated by taking old _ val as 0 and new _ val as 1 as an example.
Specifically, the function sync _ bone _ composition _ and _ swap has the following functions: and comparing whether the values of lock _ word and old _ val are the same, if the values of lock _ word and old _ val are the same, assigning the value of new _ val to lock _ word, and returning success (true). If the values of lock _ word and old _ val are not the same, then nothing is done and a failure (false) is returned.
Specifically, when the initial value of the lock _ word is 0, the second thread may change the lock _ word value to 1 by calling the function sync _ body _ compare _ and _ swap, that is, the second thread adds 1 to the lock _ word value by calling the function sync _ body _ compare _ and _ swap. Thereby realizing the function of locking the target data by the second thread.
It can be understood that, in general, adding 1 to the variable lock word needs to be implemented by two steps, the first step is: calculate lock word + 1. The second step is: lock word +1 is assigned to lock word. For example, the initial value of lock _ word is 0, the first step is to calculate 0+1, and the second step is to assign 0+1 to lock _ word, thereby implementing the addition of 1 to the lock _ word value. However, when multiple threads attempt to add 1 to lock _ word, an interrupt may occur between the two steps, resulting in multiple threads updating the same variable at the same time, which may result in erroneous results. For example, thread A and thread B attempt to add 1 to lock _ word at the same time, i.e., thread A and thread B perform the two steps described above, respectively. When thread B finishes executing the first step, i.e. 0+1, an interrupt occurs, i.e. thread B has not yet had time to assign 0+1 to lock _ word, so that lock _ word remains at the initial value of 0. If the thread A successfully completes the two steps when the thread B is interrupted, namely the thread A calculates 0+1 and assigns 0+1 to the lock _ word, the thread A successfully updates the variable lock _ word. After the interruption of the thread B is finished, the thread B can continuously assign 0+1 to the lock _ word, so that the thread B can also successfully update the variable lock _ word. This results in both thread a and thread B successfully updating the variable lock word. If lock _ word equals 1, then thread a and thread B both attempt to add 1 to lock _ word successfully, resulting in thread a and thread B simultaneously locking the target data. Further, the thread a and the thread B can access the target data at the same time, and if the thread a and the thread B modify the target data at the same time, inconsistency of the data in the database will be caused.
By sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val) as described above, the operation of adding 1 to the lock _ word value can be made uninterrupted. So that when multiple threads attempt to add 1 to the lock word, only one of the threads can successfully update the value of the variable lock word, while the other threads cannot successfully update the value of the variable lock word. That is, sync _ bone _ compare _ and _ swap (lock _ word, old _ val, new _ val) changes the two steps as described above into an uninterruptible operation. The sync _ pool _ compare _ and _ swap (lock _ word, old _ val, new _ val) function can be used as an atomic operation function. The atomic operation function may specifically be a function that employs an atomic variable property. Where an atomic variable is the basic unit of an atomic operation. By adopting the atomic variable, the threads can be prevented from abandoning CPU resources, so that the locking performance is improved.
The function sync _ bone _ compare _ and _ swap (lock _ word, old _ val, new _ val) in this embodiment may be a function encapsulated in C + +. In other embodiments, functions in other programming languages may also be employed to achieve the same or similar functionality.
In addition, if the second thread needs to unlock the target data, the second thread may call a function sync _ add _ and _ fetch (lock _ word, around), to implement subtracting 1 from lock _ word. The amount may be a fixed value, for example, the amount may be 1, and the role of sync _ add _ and _ fetch may specifically be: the difference between lock word and around (lock word around) is calculated and assigned to lock word. Since the lock _ word becomes 1 after the second thread locks the target data, the value of the lock _ word can be changed to 0 by calling sync _ add _ and _ fetch (lock _ word, amount) through the second thread, thereby realizing that the second thread unlocks the target data.
In this embodiment, if the second thread locks the target data, when the first thread needs to access the target data, the first thread also needs to lock the target data. In this case, the first thread needs to wait for the second thread to release the lock before locking the target data. In this embodiment, during the process of the first thread releasing the lock, the first thread does not release the CPU resource to enter the blocking state, but the first thread may continue to occupy the CPU resource and continuously call the function sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val).
The first thread attempts to lock the target data by calling the function sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val). For example, the maximum number of times that the first thread continuously calls the function sync _ pool _ match _ and _ swap (lock _ word, old _ val, new _ val) may be preset to 50 times. The process of the first thread continuously calling the function is the process of the first thread attempting to continuously lock the target data.
Optionally, in the process of continuously locking the target data, the thread for executing the data access request occupies a central processing unit CPU resource.
For example, the first thread is the thread that executes the data access request described above. In the process of continuously locking the target data by the first thread, the first thread can continuously occupy the CPU resource of the central processing unit, and even if the locking of the first thread fails, the first thread cannot be suspended, but continuously tries to lock the target data.
The logic for the first thread to continuously lock the target data may be represented as follows:
for(i=0;i<50;i++)
{if(x_lock(lock_word))
return;
}
wherein, x _ lock (lock _ word) represents the first thread call function sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val). i represents a variable, and the first thread calls the function sync _ pool _ compare _ and _ swap (lock _ word, old _ val, new _ val) every time i is added by 1. Each time the first thread calls the function, if the function sync _ pool _ match _ and _ swap (lock _ word, old _ val, new _ val) returns a success (true), it indicates that lock _ word becomes 0, e.g., the second thread releases the lock, so that lock _ word becomes 0. Since lock _ word _ val, and further lock _ word _ new _ val, for example, the first thread may lock the target data so that lock _ word _ 1. That is, when sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val) returns success (true), it indicates that the first thread successfully locks the target data. At this point, the first thread jumps out of the for loop. If the function sync _ pool _ match _ and _ swap (lock _ word, old _ val, new _ val) returns a failure (false) each time the first thread calls the function, it indicates that the first thread fails to lock the target data, and continues to execute the for loop. Typically, if the second thread has a short time to hold the target data, the first thread may successfully lock the target data within 50 cycles as described above.
If the first thread does not successfully lock the target data at the end of the 50 cycles as described above, the first thread may continue to occupy the CPU resources and call the function sync _ pool _ compare _ and _ swap (lock _ word, old _ val, new _ val) at the preset time interval, that is, the first thread may continue to occupy the CPU resources and lock the target data at the preset time interval. Optionally, the greater the number of times of locking failure of the target data is, the greater the preset time interval is.
Specifically, the logic that the first thread locks the target data at the preset time interval may be expressed as follows:
for(i=1;i<44;i++)
{if(gm_rw_lock_x_lock_low(lock))
return;
else
the first thread enters the dormancy, and the dormancy time T is fib (i)
}
Wherein, gm _ rw _ lock _ x _ lock (lock) represents the first thread call function sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val). i represents a variable, and the first thread calls the function sync _ pool _ compare _ and _ swap (lock _ word, old _ val, new _ val) every time i is added by 1. Each time the first thread calls the function, if the function sync _ pool _ match _ and _ swap (lock _ word, old _ val, new _ val) returns a success (true), it indicates that lock _ word becomes 0, e.g., the second thread releases the lock, so that lock _ word becomes 0. Since lock _ word _ val, and further lock _ word _ new _ val, for example, the first thread may lock the target data so that lock _ word _ 1. That is, when sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val) returns success (true), it indicates that the first thread successfully locks the target data. At this point, the first thread jumps out of the for loop. If the function sync _ bolt _ compare _ and _ swap (lock _ word, old _ val, new _ val) returns a failure (false) each time the first thread calls the function, it indicates that the first thread fails to lock the target data. Further, the first thread enters sleep, and the sleep time T ═ fib (i). In this embodiment, the larger the number of times of the first thread failing to lock the target data, the larger the preset time interval is. That is, during the cycle for (i ═ 1; i < 44; i + +) as described above, if i is larger and the first thread locks or fails to lock the target data at the i-th cycle, the larger the sleep time T required for the first thread to go to sleep, i.e., the larger the preset time interval. In the present embodiment, the preset time interval may be an increase in continuity, or may be an increase in non-continuity. For example, 1,2,3, 4 … is an increase in continuity, and 1,3, 6, 7, 9 … is an increase in non-continuity. The present embodiment describes the preset time interval by taking an example of the increase of the discontinuity, for example, the trend of the increase of the discontinuity of the preset time interval conforms to the increasing trend of the numerical values in the fibonacci number series. For example, fib (i) denotes a fibonacci number sequence, and i denotes the i-th number value in the fibonacci number sequence, and i in this case may be i in for (i ═ 1; i < 44; i + +), for example. The number of values included in the fibonacci number series is not limited in this embodiment. For example, the illustration is schematically made by taking the example that the fibonacci number series includes 43 numerical values. At this time, the fibonacci number series can be expressed as: fib (43) {1,1,2,3,5,8,13,21,34,55 … }. In a fibonacci number series, each value may also be referred to as a term, starting with term 3, each term being equal to the sum of the first two terms. The fibonacci sequence can be pre-stored in memory, thereby saving time by not calculating each item each time.
In summary, if the first thread has not successfully locked the target data at the end of 50 cycles as described above, the first thread may continue to occupy CPU resources and lock the target data at a preset time interval, where the discontinuous increase trend of the preset time interval corresponds to the increase trend of the values in the fibonacci number series, that is, the greater the number of cycles in the cycle for (i ═ 1; i < 44; i + +) as described above, the longer the first thread sleeps when the first thread fails to lock the target data, and the increase trend of the time adopts the increase trend of the values in the fibonacci number series.
Optionally, in the preset time interval, the thread for executing the data access request occupies a CPU resource of the central processing unit.
For example, during hibernation of the first thread, the first thread may continue to occupy CPU resources while both the first thread and the CPU are in a suspended state. In other embodiments, the first thread may also release CPU resources during the hibernation of the first thread.
It will be appreciated that, in general, waiting for a first thread to release a lock on target data by a second thread involves the following 3 processes:
1) the first thread enters a ready state from an execution state.
2) The second thread releases the lock on the target data.
3) The first thread enters an execution state from a ready state.
Among them, the processes 1) and 3) require a long time to be consumed. In this embodiment, the first thread may continuously occupy CPU resources while waiting for the second thread to release the lock, and attempt to continuously lock the target data, or attempt to continuously lock the target data and then attempt to lock the target data at a preset time interval, that is, the first thread does not need to enter the ready state from the execution state and enter the execution state from the ready state while waiting for the second thread to release the lock, so time consumption is reduced.
In the process of releasing the lock by the second thread through the first thread, the first thread does not release the CPU resource to enter the blocking state, but continuously occupies the CPU resource and continuously calls the locking function to try to lock the target data. If the first thread can not successfully lock the target data in a short time, the first thread circularly calls the locking function, sleeps continuously and seizes the lock. If the first thread is still unable to successfully lock the target data after a long time, the first thread may lock the target data at preset time intervals, specifically, if the first thread is unable to successfully lock, the first thread sleeps, the sleep time result continues to try to lock the target data, and the sleep time increases with the increase of the number of times of sleep, and the increase trend of the sleep time adopts the increasing trend of the numerical values in the fibonacci number series. Because the increasing trend of the numerical values in the Fibonacci number sequence is slow and does not increase rapidly like the numerical values in the exponential number sequence, the sleep time of the first thread can be controlled by adopting the Fibonacci number sequence, so that the increase of the sleep time of the first thread can accord with the golden division point of the Fibonacci number sequence, the difference between the sleep time of the first thread and the sleep time of the second thread is better, the slow increasing waiting is realized, and the resource consumption of a CPU is reduced.
Fig. 3 is a schematic structural diagram of an implementation apparatus of a database lock according to an embodiment of the present disclosure. The apparatus for implementing a database lock according to the embodiment of the present disclosure may execute the processing procedure provided in the embodiment of the method for implementing a database lock, as shown in fig. 3, the apparatus 30 for implementing a database lock includes:
an obtaining module 31, configured to obtain a data access request, where the data access request includes identification information of target data to be accessed;
a locking module 32, configured to lock the target data in a database according to the identification information of the target data; and if the continuous locking times of the target data reach preset times and the locking of the target data fails, locking the target data according to a preset time interval.
Optionally, the greater the number of times of locking failure of the target data is, the greater the preset time interval is.
Optionally, the locking module 32 is specifically configured to: and locking the target data in a database by adopting a counting method of atomic variables.
Optionally, in the process of continuously locking the target data, the thread for executing the data access request occupies a central processing unit CPU resource.
Optionally, in the preset time interval, the thread for executing the data access request occupies a CPU resource of the central processing unit.
The implementation apparatus of the database lock in the embodiment shown in fig. 3 may be used to implement the technical solution of the above method embodiment, and its implementation principle and technical effect are similar, which are not described herein again.
Fig. 4 is a schematic structural diagram of an implementation device of a database lock according to an embodiment of the present disclosure. The implementation device of the database lock provided in the embodiment of the present disclosure may execute the processing flow provided in the implementation method embodiment of the database lock, as shown in fig. 4, the implementation device 40 of the database lock includes: memory 41, processor 42, computer programs and communication interface 43; wherein the computer program is stored in the memory 41 and is configured to be executed by the processor 42 in a method for implementing a database lock as described above.
In addition, the embodiment of the present disclosure also provides a computer-readable storage medium, on which a computer program is stored, where the computer program is executed by a processor to implement the method for implementing the database lock according to the above embodiment.
It is noted that, in this document, relational terms such as "first" and "second," and the like, may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
The foregoing are merely exemplary embodiments of the present disclosure, which enable those skilled in the art to understand or practice the present disclosure. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the disclosure. Thus, the present disclosure is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (12)

1. A method for implementing a database lock, the method comprising:
acquiring a data access request, wherein the data access request comprises identification information of target data to be accessed;
locking the target data in a database according to the identification information of the target data;
and if the continuous locking times of the target data reach preset times and the locking of the target data fails, locking the target data according to a preset time interval.
2. The method of claim 1, wherein the greater the number of lock failures to the target data, the greater the predetermined time interval.
3. The method of claim 2, wherein locking the target data in a database comprises:
and locking the target data in a database by adopting a counting method of atomic variables.
4. The method of claim 1, wherein the thread for executing the data access request occupies central processor resources during the continuous locking of the target data.
5. The method of claim 1, wherein a thread executing the data access request occupies central processor resources during the predetermined time interval.
6. An apparatus for implementing a database lock, comprising:
the system comprises an acquisition module, a storage module and a processing module, wherein the acquisition module is used for acquiring a data access request, and the data access request comprises identification information of target data to be accessed;
the locking module is used for locking the target data in a database according to the identification information of the target data; and if the continuous locking times of the target data reach preset times and the locking of the target data fails, locking the target data according to a preset time interval.
7. The apparatus of claim 6, wherein the preset time interval is larger as the number of times the target data is locked is larger.
8. The apparatus for implementing a database lock according to claim 7, wherein the locking module is specifically configured to:
and locking the target data in a database by adopting a counting method of atomic variables.
9. The apparatus of claim 6, wherein the thread for executing the data access request occupies central processor resources during the continuous locking of the target data.
10. The apparatus of claim 6, wherein the thread for executing the data access request occupies a central processor resource during the predetermined time interval.
11. An implementation device of a database lock, comprising:
a memory;
a processor; and
a computer program;
wherein the computer program is stored in the memory and configured to be executed by the processor to implement the method of any one of claims 1-5.
12. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-5.
CN202011041096.8A 2020-09-28 2020-09-28 Method, device and equipment for realizing database lock and readable storage medium Active CN112231131B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011041096.8A CN112231131B (en) 2020-09-28 2020-09-28 Method, device and equipment for realizing database lock and readable storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011041096.8A CN112231131B (en) 2020-09-28 2020-09-28 Method, device and equipment for realizing database lock and readable storage medium

Publications (2)

Publication Number Publication Date
CN112231131A true CN112231131A (en) 2021-01-15
CN112231131B CN112231131B (en) 2024-05-28

Family

ID=74119424

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011041096.8A Active CN112231131B (en) 2020-09-28 2020-09-28 Method, device and equipment for realizing database lock and readable storage medium

Country Status (1)

Country Link
CN (1) CN112231131B (en)

Citations (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH01125637A (en) * 1987-11-10 1989-05-18 Nippon Telegr & Teleph Corp <Ntt> Data access control system
JP2001188685A (en) * 1999-12-27 2001-07-10 Internatl Business Mach Corp <Ibm> Method and device for managing lock of object
CN101650646A (en) * 2009-09-22 2010-02-17 杭州华三通信技术有限公司 Method and device for realizing shared data consistency
CN104077111A (en) * 2014-06-24 2014-10-01 用友优普信息技术有限公司 Concurrent access control method and device for service operations
CN104252386A (en) * 2013-06-26 2014-12-31 阿里巴巴集团控股有限公司 Data update locking method and equipment
CN104461729A (en) * 2013-09-22 2015-03-25 腾讯科技(北京)有限公司 Resource sharing protecting method and device
US9471400B1 (en) * 2015-07-28 2016-10-18 International Business Machines Corporation Reentrant read-write lock algorithm
CN108345495A (en) * 2017-01-22 2018-07-31 北京国双科技有限公司 A kind of locking method and server of multithreading
CN108363613A (en) * 2017-12-28 2018-08-03 杭州马猴烧韭科技有限公司 A kind of locking method of exclusive lock, electronic equipment and storage medium
CN110399383A (en) * 2019-07-29 2019-11-01 中国工商银行股份有限公司 Applied to the data processing method of server, device, calculate equipment, medium
US20190356323A1 (en) * 2018-05-15 2019-11-21 Perceptia Ip Pty Ltd PLL with Beat-Frequency Operation
CN110990161A (en) * 2019-11-15 2020-04-10 北京浪潮数据技术有限公司 Shared resource access method, device, equipment and computer readable storage medium
CN111324465A (en) * 2020-02-19 2020-06-23 深圳壹账通智能科技有限公司 Multithread calling method and device, computer system and storage medium
CN111399991A (en) * 2020-06-05 2020-07-10 腾讯科技(深圳)有限公司 Virtual resource locking method and device, storage medium and electronic device
CN111444214A (en) * 2020-03-31 2020-07-24 青岛海信网络科技股份有限公司 Method and device for processing large-scale data and industrial monitoring memory database
CN111459963A (en) * 2020-04-07 2020-07-28 中国建设银行股份有限公司 Core accounting transaction concurrent processing method and device
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment

Patent Citations (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH01125637A (en) * 1987-11-10 1989-05-18 Nippon Telegr & Teleph Corp <Ntt> Data access control system
JP2001188685A (en) * 1999-12-27 2001-07-10 Internatl Business Mach Corp <Ibm> Method and device for managing lock of object
CN101650646A (en) * 2009-09-22 2010-02-17 杭州华三通信技术有限公司 Method and device for realizing shared data consistency
CN104252386A (en) * 2013-06-26 2014-12-31 阿里巴巴集团控股有限公司 Data update locking method and equipment
CN104461729A (en) * 2013-09-22 2015-03-25 腾讯科技(北京)有限公司 Resource sharing protecting method and device
CN104077111A (en) * 2014-06-24 2014-10-01 用友优普信息技术有限公司 Concurrent access control method and device for service operations
US9471400B1 (en) * 2015-07-28 2016-10-18 International Business Machines Corporation Reentrant read-write lock algorithm
CN108345495A (en) * 2017-01-22 2018-07-31 北京国双科技有限公司 A kind of locking method and server of multithreading
CN108363613A (en) * 2017-12-28 2018-08-03 杭州马猴烧韭科技有限公司 A kind of locking method of exclusive lock, electronic equipment and storage medium
US20190356323A1 (en) * 2018-05-15 2019-11-21 Perceptia Ip Pty Ltd PLL with Beat-Frequency Operation
CN110399383A (en) * 2019-07-29 2019-11-01 中国工商银行股份有限公司 Applied to the data processing method of server, device, calculate equipment, medium
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment
CN110990161A (en) * 2019-11-15 2020-04-10 北京浪潮数据技术有限公司 Shared resource access method, device, equipment and computer readable storage medium
CN111324465A (en) * 2020-02-19 2020-06-23 深圳壹账通智能科技有限公司 Multithread calling method and device, computer system and storage medium
CN111444214A (en) * 2020-03-31 2020-07-24 青岛海信网络科技股份有限公司 Method and device for processing large-scale data and industrial monitoring memory database
CN111459963A (en) * 2020-04-07 2020-07-28 中国建设银行股份有限公司 Core accounting transaction concurrent processing method and device
CN111399991A (en) * 2020-06-05 2020-07-10 腾讯科技(深圳)有限公司 Virtual resource locking method and device, storage medium and electronic device

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
丰树谦, 李体红: "多用户数据库应用程序的开发与设计", 焦作大学学报, no. 02, pages 63 - 66 *
长孙泰: "自旋锁spinlock剖析与改进", pages 1 - 10, Retrieved from the Internet <URL:https://kb.cnblogs.com/page/105657/> *
马刚;刘天时;赵安科;: "数据库并发控制技术", 现代电子技术, no. 12, pages 101 - 103 *

Also Published As

Publication number Publication date
CN112231131B (en) 2024-05-28

Similar Documents

Publication Publication Date Title
US8463911B2 (en) Exclusive control method of resource and exclusive controller of resource
CN101833475B (en) Method and device for execution of instruction block
US8938631B2 (en) Energy efficient implementation of read-copy update for light workloads running on systems with many processors
US9003420B2 (en) Resolving RCU-scheduler deadlocks
US20080209422A1 (en) Deadlock avoidance mechanism in multi-threaded applications
US8769546B2 (en) Busy-wait time for threads
US8413163B2 (en) Program control device including per-timeslot switching of thread execution
US9086911B2 (en) Multiprocessing transaction recovery manager
JPH1115793A (en) Protection method for resource maintainability
CN112241400A (en) Method for realizing distributed lock based on database
WO2011096163A1 (en) Information processing system, exclusive control method, and program for exclusive control
US6662364B1 (en) System and method for reducing synchronization overhead in multithreaded code
CN113448701A (en) Multi-process outbound control method, system, electronic equipment and storage medium
CN111984428A (en) Method, device and equipment for realizing spin lock during resource access
CN107305507B (en) Deadlock control method and device
CN112231131B (en) Method, device and equipment for realizing database lock and readable storage medium
US10430234B2 (en) Thread coordination in a rule engine using a state machine
CN110908968A (en) Method, device, equipment and storage medium for avoiding frightened groups during file lock unlocking
CN116028189A (en) Multithreading service exit method, device, storage medium and computer equipment
CN112463419B (en) Main and standby node working method and device based on middleware and electronic equipment
CN113961364A (en) Large-scale lock system implementation method and device, storage medium and server
CN114327828B (en) Lock-free concurrent access method, device, equipment and medium for shared data
CN112612582B (en) Semaphore function implementation method and device
CN113806031B (en) Method and device for protecting resources through object lock
CN113806099B (en) Binary calculation-based multi-core spin lock design method

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant