Transaction concurrency control method and device, equipment and medium

Through the field-level concurrency control method, hash values ​​are used to determine concurrent transaction conflicts, which solves the performance problems of optimistic locking and pessimistic locking in concurrent scenarios in distributed database systems, and improves parallel processing capabilities and system performance.

CN118606009APending Publication Date: 2024-09-06CHINA CONSTRUCTION BANK +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410710429.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-03
Publication Date
2024-09-06

AI Technical Summary

Technical Problem

In distributed database systems, optimistic locks lead to long spin times and high overhead in scenarios with a large number of write operations, while pessimistic locks lead to low parallel processing capabilities when there are many read operations, and cannot effectively solve the parallel processing capabilities and waiting problems of concurrent transactions.

Method used

By using field-level concurrency control methods in transaction processing, we obtain field value information associated with business requests, use hash values ​​to determine whether concurrent transactions conflict, and only write and rollback when necessary, reducing lock granularity and improving parallel processing. ability.

Benefits of technology

It reduces spin time and overhead, improves the parallel processing capability of concurrent transactions, especially when there are many write operations, reduces data conflicts and transaction rollback times, and improves the overall performance of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118606009A
    Figure CN118606009A
Patent Text Reader

Abstract

The invention relates to the technical field of data processing, in particular to a transaction concurrency control method and device, equipment and a medium, and is used for solving the problems of long spinning time and relatively low parallel processing capability when concurrent transactions are controlled in related technologies. In the method, a first transaction is started based on a received service request, value information of a first field needing to be queried by the service request and value representation information of a second field having an association relationship with the service request are acquired from a database, and service processing is performed based on the value information of the first field to obtain a service processing result, and judging whether a transaction conflicting with the first transaction exists in a plurality of currently concurrent second transactions or not based on the cached value representation information of the second field and the value representation information of the second field currently acquired from the database, and processing the to-be-processed business result based on a judgment result to end the first transaction. The granularity of the field-level lock is finer, the spinning time can be shortened, and the parallel processing capacity of transactions is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a transaction concurrency control method, device, equipment and medium. Background Art

[0002] In distributed scenarios, database-based system design often leads to data contention in concurrent scenarios. There are two common solutions to this situation: optimistic locking and pessimistic locking.

[0003] When processing data, optimistic locking considers data competition to be a low-probability event, so it will not lock the database, but use version numbers or timestamps to determine whether there is data competition. Taking the version number as an example, a version number field is added to each row of data (a row of data is a record), and the version number of the row data corresponding to the data of this operation is recorded when operating the data. The current version number and the version number of the record are compared during the next modification. If they are inconsistent, it is considered that other transactions have changed the relevant records, and the modification of the current transaction will not be successful and needs to be rolled back. The situation of adding timestamps is similar and will not be repeated here. When reading data, pessimistic locking generally believes that the data will be modified by other transactions, so the row data corresponding to the data is locked through the database's own locking mechanism to seize resources, thereby ensuring the exclusivity of data operations.

[0004] However, when there are a lot of write operations, optimistic locking will have problems such as long spin time (that is, if the write fails, it will continue to execute in a loop until it succeeds) and high overhead. The biggest problem with pessimistic locking is that when there are many read operations, there will be a long wait time and the parallel processing capability is relatively low. Summary of the invention

[0005] The embodiments of the present application provide a transaction concurrency control method, apparatus, device and medium, which are used to solve the problems of long spin time and relatively low parallel processing capability in controlling concurrent transactions in the related art.

[0006] In a first aspect, the present application provides a transaction concurrency control method, comprising:

[0007] Based on the received business request, start a first transaction;

[0008] Acquire from a database value information of at least one first field to be queried by the business request, and value representation information of at least one second field associated with the business request;

[0009] Performing business processing based on the value information of the at least one first field to obtain a business processing result;

[0010] Based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database, determining whether there is a transaction in conflict with the first transaction among the multiple concurrent second transactions;

[0011] Based on the determination result, the business processing result is processed to end the first transaction.

[0012] In some possible embodiments, judging whether there is a transaction that conflicts with the first transaction among the multiple concurrent second transactions based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database includes:

[0013] Determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same;

[0014] Based on whether there is a second field whose value changes, it is determined whether there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0015] In some possible embodiments, the value representation information of the at least one second field is a hash value, and determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same includes:

[0016] If the cached hash value of the at least one second field is the same as the currently acquired hash value of the at least one second field, it is determined that there is no second field whose value has changed;

[0017] If the cached hash value of the at least one second field is different from the currently acquired hash value of the at least one second field, it is determined that there is a second field whose value has changed.

[0018] In some possible embodiments, determining whether there is a transaction in the plurality of second transactions that conflicts with the first transaction based on whether there is a second field whose value changes includes:

[0019] If there is no second field whose value has changed, determining that there is no transaction in the plurality of second transactions that conflicts with the first transaction;

[0020] If there is a second field whose value changes, it is determined that there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0021] In some possible embodiments, based on the determination result, processing the business processing result includes:

[0022] If it is determined that there is no transaction that conflicts with the first transaction, writing the business processing result to the database, and committing the first transaction after successful writing;

[0023] If it is determined that there is a transaction that conflicts with the first transaction, the business processing result is discarded and the first transaction is rolled back.

[0024] In some possible embodiments, rolling back the first transaction includes:

[0025] The step of obtaining the value information of the at least one first field from the database, replacing the cached value representation information of the at least one second field with the latest acquired value representation information, performing business processing based on the value information of the at least one first field, and obtaining a business processing result.

[0026] In a second aspect, the present application provides a transaction concurrency control device, comprising:

[0027] An opening module, configured to open a first transaction based on a received service request;

[0028] An acquisition module, used to acquire from a database value information of at least one first field to be queried by the business request, and value representation information of at least one second field associated with the business request;

[0029] A first processing module, used to perform business processing based on the value information of the at least one first field to obtain a business processing result;

[0030] a judgment module, configured to judge whether there is a transaction that conflicts with the first transaction among the multiple concurrent second transactions based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database;

[0031] The second processing module is used to process the business processing result based on the judgment result to end the first transaction.

[0032] In some embodiments, the determination module is specifically used to:

[0033] Determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same;

[0034] Based on whether there is a second field whose value changes, it is determined whether there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0035] In some embodiments, the value representation information of the at least one second field is a hash value, and the judgment module is specifically used to:

[0036] If the cached hash value of the at least one second field is the same as the currently acquired hash value of the at least one second field, it is determined that there is no second field whose value has changed;

[0037] If the cached hash value of the at least one second field is different from the currently acquired hash value of the at least one second field, it is determined that there is a second field whose value has changed.

[0038] In some embodiments, the determination module is specifically used to:

[0039] If there is no second field whose value has changed, determining that there is no transaction in the plurality of second transactions that conflicts with the first transaction;

[0040] If there is a second field whose value changes, it is determined that there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0041] In some embodiments, the second processing module is specifically configured to:

[0042] If it is determined that there is no transaction that conflicts with the first transaction, writing the business processing result to the database, and committing the first transaction after successful writing;

[0043] If it is determined that there is a transaction that conflicts with the first transaction, the business processing result is discarded and the first transaction is rolled back.

[0044] In some embodiments, the second processing module is specifically configured to:

[0045] The step of obtaining the value information of the at least one first field from the database, replacing the cached value representation information of the at least one second field with the latest acquired value representation information, performing business processing based on the value information of the at least one first field, and obtaining a business processing result.

[0046] In a third aspect, the present application provides an electronic device, including:

[0047] A memory for storing program instructions;

[0048] The processor is used to call the program instructions stored in the memory, and execute the steps included in any one of the methods in the first aspect according to the obtained program instructions.

[0049] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes any one of the methods described in the first aspect.

[0050] In a fifth aspect, the present application provides a computer program product, comprising: a computer program code, when the computer program code is run on a computer, the computer executes any one of the methods described in the first aspect.

[0051] In the embodiment of the present application, after the first transaction is opened based on the received business request, the value information of at least one first field to be queried by the business request and the value representation information of at least one second field associated with the business request are obtained from the database, and the business processing is performed based on the value representation information of the at least one first field to obtain the business processing result, and the value representation information of the at least one second field currently obtained from the database is used to determine whether there is a transaction that conflicts with the first transaction in the current multiple concurrent second transactions, and the pending business processing result is processed based on the judgment result to end the first transaction. In this way, each time data is written, only the change of the second field associated with the business request needs to be paid attention to, without paying attention to the change of the row data corresponding to the business request, which is equivalent to locking the second field, and the granularity of this field-level lock is finer. When encountering a large number of write operation scenarios, if the second fields corresponding to the concurrent transactions are completely different, these concurrent transactions can write to different fields in the same row of data in parallel, so that data conflicts can be reduced, the number of transaction rollbacks can be reduced, and the parallel processing capability of concurrent transactions can be improved, so that the spin time can be reduced and the overhead can be reduced. When encountering a large number of read operation scenarios, since the parallel read operations of concurrent transactions will not change the value of the second field, there is no data conflict. Concurrent transactions can be carried out in parallel and are not prone to long waiting times, so the parallel processing capability is relatively high. BRIEF DESCRIPTION OF THE DRAWINGS

[0052] Figure 1 A schematic diagram of transaction concurrency control of an optimistic lock provided in an embodiment of the present application;

[0053] Figure 2 A schematic diagram of transaction concurrency control of a pessimistic lock provided in an embodiment of the present application;

[0054] Figure 3 A possible application scenario diagram provided for an embodiment of the present application;

[0055] Figure 4A flowchart of a transaction concurrency control method provided in an embodiment of the present application;

[0056] Figure 5 A schematic diagram of a transaction concurrency control process of a field lock provided in an embodiment of the present application;

[0057] Figure 6 A schematic diagram of transaction concurrency control of a field lock provided in an embodiment of the present application;

[0058] Figure 7 A schematic diagram of transaction concurrency control of another field lock provided in an embodiment of the present application;

[0059] Figure 8 A structural diagram of a transaction concurrency control device provided in an embodiment of the present application;

[0060] Fig. 9 A structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0061] In order to make the purpose, technical scheme and advantages of the present application clearer, the technical scheme in the embodiment of the present application will be clearly and completely described below in conjunction with the drawings in the embodiment of the present application. Obviously, the described embodiment is only a part of the embodiment of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be arbitrarily combined with each other. In addition, although the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in an order different from that here.

[0062] The terms "first" and "second" in the specification and claims of the present application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the term "comprising" and any of their variations are intended to cover non-exclusive protection. For example, a process, method, system, product or device comprising a series of steps or units is not limited to the listed steps or units, but optionally also includes steps or units that are not listed, or optionally also includes other steps or units inherent to these processes, methods, products or devices. "Multiple" in the present application can mean at least two, for example, two, three or more, and the embodiments of the present application are not limited.

[0063] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description. It should be noted that in the embodiments of the present application, certain software, components, models and other existing solutions in the industry may be mentioned, which should be considered as exemplary, and their purpose is only to illustrate the feasibility of the implementation of the technical solution of the present application, but it does not mean that the applicant has or will necessarily use the solution.

[0064] In the technical solution of this application, the acquisition, transmission, storage, and use of data are in compliance with the requirements of relevant national laws and regulations.

[0065] Before introducing the transaction concurrency control method provided by the embodiment of the present application, in order to facilitate understanding, the technical background of the embodiment of the present application is first introduced in detail below.

[0066] In distributed scenarios, database-based system design often leads to data contention in concurrent scenarios. There are two common solutions to this situation: optimistic locking and pessimistic locking.

[0067] When processing data, optimistic locking generally considers data contention to be a low-probability event, so it does not lock the database, but uses version numbers or timestamps to determine whether there is data contention. Taking timestamps as an example, a timestamp field is added to each row of data (a row of data is a record), see Figure 1 When optimistic locking is used, after the transaction is opened, the timestamp of the row data corresponding to the operation data is recorded (the row data corresponding to the operation data may be one row data, two rows data, or more rows data, depending on the business needs of this operation, and one row data is a record). Since optimistic locking belongs to row-level locks, that is, locking in units of rows, when there are more than one row data corresponding to the operation data, the timestamps of multiple row data need to be recorded. Then, the transaction is processed to obtain the operation result. When writing the operation result, the timestamp of each saved row data is compared with the current timestamp. If they are inconsistent, it is considered that other transactions have changed this row data, and the modification of the current transaction will not succeed and need to be rolled back. If they are consistent, it is considered that no other transaction has changed this row data, and the operation result can be written. After success, the transaction is committed. However, when encountering a large number of write operation scenarios, optimistic locking will have problems such as long spin time (that is, if the write fails, it will be executed in a loop until it succeeds) and high overhead. The situation of adding version numbers is similar and will not be repeated here.

[0068] Pessimistic locking is when reading data. It is generally believed that the data will be modified by other transactions, so the row data corresponding to the data is locked through the database's own locking mechanism (the row data corresponding to the data may be one record, two records, or more records, depending on the business needs of this operation) to seize resources and ensure the exclusivity of this data operation. Figure 2 When using pessimistic locking, after opening a transaction, the database data is read in, and the database's own locking mechanism is used to lock the read data, and the row data corresponding to the data is independently occupied. Then, the operation is processed, the operation result is obtained, and the operation result is written to the database. Then, the transaction is committed. During the whole process, other transactions are not allowed to operate on the row data of the row lock, including read operations. The biggest problem with pessimistic locking is that when there are many read operations, there will be a long wait time, and the parallel processing capability is relatively low.

[0069] In some distributed scenarios, the number of read and write operations is unpredictable, and the number of read and write operations in different time periods of the day is uneven. In order to solve the above problems, an embodiment of the present application provides a field-level concurrent lock that can support scenarios with many read operations and scenarios with many write operations as much as possible.

[0070] The following first introduces the scenario of the embodiment of the present application.

[0071] See also Figure 3 , Figure 3 An application scenario diagram provided for an embodiment of the present application includes multiple user terminals 10, multiple servers 20 and a database 30, wherein the multiple user terminals 10 can be mobile phones, IPads, desktop computers, laptops, wearable devices, ATMs, card opening machines, teller service terminals, etc., and are used to submit business requests to multiple servers 20. Multiple servers 20 can form a distributed system, and based on the received business requests, multiple concurrent transactions are started, data is interacted with the database 30, business processing is performed, and these transactions are processed in parallel. The database 30 is used to store business data and provide data support services for the server to process business requests.

[0072] It should be noted that Figure 3 This is only an example of an application scenario. In the embodiments of the present application, concurrent transactions can also be triggered by multiple threads.

[0073] The following describes the transaction concurrency control method provided by the exemplary embodiment of the present application in combination with the above application scenarios and with reference to the accompanying drawings. It should be noted that the above application scenarios are only shown to facilitate understanding of the spirit and principles of the present application, and the implementation of the present application is not limited in this regard.

[0074] Please refer to Figure 4, is a flowchart of a transaction concurrency control method provided in an embodiment of the present application, the method is applied to Figure 3 In any server 20, and includes the following steps.

[0075] In step 401, a first transaction is started based on a received service request.

[0076] In step 402, value information of at least one first field to be queried by the business request and value representation information of at least one second field associated with the business request are obtained from a database.

[0077] In practical applications, the at least one second field associated with the service request may include a field associated with the service request and / or a field that may be associated with and affect the service request, depending on the specific service. In addition, the first field and the second field may partially overlap.

[0078] Assume that the database stores a student score record table and a student basic information table. The fields of the student score record table include student ID, Chinese score, math score, average score, and whether the student has made progress. The fields of the student basic information table include student ID, gender, student name, and average score ranking. For example, if the business request is to update the Chinese score of student A, the first field to be queried may include student A's student ID, student name, and Chinese score. The second field associated with the business request may include student A's student ID, Chinese score, and average score ranking, where the student ID and student name overlap.

[0079] Furthermore, the value characterization information of the at least one second field includes, for example, a hash value of the value information of the at least one second field, a value range to which the value belongs, indication information of the value range to which the value belongs, etc., and the value characterization information can be obtained by processing the value information of the at least one second field by the database.

[0080] Taking the value representation information as a hash value as an example, in actual applications, no matter how many second fields there are, all second fields can be hashed together to obtain a hash value. When there are at least two second fields and these at least two second fields are distributed in different business tables, the business table can also be used as a unit to perform hash calculations on the second fields in each business table, that is, each business table containing the second field corresponds to a hash value.

[0081] In addition, it should be noted that optimistic locking is to add a version number field or a timestamp field to each row of data, and then use the changes in the version number field or the timestamp field to lock the row of data associated with the business request. In the embodiment of the present application, there is no need to add additional fields to each row of data. The value of the field associated with the business request is used to represent the change of information, and the field lock is applied to the field associated with the business request, and other fields in the same row as these fields can still be read or written normally.

[0082] In step 403, business processing is performed based on the value information of the at least one first field to obtain a business processing result.

[0083] For example, if the Chinese score of student A in this test is 80, then the Chinese score of student A found can be modified to 80.

[0084] In step 404, based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database, it is determined whether there is a transaction in the current concurrent multiple second transactions that conflicts with the first transaction.

[0085] The transaction that conflicts with the first transaction refers to a transaction that has a data access conflict with the first transaction.

[0086] In specific implementation, the following steps may be used to determine whether there is a transaction in conflict with the first transaction among the multiple concurrent second transactions.

[0087] The first step is to determine whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field is the same as the value representation information of the at least one second field currently obtained.

[0088] Taking the example that the value representation information of the at least one second field is a hash value, if the hash value of the cached at least one second field is the same as the hash value of the at least one second field currently obtained, it can be determined that there is no second field whose value has changed; if the hash value of the cached at least one second field is different from the hash value of the at least one second field currently obtained, it can be determined that there is a second field whose value has changed.

[0089] The second step is to determine whether there is a transaction that conflicts with the first transaction among the multiple concurrent second transactions based on whether there is a second field whose value has changed.

[0090] For example, if there is no second field whose value has changed, it is determined that there is no transaction in the current multiple concurrent second transactions that conflicts with the first transaction; if there is a second field whose value has changed, it is determined that there is a transaction in the current multiple concurrent second transactions that conflicts with the first transaction.

[0091] In step 405, based on the determination result, the business processing result is processed to end the first transaction.

[0092] For example, if it is determined that there is no transaction that conflicts with the first transaction, the business processing result can be written to the database, and after successful writing, the first transaction can be committed to end the first transaction; if it is determined that there is a transaction that conflicts with the first transaction, the business processing result can be discarded and the first transaction can be rolled back.

[0093] In the first case, rolling back the first transaction can directly return to step 402 and execute subsequent steps.

[0094] In the second case, rolling back the first transaction can obtain the value information of the at least one first field from the database, and replace the cached value representation information of the at least one second field with the latest obtained value representation information, and then return to step 403 and execute subsequent steps. In this way, the value representation information can be obtained from the database less once, thereby reducing the data interaction pressure with the database.

[0095] In the embodiment of the present application, each time data is written, it is only necessary to pay attention to the changes in the second field associated with the business request, without paying attention to the changes in the row data corresponding to the business request, which is equivalent to locking the second field, and the granularity of this field-level lock is finer. When encountering a large number of write operation scenarios, if the second fields corresponding to the concurrent transactions are completely different, these concurrent transactions can write to different fields in the same row of data in parallel, so that data conflicts can be reduced, the number of transaction rollbacks can be reduced, and the parallel processing capabilities of concurrent transactions can be improved. Therefore, the spin time can be reduced and the overhead can be reduced. When encountering a large number of read operation scenarios, since the parallel read operations of concurrent transactions will not change the value of the second field, there is no data conflict, and concurrent transactions can be carried out in parallel and are not prone to long-term waiting, so the parallel processing capability is relatively high.

[0096] Unlike pessimistic locks and optimistic locks, which are row-level concurrent locks (locked in units of row data), the concurrent locks in the embodiments of the present application are field-level (locked in units of fields). For this type of field-level concurrent lock, before opening a transaction, the second field (i.e., column name) of business concern can be selected in advance according to the specific business processing content as the key attribute of this operation (which can be an attribute of multiple tables). Among them, the second field generally selects the field (or column) that this operation will affect the operation result and the field that cannot be overwritten and is ready to be written. The field that cannot be overwritten and is ready to be written refers to the period during which the read and commit modification is performed. If the submission of other transactions affects this operation, the operation will be invalid.

[0097] See also Figure 5 , Figure 5A control process diagram of a field-level concurrent lock (referred to as field lock) provided in an embodiment of the present application, after opening a new transaction, query the corresponding data record to obtain the value information of the first field, and directly generate a Hash value (i.e., the hash value of the second field) as the Key value through the HASH function of the database (such as the Md5 algorithm) according to the column corresponding to the Key attribute, and retain it in the memory. Subsequently, after processing the business data, when submitting the data, verify whether the Hash value calculated by the column corresponding to the current Key attribute and the saved Hash value have changed. If not, the data can be successfully submitted, that is, the record is successfully updated, and then the transaction is submitted. If there is a change, it means that there is a transaction in the current multiple concurrent transactions that has a data conflict with the new transaction, and it is necessary to wait for the conflicting transaction to be submitted before the new transaction can be reprocessed, that is, if the data submission fails, return to re-query the data record, perform the next calculation and submit until the execution is successful.

[0098] The scheme in the embodiment of the present application is described in detail below in conjunction with specific embodiments.

[0099] Suppose there are two tables: a student performance record table and a student information basic table, where:

[0100] The table definition (i.e., the fields included) of the student achievement record table are:

[0101] The value of the student ID primary key (PK) is char type.

[0102] The Chinese language score value is an integer int type.

[0103] The math score value is int type.

[0104] The average score is of int type.

[0105] Whether to progress is of int type.

[0106] The table definition (i.e. the fields included) of the basic student information table are:

[0107] The value of student ID PK is char type.

[0108] Gender value is int type.

[0109] The student name value is a variable-length character varchar type.

[0110] The student ranking value is int type.

[0111] Suppose that the Chinese and math scores of student A in the student score record table (located in the same row) are updated in parallel. Figure 6 , Figure 6A concurrency control diagram of a transaction provided in an embodiment of the present application, transaction 1 and transaction 2 are concurrent, transaction 1 is used to update the Chinese score of student A, and the corresponding focus column (i.e., the second field) is the student number, Chinese score, and student ranking, and transaction 2 is used to update the math score of student A, and the corresponding focus column (i.e., the second field) is the student number, math score, and student ranking. The execution process is as follows:

[0112] Execute transaction 1:

[0113] 1. Query the record of student A in the student score record table and student basic information table;

[0114] 2. Generate a hash value for student A's student ID, Chinese score in the student score table, and student A's ranking (last score ranking) in the student basic information table and store it in memory;

[0115] 3. Fill in your Chinese language score.

[0116] Switch execution transaction 2:

[0117] 1. Query the record of student A in the student score record table and student basic information table;

[0118] 2. Generate a hash value for student A's student ID, math score in the student score table, and student A's ranking (last score ranking) in the student basic information table and store it in memory;

[0119] 3. Fill in this math score.

[0120] 4. Update the math score and average score records corresponding to Student A, and verify the Hash value of the corresponding column; because the Hash verification passes, the math score and the corresponding average score are updated successfully.

[0121] 5. Submission successful.

[0122] Switch execution transaction 1:

[0123] 4. Update the Chinese score and average score records corresponding to Student A, and verify the Hash value of the corresponding column; because the Hash verification passes, the Chinese score and the corresponding average score are updated successfully.

[0124] 5. Submission successful.

[0125] That is to say, by using the above-mentioned field-level concurrent lock, the Chinese and math scores of student A in the same row in the student score record table can be updated in parallel. In this way, concurrent transactions 1 and 2 can be executed in parallel, so the parallel processing capability is relatively high.

[0126] If the above transaction 1 and transaction 2 are implemented with pessimistic locks, transaction 1 will lock the row of student A's Chinese score in the student score record table and the row of student A's student ranking in the student basic information table at the same time (locked using the database's lock mechanism, i.e., truly locked), and then fill in student A's current Chinese score, submit the new student A's Chinese score, and update student A's student ranking simultaneously. During this period, since the row of student A's Chinese score in the student score record table and the row of student A's student ranking in the student basic information table are locked, transaction 2 cannot update student A's math score and student ranking. It must wait until transaction 1 ends and the lock is released before it can update student A's math score and student ranking. In this way, it is equivalent to determining that concurrent transactions 1 and 2 are in conflict, thereby controlling the serial execution of concurrent transactions 1 and 2, and therefore, the parallel processing capability is relatively low.

[0127] In order to better illustrate the difference between field-level concurrent lock and optimistic lock in the embodiment of the present application, it is assumed that Figure 6 The math scores and Chinese scores in the char type are all valued, and the value-taking rules are: 80 points or above are excellent, 60 points to 80 points are qualified, and below 60 points are unqualified. The average score is also of char type, and the value-taking rules are: if both Chinese and data are excellent, it is excellent; if one subject is excellent and the other is qualified, it is qualified; if any subject is unqualified, it is unqualified.

[0128] Further assume that in the student score record table, student A's current Chinese score is qualified, math score is qualified, and average score is qualified. And assume that student A's Chinese score this time is 78 points, and math score is 82 points. Similarly, the Chinese score and math score of student A in the same row in the student score record table are updated in parallel.

[0129] If the field-level concurrent lock in the embodiment of the present application is adopted, see Figure 6 After executing step 3, transaction 1 switches to execute transaction 2. The hash values ​​of student A's student ID, math score and average score that transaction 2 focuses on are the same after transaction 2 starts and before the math score (value is excellent) is submitted. Therefore, transaction 2 will be submitted successfully, that is, student A's math score will become excellent, the average score will still be qualified. Then, when transaction 1 executes to step 4, it is judged that the hash values ​​after transaction 1 starts and before the Chinese score (value is qualified) is submitted are the same (although the value of the math score has changed, the math score is not the field that the current business is concerned about, and the hash values ​​of the fields student ID, Chinese score and average score that the current business is concerned about have not changed), so transaction 1 will also be submitted successfully, that is, student A's Chinese score will become qualified, the average score will still be qualified.

[0130] In the above situation, if transaction 1 and transaction 2 are implemented using optimistic locking, the version number of the row where student A's math score is located will change after transaction 2 is successfully submitted. Subsequently, when transaction 1 is executed to step 4, it is determined that the version number of the row where student A's Chinese score is located (the same as the row where the math score is located) has changed after the start of transaction 1 and before the submission of this Chinese score (the value is qualified). Then, it will be considered that there is a transaction that conflicts with transaction 1. After that, it will give up modifying this Chinese score, and then roll back transaction 1 and re-execute transaction 1, thereby turning concurrent transactions 1 and 2 into serial ones.

[0131] The following is an example of a transaction in which there is a conflict in the embodiment of the present application.

[0132] In a distributed scenario, transaction 1 and transaction 2 are concurrent. Assume that transaction 1 is used to update student A's Chinese score and average score, and transaction 2 is used to update the student ranking. The second fields that transaction 1 focuses on are: student ID, Chinese score, and student ranking, and the second fields that transaction 2 focuses on are: student ID, Chinese score, and math score.

[0133] See also Figure 7 , Figure 7 This is another concurrency control diagram of a transaction provided in an embodiment of the present application. The execution process is as follows:

[0134] Execute transaction 1:

[0135] 1. Query the record of student A in the student score record table and student basic information table;

[0136] 2. Generate hash values ​​for the columns of interest, such as student A's student ID and Chinese score in the student score table, and student A's ranking (last exam) in the student basic information table, and store them in memory;

[0137] 3. Fill in your Chinese language score.

[0138] Switch execution transaction 2:

[0139] 1. Query the student basic information table;

[0140] 2. Update the student rankings based on the current number of students and the last student rankings.

[0141] After transaction 2 is executed, switch to transaction 1:

[0142] 4. Update the Chinese language score and average score records corresponding to Student A, and verify the Hash value of the corresponding column; because the Hash verification fails (that is, the hash value has changed), the update of the Chinese language score and the corresponding average score fails.

[0143] 5. Roll back to step 1 and execute again.

[0144] In the above process, since the ranking of student A will be changed after the successful submission of transaction 2 (excluding the case where the ranking of student A does not change before and after the successful submission of transaction 2), the hash check of transaction 1 will fail in step 4, and it is determined that there is a concurrent transaction that conflicts with transaction 1. Therefore, transaction 1 can be rolled back.

[0145] Compared with pessimistic locking, the field-level concurrent lock in the embodiment of the present application does not actually lock the table, so it will not cause deadlock and has higher reading performance; compared with optimistic locking, the field-level concurrent lock in the embodiment of the present application does not require additional fields to access version numbers or timestamps. At the same time, when there are a large number of writes, since only the column of interest (i.e., the second field) is selected as the basis for comparison each time, the probability of long-term spinning when different businesses change different fields of the same table can be reduced.

[0146] Based on the same inventive concept, the present application embodiment provides a transaction concurrency control device, please refer to Figure 8 , the device comprises:

[0147] An opening module 801 is used to open a first transaction based on a received service request;

[0148] The acquisition module 802 is used to acquire, from a database, value information of at least one first field to be queried by the business request and value representation information of at least one second field associated with the business request;

[0149] A first processing module 803 is used to perform business processing based on the value information of the at least one first field to obtain a business processing result;

[0150] A judgment module 804 is used to judge whether there is a transaction that conflicts with the first transaction among the multiple concurrent second transactions based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database;

[0151] The second processing module 805 is used to process the business processing result based on the judgment result to end the first transaction.

[0152] In some embodiments, the determination module 804 is specifically used to:

[0153] Determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same;

[0154] Based on whether there is a second field whose value changes, it is determined whether there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0155] In some embodiments, the value representation information of the at least one second field is a hash value, and the determination module 804 is specifically used to:

[0156] If the cached hash value of the at least one second field is the same as the currently acquired hash value of the at least one second field, it is determined that there is no second field whose value has changed;

[0157] If the cached hash value of the at least one second field is different from the currently acquired hash value of the at least one second field, it is determined that there is a second field whose value has changed.

[0158] In some embodiments, the determination module 804 is specifically used to:

[0159] If there is no second field whose value has changed, determining that there is no transaction in the plurality of second transactions that conflicts with the first transaction;

[0160] If there is a second field whose value changes, it is determined that there is a transaction in the multiple second transactions that conflicts with the first transaction.

[0161] In some embodiments, the second processing module 805 is specifically used to:

[0162] If it is determined that there is no transaction that conflicts with the first transaction, writing the business processing result to the database, and committing the first transaction after successful writing;

[0163] If it is determined that there is a transaction that conflicts with the first transaction, the business processing result is discarded and the first transaction is rolled back.

[0164] In some embodiments, the second processing module 805 is specifically used for:

[0165] The step of obtaining the value information of the at least one first field from the database, replacing the cached value representation information of the at least one second field with the latest acquired value representation information, performing business processing based on the value information of the at least one first field, and obtaining a business processing result.

[0166] Based on the same inventive concept, the embodiment of the present application provides an electronic device, which can realize the function of the concurrent control device of the transaction discussed above, please refer to Fig. 9 The device includes a memory 901, one or more processors 902 and a bus 903.

[0167] The memory 901 is used to store computer programs executed by the processor 902. The memory 901 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and programs required for running the instant messaging function, etc.; the data storage area may store various instant messaging information and operation instruction sets, etc.

[0168] The memory 901 may be a volatile memory, such as a random-access memory (RAM); the memory 901 may also be a non-volatile memory, such as a read-only memory, a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD), or the memory 901 may be any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 901 may be a combination of the above memories.

[0169] The processor 902 may include one or more central processing units (CPU) or a digital processing unit, etc. The processor 902 is used to implement the transaction concurrency control method in the above embodiment when calling the computer program stored in the memory 901 .

[0170] The specific connection medium between the memory 901 and the processor 902 is not limited in the embodiment of the present application. Fig. 9 In the embodiment, the memory 901 and the processor 902 are connected via a bus 903. The bus 903 is connected to the processor 902 via a bus 903. Fig. 9 The connections between other components are shown in bold lines, which are only for illustration and are not intended to be limiting. Bus 903 can be divided into address bus, data bus, control bus, etc. For ease of illustration, Fig. 9 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0171] Based on the same inventive concept, the embodiment of the present application provides a computer-readable storage medium, and the computer program product includes: computer program code, when the computer program code is executed on a computer, the computer executes any of the transaction concurrency control methods discussed above. Since the principle of solving the problem by the above-mentioned computer-readable storage medium is similar to the transaction concurrency control method, the implementation of the above-mentioned computer-readable storage medium can refer to the implementation of the method, and the repeated parts will not be repeated.

[0172] Based on the same inventive concept, the embodiment of the present application also provides a computer program product, which includes: computer program code, when the computer program code is run on a computer, the computer executes any of the transaction concurrency control methods discussed above. Since the principle of solving the problem by the above computer program product is similar to the transaction concurrency control method, the implementation of the above computer program product can refer to the implementation of the method, and the repeated parts will not be repeated.

[0173] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0174] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0175] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0176] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of user-operated steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0177] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A transaction concurrency control method, characterized in that: include: Based on the received business request, start a first transaction; Acquire from a database value information of at least one first field to be queried by the business request, and value representation information of at least one second field associated with the business request; Performing business processing based on the value information of the at least one first field to obtain a business processing result; Based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database, determining whether there is a transaction in conflict with the first transaction among the multiple concurrent second transactions; Based on the determination result, the business processing result is processed to end the first transaction.

2. The method according to claim 1, characterized in that The method further comprises: determining, based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database, whether there is a transaction in conflict with the first transaction among the multiple concurrent second transactions, including: Determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same; Based on whether there is a second field whose value changes, it is determined whether there is a transaction in the multiple second transactions that conflicts with the first transaction.

3. The method according to claim 2, characterized in that The value representation information of the at least one second field is a hash value, and determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same includes: If the cached hash value of the at least one second field is the same as the currently acquired hash value of the at least one second field, it is determined that there is no second field whose value has changed; If the cached hash value of the at least one second field is different from the currently acquired hash value of the at least one second field, it is determined that there is a second field whose value has changed.

4. The method according to claim 2, characterized in that Determining whether there is a transaction in the plurality of second transactions that conflicts with the first transaction based on whether there is a second field whose value changes, includes: If there is no second field whose value has changed, determining that there is no transaction in the plurality of second transactions that conflicts with the first transaction; If there is a second field whose value changes, it is determined that there is a transaction in the multiple second transactions that conflicts with the first transaction.

5. The method according to any one of claims 1 to 4, characterized in that: Based on the judgment result, the business processing result is processed, including: If it is determined that there is no transaction that conflicts with the first transaction, writing the business processing result to the database, and committing the first transaction after successful writing; If it is determined that there is a transaction that conflicts with the first transaction, the business processing result is discarded and the first transaction is rolled back.

6. The method according to claim 5, characterized in that Rolling back the first transaction includes: The step of obtaining the value information of the at least one first field from the database, replacing the cached value representation information of the at least one second field with the latest acquired value representation information, performing business processing based on the value information of the at least one first field, and obtaining a business processing result.

7. A transaction concurrency control device, characterized in that: include: An opening module, configured to open a first transaction based on a received service request; An acquisition module, used to acquire from a database value information of at least one first field to be queried by the business request, and value representation information of at least one second field associated with the business request; A first processing module, used to perform business processing based on the value information of the at least one first field to obtain a business processing result; a judgment module, configured to judge whether there is a transaction that conflicts with the first transaction among the multiple concurrent second transactions based on the cached value representation information of the at least one second field and the value representation information of the at least one second field currently obtained from the database; The second processing module is used to process the business processing result based on the judgment result to end the first transaction.

8. The device according to claim 7, characterized in that The judgment module is specifically used for: Determining whether there is a second field whose value has changed based on whether the cached value representation information of the at least one second field and the currently acquired value representation information of the at least one second field are the same; Based on whether there is a second field whose value changes, it is determined whether there is a transaction in the multiple second transactions that conflicts with the first transaction.

9. The device according to claim 8, characterized in that The value representation information of the at least one second field is a hash value, and the judgment module is specifically used to: If the cached hash value of the at least one second field is the same as the currently acquired hash value of the at least one second field, it is determined that there is no second field whose value has changed; If the cached hash value of the at least one second field is different from the currently acquired hash value of the at least one second field, it is determined that there is a second field whose value has changed.

10. The device according to claim 8, characterized in that The judgment module is specifically used for: If there is no second field whose value has changed, determining that there is no transaction in the plurality of second transactions that conflicts with the first transaction; If there is a second field whose value changes, it is determined that there is a transaction in the multiple second transactions that conflicts with the first transaction.

11. The device according to any one of claims 7 to 10, characterized in that: The second processing module is specifically used for: If it is determined that there is no transaction that conflicts with the first transaction, writing the business processing result to the database, and committing the first transaction after successful writing; If it is determined that there is a transaction that conflicts with the first transaction, the business processing result is discarded and the first transaction is rolled back.

12. The device according to claim 11, characterized in that The second processing module is specifically used for: The step of obtaining the value information of the at least one first field from the database, replacing the cached value representation information of the at least one second field with the latest acquired value representation information, performing business processing based on the value information of the at least one first field, and obtaining a business processing result.

13. An electronic device, characterized in that: include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute the steps included in any one of claims 1-6 according to the obtained program instructions.

14. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes the method according to any one of claims 1 to 6.

15. A computer program product, characterized in that The computer program product comprises: a computer program code, and when the computer program code is run on a computer, the computer is enabled to execute the method according to any one of claims 1 to 6.