A database modification method, apparatus, device and medium
By determining the locking information in the conflict transaction registry when releasing the target transaction lock object and determining the execution strategy based on the registry information, the problem of multiple transactions waking up at the same time is avoided, thus improving the concurrent execution efficiency of the database system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHANGHAI DAMENG DATABASE
- Filing Date
- 2022-04-01
- Publication Date
- 2026-04-28
AI Technical Summary
In existing technologies, concurrent conflicts caused by multiple transactions waking up simultaneously increase the resource consumption and maintenance costs of database row locks, and reduce system execution efficiency.
By determining the registration status of the lock information of the target transaction in the conflict transaction registry when releasing the transaction lock object of the target transaction, and determining the execution strategy of the waiting transaction based on the registration status, conflicting transactions are avoided from being woken up at the same time, ensuring that only one transaction is actually started and executed.
This improves the efficiency of concurrent execution of the system and avoids invalid data queries and modifications when conflicting transactions are woken up simultaneously.
Smart Images

Figure CN114676115B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a database modification method, apparatus, device, and medium. Background Technology
[0002] Database systems allow multiple users to access and modify data concurrently. The concurrent operations of different users ultimately manifest as the concurrent execution of different transactions. It is possible for multiple transactions to access or modify the same row of data at the same time. In existing technologies, in order to ensure the correctness and consistency of data, database systems must implement concurrency control. The most common method is locking. Before modifying data, the database object is locked (X lock). During this period, the access and modification actions of other transactions will be blocked. After the data modification is completed, the X lock is released to ensure the correctness and consistency of data under concurrent conditions.
[0003] In existing technologies, to reduce the resource consumption and maintenance costs of database row locks, one row lock optimization method is to use the transaction identifier (TID) as the locking target, replacing traditional row-level locking. When a transaction starts, it first locks the current transaction ID (TID) using the X-lock mode. Furthermore, after performing insert, delete, or update operations, simply writing the current transaction ID into the TID field of the record is equivalent to applying an X-lock to that row as well, reducing the consumption of row lock resources. Before a transaction ends, if another transaction wants to modify the same record, it first reads the TID value from the record and determines whether the record is visible to it according to the visibility rules of multi-version concurrency control. Since transaction 1 has not yet committed, this record is not visible to transaction 2. When transaction 2 applies an X lock to the current TID value in the record, it will be blocked until transaction 1 ends and wakes it up, thus achieving the purpose of controlling the concurrency of multiple transactions. This locking strategy does not require additional locking when there is no record update conflict. However, in many scenarios in the existing technology, there may still be situations where multiple update conflicting transactions are still woken up at the same time, which will cause unnecessary concurrency conflicts, increase the resource consumption and maintenance cost of database row locks, and make the system execution inefficient.
[0004] Public content
[0005] In view of this, the present invention provides a database modification method, apparatus, device and medium that can avoid invalid data queries and modifications when conflicting transactions are woken up at the same time, thereby improving the efficiency of concurrent execution of the system.
[0006] According to one aspect of the present invention, an embodiment of the present invention provides a database modification method, the method comprising:
[0007] When releasing the transaction lock object of the target transaction and waking up the waiting transactions, determine the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registry;
[0008] The execution strategy for the waiting transaction is determined based on the registration information;
[0009] The execution strategy is used to control the data modification of the waiting transactions.
[0010] According to another aspect of the present invention, embodiments of the present invention also provide a database modification method apparatus, the apparatus comprising:
[0011] The registration and determination module is used to determine the registration status of the locking information of the target transaction record in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction.
[0012] The strategy determination module is used to determine the execution strategy of the waiting transaction based on the registration information;
[0013] The data modification module is used to control the modification of data by the waiting transactions according to the execution strategy.
[0014] According to another aspect of the present invention, embodiments of the present invention also provide an electronic device, the electronic device comprising:
[0015] At least one processor; and
[0016] A memory communicatively connected to the at least one processor; wherein,
[0017] The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the database modification method according to any embodiment of the present invention.
[0018] According to another aspect of the present invention, embodiments of the present invention also provide a computer-readable storage medium storing computer instructions for causing a processor to execute and implement the database modification method described in any embodiment of the present invention.
[0019] The technical solution of this invention, by determining the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction, determines the execution strategy of the waiting transaction based on the registration status, and controls the modification of data by the waiting transaction according to the execution strategy. This invention, by determining the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction, and determining the execution strategy of the waiting transaction based on the registration status, avoids invalid data queries and modifications after conflicting transactions are simultaneously woken up, thus improving the efficiency of concurrent execution of the system.
[0020] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 This is a flowchart of a database modification method provided in Embodiment 1 of the present invention;
[0023] Figure 2 This is a flowchart of a database modification method provided in Embodiment 2 of the present invention;
[0024] Figure 3 This is a flowchart of a database modification method provided in Embodiment 3 of the present invention;
[0025] Figure 4 This is a structural block diagram of a database modification device provided in Embodiment 4 of the present invention;
[0026] Figure 5 A schematic diagram of an electronic device that can be used to implement embodiments of the present invention is shown. Detailed Implementation
[0027] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0028] It should be noted that the terms "target," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0029] In existing technologies, database modifications typically begin with data preparation: `Create table T(idint, val int); Insert into T values(1, 1); Commit;` The inserted record is identified as Rec1, and its Rowid value is identified as Rowid1. TrxA executes `Update T set val = val + 1 where id = 1;` updating the Rec1 record in table T. When TrxA starts, it locks its transaction number TidA in X mode, with the lock object being `LockA[TidA, X]`. After the update operation is executed, the TID field of Rec1 is written to TidA.
[0030] TrxB executes `Update T set val = val + 1 where id = 1;` to update the Rec1 record in table T. When TrxB starts, it locks its own transaction number TidB in X mode, with the lock object being `LockB[TidB, X]`. During the update operation, it reads the TID field of Rec1, which is TidA. Since TrxA is the active transaction, TrxB executes an S lock on TrxA's transaction number TidA, resulting in a lock wait. TrxB's lock object in the wait state is `LockB2[TidA, S, T, Rowid1]`, which conflicts with TrxA's `LockA[TidA, X]`.
[0031] TrxC executes `Update T set val = val + 1 where id = 1`, updating the Rec1 record in table T. Upon startup, TrxC locks its own transaction number TidC in X mode, with the lock object being `LockC[TidC, X]`. During the update operation, it reads the TID field of Rec1, which is TidA. Since TrxA is the active transaction, TrxC executes an S lock on TrxA's transaction number TidA, resulting in a lock wait. The lock object that TrxC is waiting for is `LockC2[TidA, S, T, Rowid1]`, which conflicts with `LockA[TidA, X]` of TrxA transaction and `LockB2[TidA, S, T, Rowid1]` of TrxB transaction.
[0032] TrxA commits, releases LockA[TidA, X], grants permissions to LockB2[TidA, S, T, Rowid1], and wakes up TrxB. TrxC's LockC2[TidA, S, T, Rowid1] continues to wait due to a conflict with LockB2[TidA, S, T, Rowid1]. After TrxB is woken up, it needs to re-execute the query operation to obtain the updated Rec1 record content, and then re-execute the update operation. After TrxB's update operation is completed, the TID field of the Rec1 record is modified to TidB.
[0033] TrxD executes `Update T set val = val + 1 where id = 1`, updating the Rec1 record in table T. When TrxD starts, it locks its own transaction number TidD in X mode, with the lock object being `LockD[TidD, X]`. During the update operation, it reads the TID field of Rec1, which is TidB. Since TrxB is an active transaction, TrxD executes an S lock on TrxB's transaction number TidB, resulting in a lock wait. TrxD's lock object in the wait state is `LockD2[TidB, S, T, Rowid1]`, which conflicts with TrxB's `LockB[TidB, X]`.
[0034] TrxB commits the transaction, releases LockB2[TidA, S, T, Rowid1], grants LockC2[TidA, S, T, Rowid1] permissions to wake up TrxC, releases LockB[TidB, X], grants LockD2[TidB, S, T, Rowid1] permissions to wake up TrxD.
[0035] In the above scenario, since LockC2 and LockD2 are locking different transaction numbers, it is not considered a locking conflict. Therefore, TrxC and TrxD are woken up at the same time, and each re-executes the query to obtain the updated record content. When the update operation is re-executed, since the two transactions are modifying the same row of records, TrxC and TrxD form a new update conflict. That is, multiple transactions with data update conflicts may still be woken up at the same time, thus causing unnecessary concurrency conflicts and reducing the system's execution efficiency.
[0036] In view of this, this application proposes a database modification method, apparatus, device and medium to ensure that although multiple conflicting transactions are awakened when a transaction is committed or rolled back, only one transaction is actually started and executed, while the other conflicting transactions remain in a locked waiting state. This avoids invalid data queries and data modifications after conflicting transactions are awakened at the same time, thereby improving the system's concurrent execution efficiency.
[0037] Example 1
[0038] In one embodiment, Figure 1 This is a flowchart of a database modification method provided in Embodiment 1 of the present invention. This embodiment is applicable to situations where data in a database is modified. The method can be executed by a database modification device, which can be implemented in hardware and / or software and can be configured in an electronic device. Figure 1 As shown, the method includes:
[0039] S110. When releasing the transaction lock object of the target transaction and waking up the waiting transaction, determine the registration status of the lock information of the target record of the waiting transaction in the conflict transaction registry.
[0040] In this context, the target transaction can be understood as the transaction released during a transaction commit or rollback. A transaction lock object can be understood as the lock object corresponding to a transaction lock that the target transaction uses to lock a specific transaction identifier. Transaction locks can be used to lock transactions when there is a conflict between the target transaction and waiting transactions. A transaction lock object may contain the identifier of the transaction being locked by the target transaction, the locking mode of the target transaction, the identifier of the data table modified by the target transaction, and the row identifier of the record where the modified data is located, etc., but this embodiment does not impose any limitations. A waiting transaction can be understood as a transaction that conflicts with the target transaction, and this waiting transaction needs to be awakened when the target transaction performs a transaction commit or rollback operation. The target record can be understood as the record that the waiting transaction about to be awakened will modify.
[0041] In this embodiment, the locking information can be understood as the information recorded in the transaction lock object of the target transaction. Specifically, the locking information may include the transaction identifier locked by the target transaction, the locking mode of the target transaction, the table identifier modified by the target transaction, the row identifier of the record modified by the target transaction, and the transaction identifier that the target transaction may re-lock.
[0042] It should be noted that the content recorded in the conflict transaction registry may include the number of conflicts, an array of table identifiers modified by the conflicting transactions, an array of row identifiers modified by the conflicting transactions, and an array of blocking transaction identifiers of the conflicting transactions. Of course, the conflict transaction registry can also be implemented using a hash table, which can speed up the lookup of blocking information and reduce the array scanning cost when checking the conflict transaction table. This embodiment does not impose any restrictions on this approach.
[0043] In this embodiment, when the target transaction commits or rolls back, releases the transaction lock object, and wakes up waiting transactions, it is necessary to check whether the locking information of the target record of the waiting transaction has been registered in the conflict transaction registry. Specifically, when the target transaction commits or rolls back, releases the transaction lock object, and wakes up waiting transactions, the conflict transaction registry can be traversed to check whether there is a modified table identifier number, a row identifier number where the modified record is located, etc., that are the same as the target record of the waiting transaction. If they exist, it indicates that the locking information of the target record of the waiting transaction has been registered; conversely, if they do not exist, it indicates that the locking information of the target record of the waiting transaction has not been registered.
[0044] In one embodiment, the blocking information includes at least one of the following: the blocking transaction identifier, the blocking mode, the modified table identifier, the modified record row identifier, and the re-blocking transaction identifier.
[0045] In this context, the blocking transaction identifier can be understood as the identifier of the transaction being blocked by the target transaction. For example, the identifier of the transaction being blocked by the target transaction could be transaction A, transaction B, or transaction C; this embodiment does not impose any restrictions. The blocking mode can be a shared block (S-block) or an exclusive block (X-block); this embodiment does not impose any restrictions. The modified table identifier can be understood as the table identifier modified by the target transaction before committing or rolling back. The modified record row identifier can be understood as the row identifier of the record modified by the target transaction before committing or rolling back. The re-blocking transaction identifier can be understood as the transaction identifier that needs to be re-blocked when a new blocking request is initiated.
[0046] In this embodiment, the blocking information of the target record includes at least: the blocking transaction identifier, the blocking mode, the modification table identifier, the modification record row identifier, and the re-blocking transaction identifier, so as to check whether the blocking information of the target record has been registered in the conflict transaction registry table based on the target record.
[0047] S120. Determine the execution strategy for pending transactions based on the registration information.
[0048] The execution strategy can be understood as the strategy for the next step of the waiting transaction, determined by the registration status of the blocking information recorded by the target of the waiting transaction. The execution strategy can be a strategy for the waiting transaction to continue waiting, or a strategy for continuing to execute and perform data processing; this embodiment does not impose any restrictions.
[0049] In this embodiment, the registration scenarios can be divided into two types. One is that when the target transaction is committed or rolled back, after releasing the transaction lock object of the target transaction and before waking up the waiting transactions, the system checks if there is any locking information for the target record of the waiting transaction registered in the conflict transaction registry. The other is that when the target transaction is committed or rolled back, after releasing the transaction lock object of the target transaction and before waking up the waiting transactions, the system checks if there is any locking information for the target record of the waiting transaction already registered in the conflict transaction registry. Of course, the strategies executed for these two different registration scenarios are different.
[0050] In this embodiment, the strategy for determining whether to continue waiting or continue data processing after the waiting transaction is awakened can be based on the registration status of the blocking information recorded in the target record of the waiting transaction. Specifically, if the blocking information of the target record of the waiting transaction has already been registered, there is no need to register the blocking information again, and the execution strategy after the waiting transaction is awakened can be the continue waiting strategy. Conversely, if the blocking information of the target record of the waiting transaction has not been registered, the blocking information needs to be registered in the conflict transaction registration table, and the execution strategy after the waiting transaction is awakened can be the processing strategy to continue data processing operations.
[0051] S130. Control the data modification of waiting transactions according to the execution strategy.
[0052] In this context, modifying data can be understood as inducing waiting transactions to modify data according to the corresponding execution strategy.
[0053] In this embodiment, a corresponding execution strategy can be used to enable waiting transactions to modify data. It should be noted that data modification can be achieved by registering the blocking information in the conflict transaction registry table if the blocking information for the target record of the waiting transaction has not been registered before, then re-querying the updated data and re-executing the corresponding data modification operation after the waiting transaction is awakened. Alternatively, if the blocking information for the target record of the waiting transaction has already been registered, then after the transaction is awakened, a new blocking request is initiated based on the blocking information registered in the conflict transaction registry table, a new transaction lock object is determined, and the transaction is re-locked, putting the transaction in a continuing waiting state. This ensures that only one transaction is actually started and executed, while other conflicting transactions remain in a locked waiting state.
[0054] The technical solution described in this invention, by determining the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction, determines the execution strategy of the waiting transaction based on the registration status, and controls the modification of data by the waiting transaction according to the execution strategy. This invention, by determining the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction, and determining the execution strategy of the waiting transaction based on the registration status, avoids invalid data queries and modifications after conflicting transactions are simultaneously woken up, thus improving the efficiency of concurrent execution of the system.
[0055] In one embodiment, the database modification method further includes:
[0056] Initialize the conflict transaction registry, which includes setting the number of conflicts in the conflict transaction registry to 0, and setting the arrays of modified table identifiers, modified row identifiers, and blocked transaction identifiers in the conflict transaction registry to be empty.
[0057] In this embodiment, the conflict transaction registration table needs to be initialized first. Initializing the conflict transaction registration table may include: setting the number of conflicts in the conflict transaction registration table to 0, and setting the array of table identifiers modified by conflict transactions, the array of row identifiers modified by conflict transactions, and the array of blocking transaction identifiers of conflict transactions in the conflict transaction registration table to be empty.
[0058] Example 2
[0059] In one embodiment, Figure 2 This is a flowchart of a database modification method provided in Embodiment 2 of the present invention. Based on the above embodiments, this embodiment further refines the determination of the registration status of the blocking information of the target transaction in the conflict transaction registration table, and the determination of the execution strategy for waiting transactions based on the registration status. Figure 2 As shown, the database modification method in this embodiment may specifically include the following steps:
[0060] S210. When releasing the transaction lock object of the target transaction and waking up the waiting transaction, iterate through the conflict transaction registry to find whether there is a lock information for the target record of the waiting transaction. If yes, execute S220; otherwise, execute S230.
[0061] S220. The registration status is that the blocking information has been registered. The execution strategy is determined to be a waiting strategy. The waiting strategy includes: setting the re-blocking transaction identifier in the transaction lock object, and waiting transactions re-blocking according to the re-blocking transaction identifier.
[0062] The waiting strategy can be understood as a strategy that, when a blocking information has been registered, causes a waiting transaction to be awakened and then re-blocked according to the re-blocking transaction identifier, so that the waiting transaction continues to be in a waiting state.
[0063] In this embodiment, the conflict transaction registry is checked. If the blocking information has already been registered, the execution strategy can be to obtain the blocking transaction identifier of the conflicting transaction registered in the conflict transaction registry, set this identifier as the re-blocking transaction identifier, and then wake up the waiting transaction. After the waiting transaction is woken up, it can reconstruct a new transaction lock object based on the re-blocking transaction identifier, blocking mode, modified table identifier, and modified row identifier, and then lock it, so that the current transaction continues to be in a waiting state. When the waited transaction commits or rolls back, it will be woken up again. The blocking mode can be either S-blocking or X-blocking; this embodiment does not impose any restrictions.
[0064] In this embodiment, when releasing the transaction lock object of the target transaction and waking up the waiting transaction, the conflict transaction registration table is traversed to check whether there is any locking information for the target record of the waiting transaction. If it exists, it means that the locking information for the target record of the waiting transaction has been registered and there is no need to perform a duplicate registration operation.
[0065] In one embodiment, waiting for a transaction to re-lock based on a re-lock transaction identifier includes:
[0066] Obtain the relock request from the waiting transaction, and create a new transaction lock object for the waiting transaction according to the relock request;
[0067] The waiting transaction re-executes the lock according to the new transaction lock object.
[0068] In this context, a re-lock request can be understood as a newly initiated lock request. It can be a shared lock (S-lock mode) or an exclusive lock (X-lock mode), and this embodiment does not impose any restrictions. A new transaction lock object can be understood as a new transaction lock object determined based on the new lock request. Of course, the new transaction lock object contains the locking transaction identifier, which is the re-locking transaction identifier, the current lock mode, the modified table identifier, and the row identifier where the modified record is located.
[0069] It should be noted that when creating a new transaction lock object for a waiting transaction according to a relock request, the locking transaction identifier is the relock transaction identifier that has already been set. Before the waiting transaction is awakened, the locking transaction identifier of the conflicting transaction is obtained from the conflicting transaction registry table and set as the relock transaction identifier. After the waiting transaction is awakened, the locking is re-locked with a locking request in either S-lock mode or X-lock mode.
[0070] In this embodiment, when a relock request for a waiting transaction is obtained, a new transaction lock object for the waiting transaction can be created according to the relock request to relock it.
[0071] S230. The registration status is that the blocking information has not been registered before. The execution strategy is determined to be the processing strategy. The processing strategy includes: registering the blocking information in the conflict transaction registration table, waiting for the transaction to query the updated record content, and re-executing the data update operation.
[0072] The processing strategy can be understood as registering the blocking information and performing corresponding data processing operations when the blocking information has not been registered before.
[0073] In this embodiment, when releasing the transaction lock object of the target transaction and waking up the waiting transaction, the conflict transaction registration table is traversed to check whether there is any locking information for the target record of the waiting transaction. If the registration status is that the locking information has not been registered, the locking information needs to be registered. The execution strategy is to register the locking information in the conflict transaction registration table, and after the waiting transaction is woken up, the waiting transaction queries the updated record content and re-executes the data update operation. For example, data insertion, deletion and update operations are performed on the table in the database.
[0074] Specifically, registering blocking information in the conflict transaction registration table may include: adding the table identifier of the blocking information to the modified table identifier array in the conflict transaction registration table; adding the row identifier of the blocking information to the row identifier array of the conflict transaction modification registered in the conflict transaction registration table; adding the blocking transaction identifier of the blocking information to the blocking transaction identifier array of the conflict transaction registered in the conflict transaction registration table; and incrementing the number of conflicts in the conflict transaction registration table by 1.
[0075] In one embodiment, registering the blocking information in the conflict transaction registry includes:
[0076] Add the table identifier of the blocked information to the modified table identifier array in the conflict transaction registry;
[0077] Add the row identifier number of the blocked information to the modified row identifier number array in the conflict transaction registry;
[0078] Add the blocking transaction identifier of the blocking information to the blocking transaction identifier array in the conflict transaction registry table;
[0079] Increment the number of conflicts in the conflict registration table by 1.
[0080] The modified table identifier array can be composed of modified table identifiers; the modified row identifier array can be composed of modified row identifiers; and the locked transaction identifier array can be composed of locked transaction identifiers. For example, the modified table identifier array can be represented by tabids[]; the modified row identifier array can be represented by rowids[]; and the locked transaction identifier array can be represented by tids[], but this embodiment does not impose any restrictions.
[0081] In this embodiment, registering the blocking information in the conflict transaction registration table when the blocking information has not been registered before may include: adding the table identifier of the blocking information to the modified table identifier array in the conflict transaction registration table; adding the row identifier of the blocking information to the row identifier array of the modified conflict transaction registered in the conflict transaction registration table; adding the blocking transaction identifier of the blocking information to the blocking transaction identifier array of the conflict transaction registered in the conflict transaction registration table; and incrementing the number of conflicts in the conflict transaction registration table by 1.
[0082] The technical solution described in this invention involves, when releasing the transaction lock object of the target transaction and waking up the waiting transaction, traversing the conflict transaction registration table to check if there is any locking information for the target record of the waiting transaction; if so, it indicates that the locking information has been registered; if not, it indicates that the locking information has not been registered. If the locking information has been registered, the execution strategy is determined to be a waiting strategy, which includes: setting a re-locking transaction identifier in the transaction lock object, and the waiting transaction re-locking according to the re-locking transaction identifier; if the locking information has not been registered, the execution strategy is determined to be a processing strategy, which includes: registering the locking information in the conflict transaction registration table, the waiting transaction querying the updated record content, and re-executing the data update operation. In this embodiment of the invention, when releasing the transaction lock object of the target transaction and waking up waiting transactions, the system iterates through the conflict transaction registration table to check if there is any locking information for the target record of the waiting transaction. If the locking information has been registered, the execution strategy is determined to be a waiting strategy; if the locking information has not been registered, the execution strategy is determined to be a processing strategy. This ensures that although multiple conflicting transactions are woken up when a transaction is committed or rolled back, only one transaction actually starts and executes, while the other conflicting transactions remain in a locked waiting state. This avoids invalid data queries and data modifications after conflicting transactions are woken up simultaneously, thereby improving the system's concurrent execution efficiency.
[0083] Example 3
[0084] In one embodiment, to facilitate a better understanding of the database modification method, Figure 3This is a flowchart of a database modification method provided in Embodiment 3 of the present invention. In this flowchart, TrxB represents transaction B, TrxC represents transaction C, TrxD represents transaction D, n_row represents the number of conflicts, lock_reg represents the conflict transaction registration table, LockB2 represents a lock object in the TrxB waiting state, LockC2 represents a lock object in the TrxC waiting state, LockD2 represents a lock object in the TrxD waiting state, S-lock is a shared lock, LockD3 represents a new transaction lock object, and relock_tid represents the transaction identifier number for relocking. Taking the TrxB transaction commit scenario as an example, the specific execution steps are explained as follows:
[0085] S310. Initialize the conflict transaction registry.
[0086] In this embodiment, the conflict transaction registration table lock_reg is first initialized, the n_row registered in lock_reg is set to 0, and the arrays of table identifiers modified by conflict transactions, row identifiers modified by conflict transactions, and locking transaction identifiers of conflict transactions in the conflict transaction registration table are all set to empty.
[0087] S320. If the blocking information has not been registered, determine the execution policy as the processing policy and modify the data according to the processing policy.
[0088] In this embodiment, before releasing LockB2[TidA,S,T,Rowid1] and granting permissions to LockC2[TidA,S,T,Rowid1], the lock_reg is checked. It is found that there is no lock information for the [T,Rowid1] record registered in the lock_reg. Therefore, the lock information of LockB2 is registered in the lock_reg, and TrxC is woken up. After TrxC is woken up, the query is re-executed to obtain the updated record content, and then the update operation is re-executed.
[0089] It should be noted that the specific steps for registering LockB2 locking information in lock_reg are as follows:
[0090] a1. Set the tabids[n_row] registered in lock_reg to T; where T represents the table identifier.
[0091] a2. Set the rowids[n_row] registered in lock_reg to Rowid1; Rowid1 represents the row identifier.
[0092] a3. Set tids[n_row] registered in lock_reg to TidA; TidA represents the lock transaction identifier.
[0093] a4. Set the n_row registered in lock_reg to n_row+1.
[0094] S330. If the blocking information has been registered, determine the execution policy as the waiting policy and process it according to the waiting policy.
[0095] In this embodiment, before releasing LockB[TidB, X] and granting permissions to LockD2[TidB, S, T, Rowid1], the lock_reg is checked. It is found that the locking information of [T, Rowid1] has been registered. Therefore, the relock_tid in the LockD2 object is set to the locking transaction identifier number of the conflicting transaction registered in lock_reg, i.e., TidA, and TrxD is woken up.
[0096] It should be noted that after the TrxD transaction is awakened, it re-initiates an S lock request for relock_tid based on the relock_tid information registered on LockD2. The new transaction lock object is LockD3[TidA, S, T, Rowid1]. Since LockD3 conflicts with LockC2[TidA, S, T, Rowid1], the TrxD transaction continues to wait and is awakened when the TrxC transaction is committed or rolled back.
[0097] In this embodiment of the invention, when a transaction is committed or rolled back, the lock object is released, and waiting transactions are woken up, the conflict transaction registry is checked to determine whether the waiting transactions should continue execution or wait again after being woken up. If the relevant information of the waiting transaction's pending modification record is not registered in the conflict transaction registry, the relevant information is registered in the conflict transaction registry and the waiting transaction is woken up, and it continues execution after being woken up. If the relevant information of the waiting transaction's pending modification record is already registered in the conflict transaction registry, after the waiting transaction is woken up, the locked transaction identifier of the conflicting transactions registered in the conflict table is re-locked in S mode or X mode, and it re-enters the waiting state. Ultimately, although a transaction commit or rollback wakes up multiple conflicting transactions, it ensures that only one transaction actually starts and executes, while other conflicting transactions remain in a locked waiting state.
[0098] The transaction lock object records the following: tid: the locking transaction identifier, mode: the locking mode, tabid: the table identifier being modified, rowid: the row identifier of the modified record, and relock_tid: the transaction identifier that needs to be relocked. The conflict transaction registration table records the following: n_row: the number of conflicts, tabids[]: an array of table identifiers modified by the conflicting transactions, rowids[]: an array of row identifiers modified by the conflicting transactions, and tids[]: an array of locking transaction identifiers for the conflicting transactions.
[0099] It should be noted that the conflict transaction registration table can also be a hash table to reduce the array scanning cost; this embodiment does not impose any restrictions on this.
[0100] In this embodiment of the invention, when a locked object is released by committing or rolling back a transaction and a waiting transaction is woken up, information such as the Table ID, Row ID, and locked transaction ID of the transaction lock is registered. Based on this registered transaction lock information, it is checked whether the wake-up of the transaction will cause new update conflicts. This ensures that only one waiting transaction is woken up when the transaction lock of the same row record is released, avoiding invalid data queries and data modifications after conflicting transactions are woken up at the same time, thereby improving the system's concurrent execution efficiency.
[0101] Example 4
[0102] In one embodiment, Figure 4 This is a structural block diagram of a database modification device provided in Embodiment 4 of the present invention. This device is suitable for modifying data in a database and can be implemented in hardware or software. It can be configured in an electronic device to implement a database modification method according to an embodiment of the present invention. Figure 4 As shown, the device includes: a registration determination module 410, a strategy determination module 420, and a data modification module 430.
[0103] The registration and determination module is used to determine the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction.
[0104] The strategy determination module is used to determine the execution strategy of the waiting transaction based on the registration information;
[0105] The data modification module is used to control the modification of data by the waiting transactions according to the execution strategy.
[0106] In this embodiment of the invention, the registration determination module determines the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction. The strategy determination module determines the execution strategy of the waiting transaction based on the registration status, which avoids invalid data queries and data modifications after the conflicting transactions are woken up at the same time, thereby improving the efficiency of concurrent execution of the system.
[0107] In one embodiment, the registration determination module 410 includes:
[0108] The information lookup unit is used to traverse and search the conflict transaction registration table to find whether there is any blocking information waiting for the transaction target record; if so, it is determined that the registration status is that the blocking information has been registered; if not, it is determined that the registration status is that the blocking information has not been registered.
[0109] In one embodiment, the strategy determination module 420 includes:
[0110] The first strategy determination module is used to determine the execution strategy as a waiting strategy when the blocking information has been registered. The waiting strategy includes setting a re-blocking transaction identifier in the transaction lock object, and the waiting transaction re-blocking according to the re-blocking transaction identifier.
[0111] The second strategy determination module is used to determine the execution strategy as a processing strategy when the blocking information has not been registered. The processing strategy includes: registering the blocking information in the conflict transaction registration table, querying the updated record content of the waiting transaction, and re-executing the data update operation.
[0112] In one embodiment, the first strategy determination module includes:
[0113] The new transaction lock object determination module is used to obtain the relocking request of the waiting transaction and create a new transaction lock object for the waiting transaction according to the relocking request;
[0114] A transaction relocking unit is used for the waiting transaction to re-execute the lock according to the new transaction lock object.
[0115] In one embodiment, registering the blocking information in the conflict transaction registry includes:
[0116] Add the table identifier of the blocking information to the modified table identifier array in the conflict transaction registration table;
[0117] Add the row identifier of the blocking information to the row identifier array of the conflict transaction modification registered in the conflict transaction registration table;
[0118] Add the blocking transaction identifier of the blocking information to the array of blocking transaction identifiers of the conflicting transactions registered in the conflicting transaction registration table;
[0119] Increment the number of conflicts in the conflict transaction registration table by 1.
[0120] In one embodiment, the blocking information includes at least one of the following: the blocking transaction identifier, the blocking mode, the modified table identifier, the modified record row identifier, and the re-blocking transaction identifier.
[0121] In one embodiment, the database modification device further includes:
[0122] An initialization module is used to initialize a conflict transaction registration table. The initialization of the conflict transaction registration table includes: setting the number of conflicts in the conflict transaction registration table to 0, and setting the arrays of table identifiers modified by conflict transactions, row identifiers modified by conflict transactions, and locking transaction identifiers of conflict transactions in the conflict transaction registration table to be empty.
[0123] The database modification apparatus provided in this embodiment of the invention can execute the database modification method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the method execution.
[0124] In one embodiment, Figure 5 A schematic diagram of an electronic device that can be used to implement embodiments of the present invention is shown. The electronic device 10 is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices (e.g., helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.
[0125] like Figure 5 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 or a random access memory (RAM) 13, communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer program stored in the ROM 12 or loaded from storage unit 18 into the RAM 13. The RAM 13 may also store various programs and data required for the operation of the electronic device 10. The processor 11, ROM 12, and RAM 13 are interconnected via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0126] Multiple components in electronic device 10 are connected to I / O interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of displays, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows electronic device 10 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0127] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as database modification methods.
[0128] In some embodiments, the database modification method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on electronic device 10 via ROM 12 and / or communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the database modification method described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to perform the database modification method by any other suitable means (e.g., by means of firmware).
[0129] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0130] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.
[0131] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0132] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0133] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or computing systems that include middleware components (e.g., application servers), or computing systems that include frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.
[0134] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.
[0135] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.
[0136] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A database modification method, characterized in that, include: When releasing the transaction lock object of the target transaction and waking up the waiting transactions, determine the registration status of the locking information of the target record of the waiting transaction in the conflict transaction registry; The execution strategy for the waiting transaction is determined based on the registration information; Control the data to be modified by the waiting transaction according to the execution strategy; The blocking information includes at least one of the following: the blocking transaction identifier, the blocking mode, the modified table identifier, the modified record row identifier, and the re-blocking transaction identifier; The step of determining the execution strategy for the waiting transaction based on the registration information includes: If the blocking information has been registered, the execution strategy is determined to be a waiting strategy, wherein the waiting strategy includes: setting a re-blocking transaction identifier in the transaction lock object, and the waiting transaction re-blocking according to the re-blocking transaction identifier; If the blocking information has not been registered, the execution strategy is determined to be a processing strategy, wherein the processing strategy includes: registering the blocking information in the conflict transaction registration table, the waiting transaction querying the updated record content, and re-executing the data update operation.
2. The method according to claim 1, characterized in that, The registration status of blocking information for pending transaction target records in the conflict transaction registration form includes: Iterate through the conflict transaction register to check if there is any blocking information waiting for the transaction target record; If so, then it is determined that the registration status indicates the blocking information has been registered; If not, then the registration status indicates that the blocking information has not been registered.
3. The method according to claim 1, characterized in that, The waiting transaction is re-blocked according to the re-blocking transaction identifier, including: Obtain the relock request of the waiting transaction, and create a new transaction lock object for the waiting transaction according to the relock request; The waiting transaction re-executes the lock according to the new transaction lock object.
4. The method according to claim 1, characterized in that, Registering the blocking information in the conflict transaction registry includes: Add the table identifier of the blocking information to the modified table identifier array in the conflict transaction registration table; Add the row identifier of the blocking information to the modified row identifier array in the conflict transaction registry table; Add the blocking transaction identifier of the blocking information to the blocking transaction identifier array in the conflict transaction registration table; Increment the number of conflicts in the conflict transaction registration table by 1.
5. The method according to claim 1, characterized in that, Also includes: Initialize the conflict transaction registration table, wherein the initialization of the conflict transaction registration table includes: setting the number of conflicts in the conflict transaction registration table to 0, and setting the modified table identifier array, modified row identifier array and blocking transaction identifier array in the conflict transaction registration table to be empty.
6. A database modification device, characterized in that, include: The registration and determination module is used to determine the registration status of the locking information of the target transaction record in the conflict transaction registration table when releasing the transaction lock object of the target transaction and waking up the waiting transaction. The strategy determination module is used to determine the execution strategy of the waiting transaction based on the registration information; The data modification module is used to control the waiting transactions to modify data according to the execution strategy. The blocking information includes at least one of the following: the blocking transaction identifier, the blocking mode, the modified table identifier, the modified record row identifier, and the re-blocking transaction identifier; The strategy determination module includes: The first strategy determination module is used to determine the execution strategy as a waiting strategy when the blocking information has been registered. The waiting strategy includes setting a re-blocking transaction identifier in the transaction lock object, and the waiting transaction re-blocking according to the re-blocking transaction identifier. The second strategy determination module is used to determine the execution strategy as a processing strategy when the blocking information has not been registered. The processing strategy includes: registering the blocking information in the conflict transaction registration table, querying the updated record content of the waiting transaction, and re-executing the data update operation.
7. An electronic device, characterized in that, The electronic device includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the database modification method according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that cause a processor to execute the database modification method according to any one of claims 1-5.
Citation Information
Patent Citations
Concurrency control method and device
CN101615203A