WO2017128028A1 - 一种事务处理方法及装置 - Google Patents

一种事务处理方法及装置 Download PDF

Info

Publication number
WO2017128028A1
WO2017128028A1 PCT/CN2016/072091 CN2016072091W WO2017128028A1 WO 2017128028 A1 WO2017128028 A1 WO 2017128028A1 CN 2016072091 W CN2016072091 W CN 2016072091W WO 2017128028 A1 WO2017128028 A1 WO 2017128028A1
Authority
WO
WIPO (PCT)
Prior art keywords
transaction
version number
priority
submitted
conflict
Prior art date
Application number
PCT/CN2016/072091
Other languages
English (en)
French (fr)
Inventor
梁晓豪
张军
刘哲
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Priority to CN201680000995.9A priority Critical patent/CN107533474B/zh
Priority to PCT/CN2016/072091 priority patent/WO2017128028A1/zh
Publication of WO2017128028A1 publication Critical patent/WO2017128028A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements

Definitions

  • the present invention relates to the field of data processing, and in particular, to a transaction processing method and apparatus.
  • a database transaction (hereinafter referred to as a transaction) is a logical unit in the execution of a database management system and consists of a limited sequence of database operations.
  • a transaction can be a SQL (Structured Query Language) statement (such as an update operation), or a set of SQL statements (such as several additions and deletions).
  • SQL Structured Query Language
  • a transaction is a complete logical process consisting of a series of operations that cannot be split. For example, in a transaction, the deduction from one account and the increment of another account must be successful or fail at the same time.
  • This is summarized as four attributes of transaction: atomicity, consistency, isolation, and persistence. These four attributes are often referred to as ACID characteristics.
  • Different database transaction implementations are different, and the row lock mechanism is handled differently.
  • MVCC Multiversion Concurrency Control
  • MySQL database uses locks (table locks, row locks, etc.) to avoid conflicts caused by database transaction concurrency.
  • the method is in the preparation stage of the transaction, such as adding, deleting, and modifying a certain row, and then obtaining the write lock of the row; the same row write locks are mutually exclusive between different transactions, and can only wait for a transaction to release the write lock. The rest of the transaction can get the row write lock and continue operation.
  • MySQL's write lock takes almost the same amount of time as the transaction, so the rest of the transaction waits longer, resulting in poor transaction concurrency.
  • the invention provides a transaction processing method and device for improving the concurrent performance of a transaction.
  • An aspect of the present invention provides a transaction processing method, the method comprising:
  • the transaction processing device needs to allocate a version number for the transaction to start execution, for example, assigning the first version number to the first transaction;
  • the transaction processing device determines whether the second transaction has been submitted;
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and then performing the first a transaction, wherein the second version number is a version number assigned to the second transaction;
  • the second transaction has been submitted, determining whether the first version number is smaller than the second version number, if not, performing a transaction failure processing flow on the first transaction, and if yes, performing the A business.
  • the conflict includes a read/write conflict or a write-write conflict.
  • the priority of the first transaction is determined to be higher than the priority of the second transaction, where the method includes:
  • the process for performing transaction failure processing on the first transaction specifically includes the following:
  • the process for performing transaction failure processing on the first transaction specifically includes the following:
  • the transaction processing methods for read transaction operations include the following:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction . Therefore, the query and read of the transaction are satisfied by assigning the final version number to the data submitted for the first transaction.
  • the present invention provides a transaction processing apparatus comprising:
  • An allocation unit configured to allocate a first version number to the first transaction
  • a processing unit configured to determine whether the second transaction has been submitted when determining that the second transaction has a conflict with the first transaction
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and then performing the first a transaction, wherein the second version number is a version number assigned to the second transaction;
  • the second transaction has been submitted, determining whether the first version number is smaller than the second version number, if not, performing a transaction failure processing flow on the first transaction, and if yes, performing the A business.
  • the conflict includes a read/write conflict or a write-write conflict.
  • the processing unit is configured to determine, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, specifically :
  • the processing unit is configured to: when the absolute value of the first version number is less than an absolute value of the second version number, determine that the priority of the first transaction is higher than the priority of the second transaction;
  • the processing unit is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processing unit is configured to allocate a third version number to the first transaction, and restart the execution of the first transaction, where the third version number is greater than the first version number.
  • the processing unit is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processing unit is configured to perform a failure rollback on the first transaction, and return a message that the transaction fails to the client.
  • processing unit is further configured to:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction . Therefore, the query and read of the transaction are satisfied by assigning the final version number to the data submitted for the first transaction.
  • the present invention provides a transaction processing apparatus comprising: a processor and a memory, wherein the memory is for storing instructions, the processor is for executing the instructions, and the instructions are being When executed, the transaction processing device is caused to perform the following steps:
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and then performing the first a transaction, wherein the second version number is a version number assigned to the second transaction;
  • the second transaction has been submitted, determining whether the first version number is smaller than the second version number, if not, performing a transaction failure processing flow on the first transaction, and if yes, performing the A business.
  • the conflict includes a read/write conflict or a write-write conflict.
  • the processor is configured to determine, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, specifically:
  • the processor configured to determine that a priority of the first transaction is higher than a priority of the second transaction when an absolute value of the first version number is less than an absolute value of the second version number;
  • the first transaction has a higher priority than the second transaction.
  • the processor is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processor is configured to allocate a third version number to the first transaction, and restart the execution of the first transaction, where the third version number is greater than the first version number.
  • the processor is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processor is configured to perform a failure rollback on the first transaction, and return a message that the transaction fails to the client.
  • the processor is further configured to:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction .
  • the query and read of the transaction are satisfied by assigning the final version number to the data submitted for the first transaction.
  • the transaction processing device allocates a first version number for the first transaction; and when it is determined that the second transaction has a conflict with the first transaction, determining the second transaction Whether the second transaction is not submitted, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the The first transaction, wherein the second version number is a version number assigned by the second transaction; if the second transaction has been submitted, determining whether the first version number is smaller than the second version number, If it is less, the transaction failure processing flow is performed on the first transaction, and if it is greater, the first transaction is executed.
  • FIG. 1 is a schematic structural diagram of a single server database system provided by the present invention.
  • FIG. 2 is a schematic structural diagram of a distributed database system provided by the present invention.
  • FIG. 3 is a schematic flow chart of an embodiment of a transaction processing method provided by the present invention.
  • FIG. 4 is a schematic structural diagram of an embodiment of a transaction processing apparatus according to the present invention.
  • FIG. 5 is a schematic structural diagram of another embodiment of a transaction processing apparatus according to the present invention.
  • first, second, etc. may be used to describe individual users or terminals in the embodiments of the present invention, the user or terminal should not be limited to these terms. These terms are only used to distinguish users or terminals from each other.
  • first user may also be referred to as a second user without departing from the scope of the embodiments of the present invention.
  • second user may also be referred to as a first user; similarly, the second user may also be This is not limited by the embodiment of the present invention.
  • FIG. 1 and 2 are schematic diagrams of an application scenario architecture according to an embodiment of the present invention.
  • FIG. 1 is a single server database system
  • FIG. 2 is a distributed database system.
  • a single server database system (ie, a server) includes a database management system and a database for processing a client request (eg, adding, deleting, and changing) and returning a result of the request to the client, wherein the database management system includes a transaction manager. And other management components for managing the database system.
  • the method of the embodiment of the present invention may be executed by a server, or a database management system in a server, or a transaction manager in a database management system.
  • the distributed database system includes a plurality of servers and a distributed management system for processing client requests (such as additions and deletions) and returning result requests to the client, wherein each server includes a database management system and a database.
  • the distributed management system includes management components such as a transaction manager for managing the distributed database system.
  • the method of the embodiment of the present invention may be performed by a distributed database system, or a distributed management system in a distributed database system, or a transaction manager in a distributed management system.
  • Atomicity A transaction is a complete and inseparable unit of work. A set of operations within a transaction either succeed or fail. If it must be aborted after a certain step, it needs to be rolled back to the state at the beginning of the transaction. .
  • Consistency Indicates that the integrity of the data is not corrupted at the beginning and after the transaction.
  • Isolation It shows the degree of mutual interference when two transactions are executed at the same time. It is divided into 4 levels, from the lowest to the highest, in order: (1) read uncommitted: no lock is set, so the intermediate state of the data It may be read by another transaction, also known as "dirty reading.” This level is rarely used practically unless there are good reasons for it. (2) Read committed: The read lock will be released after reading, so other transactions may change the data of the read lock, so that when the current transaction is read again, it may be read. Other transactions have modified data, so a certain data read by a transaction may become another data after the next read. (3) Repeatable read: The read lock will block the writing of other transactions. As long as the data is read in the transaction, it is locked, and other transactions cannot be modified.
  • the processing of a transaction is usually divided into two phases: the Prepare phase and the Commit phase.
  • Transaction preparation phase Indicates the start of a transaction, including sending/executing various database operations (such as adding, deleting, and revising). Database operations at this stage are not committed to the database and can be revoked/rollback; when an exception occurs (such as a system down), the operation of the transaction may be completely lost. In addition, due to transaction isolation, this phase Data operations are not visible to other customers.
  • Transaction commit phase Includes a commit action for a limited sequence of database operations after the transaction begins. There are two results of this action: 1. Success, the sequence of operations of the transaction is successfully submitted. Due to the persistence of the transaction, the transaction is permanently saved and can be read by other clients. 2. Failure, there is any transaction in the transaction. If an operation fails to commit, the entire transaction fails, all the sequences of operations fail, and the state is rolled back to the state before the transaction started.
  • the embodiment of the present invention provides a transaction processing method.
  • the method of the present invention is mainly executed by a transaction processing device, which may be a server in a single server database system architecture, or a database management system in a server, or a database management system.
  • a transaction processing method includes:
  • the transaction processing device needs to allocate a version number for the transaction to be executed, and the version number is used for conflict processing when the transaction conflicts.
  • the assigned version numbers are different. For example, the first version number is different from the second version number assigned to the second transaction.
  • the conflict includes a read/write conflict or a write-write conflict.
  • transaction concurrency will bring about transaction conflicts, mainly including read-write conflicts or write-write conflicts.
  • the conflict mainly includes: update loss, dirty read, non-repeatable read, phantom read.
  • the version number is the version number assigned to the second transaction.
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the first transaction, where the first transaction
  • the second version number is the version number assigned to the second transaction.
  • the second transaction with a lower priority may directly perform the failure processing, or wait for the first transaction with a high priority to be executed to determine whether to continue the execution of the second transaction or the failure processing.
  • an update is lost: an update of one transaction overrides an update of another transaction.
  • the first transaction is transaction A, the first version number is 100; transaction A: depositing 100 yuan to the bank card.
  • the second transaction is transaction B, the second version number is 101; transaction B: deposits 200 yuan to the bank card.
  • a and B simultaneously read the balance of the bank card, respectively update the balance, and the transaction B submitted after the transaction A covers the update of the transaction A.
  • the loss of updates is essentially a conflict of write operations.
  • the embodiment of the present invention if the second transaction is not committed, for example, in the above transaction A and Before B commits (including the preparation phase of transaction A, B or the stage to be submitted), resolve the conflicts generated by transactions A and B, to avoid transactions A and B in the preparation phase and the commit phase due to transaction conflicts, waiting for processing time is too long
  • the transaction priority may be determined according to the version number 100 of the transaction A and the version number 101 of the transaction B, and the transaction with high priority is executed according to the determined transaction priority.
  • the transaction with the highest priority is regarded as the first transaction
  • the first transaction is executed
  • the transaction with the lower priority is used as the second transaction, which may be
  • the second transaction performs failure processing or waits for the execution of the second transaction or the failure processing after the execution of the first transaction having the highest priority.
  • the determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction specifically:
  • the version number 100 of the transaction A is smaller than the version number 101 of the transaction B. If it is specified that the absolute value of the version number of the transaction is small, the priority of the transaction is high, then the priority of the transaction A can be determined to be high; or, if the transaction A The version number is 101, and the version number of transaction B is 100. If the absolute value of the version number of the transaction is large, the priority of the transaction is high, and the version number 101 of the transaction A is greater than the version number 100 of the transaction B, and the transaction A is determined. The priority is high.
  • dirty read A transaction reads data that has not been committed by another transaction.
  • Transaction B: Zhang San inquires the balance. After transaction A has been transferred (not yet submitted), transaction B has an additional $100. Transaction A rolls back due to some kind of problem, such as timeout. The data queried by transaction B is fake data.
  • the dirty read is essentially a conflict between read and write operations.
  • the transaction priority can be determined, and the priority is executed according to the determined transaction priority.
  • Other transaction conflict issues such as non-repeatable reading and phantom reading can also be referred to the above manner, and will not be described here.
  • the problem may be: determining whether the first version number of the first transaction is smaller than the second version number of the second transaction, if not, executing a transaction failure processing flow on the first transaction; if greater than, executing the first Transaction. It can be understood that a transaction with a smaller version number occurs earlier than a transaction with a larger version number.
  • the process for performing a transaction failure processing on the first transaction specifically includes:
  • the process for performing a transaction failure processing on the first transaction specifically includes:
  • the transaction processing device allocates a first version number for the first transaction; when it is determined that the second transaction has a conflict with the first transaction, determining whether the second transaction has been submitted; Not submitting, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the first transaction, where the first The second version number is the version number assigned to the second transaction; if the second transaction has been submitted, determining whether the first version number is smaller than the second version number, and if less, performing the first transaction
  • the transaction failure processing flow if it is greater, executes the first transaction.
  • the version number described in the above embodiment is used to resolve transaction conflicts and effectively reduce the length of time that the transaction waits for processing.
  • the version number of the transaction is generally used to replace the actual time when the transaction occurred; and each time, when a new transaction is started, the version number of the transaction is automatically incremented.
  • the transaction data can be queried and read according to the actual time when the transaction represented by the version number occurs. Therefore, when the first transaction is ready to be submitted, the transaction processing apparatus in the embodiment of the present invention may allocate a final version number to the first transaction, and the final version number is used as the version number of the data processed by the first transaction. Query and read the data processed by the first firm.
  • the method further includes:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction .
  • the version number 100 also serves as the version number of the data processed by the first transaction; the user performs a read transaction operation on the data submitted by the first transaction.
  • the fourth version number assigned by the transaction processing device for the third transaction is 150. Then, when reading the data of the first transaction, since the final version number 100 of the data of the first transaction is smaller than the version number (ie, the fourth version number) 150 of the read transaction, the first office is read.
  • the processed data is the data to be read; if the transaction manager assigns the final version number of the first transaction to 200, then when reading the data processed by the first transaction, due to the first transaction
  • the final version number 200 ie, the version number of the data processed by the first transaction
  • the small (ie, earlier than the first transaction) transaction processed the data (prioritized to read the transaction data closest to the version number 150, such as only the transaction data of the version number 140, 130, then The transaction data of the version number 140 is preferentially read), and then the judgment flow of the above read transaction is re-executed.
  • the data of the first transaction is not committed, the data of the first transaction is not read, but the read is smaller than the final version number of the first transaction (for example, the version number 100 listed above) (ie, Transaction data of an earlier version than the first transaction (first read the transaction closest to the version number 100, for example, only the transaction data of the version number 90, 80, then the transaction of the version number 90 is preferentially read.
  • the version number of the transaction data is less than the version number of the read transaction (ie, the fourth version number, such as the version number 150 listed above) , the judgment process of executing the subsequent read transaction.
  • the transaction processing apparatus includes:
  • the allocating unit 201 is configured to allocate a first version number to the first transaction
  • the processing unit 202 is configured to determine, when the second transaction has a conflict with the first transaction, whether the second transaction has been submitted;
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the first transaction Wherein the second version number is a version number assigned to the second transaction;
  • the allocating unit 201 allocates a first version number for the first transaction; when determining that there is a conflict between the second transaction and the first transaction, the processing unit 202 determines whether the second transaction has been submitted; The second transaction is not submitted, and the priority of the first transaction is determined to be higher than the priority of the second transaction according to the size of the first version number and the second version number, and the first transaction is executed, where The second version number is a version number assigned by the second transaction; if the second transaction has been submitted, determining whether the first version number is smaller than the second version number, if less than, the A transaction execution transaction failure processing flow, if greater than, executing the first transaction.
  • the conflict includes a read/write conflict or a write-write conflict.
  • the processing unit 202 is configured to determine, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, specifically:
  • the processing unit 202 is configured to: when the absolute value of the first version number is less than an absolute value of the second version number, determine that the priority of the first transaction is higher than the priority of the second transaction;
  • the processing unit 202 is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processing unit 202 is configured to allocate a third version number to the first transaction, and restart the execution of the first transaction, where the third version number is greater than the first version number.
  • the processing unit 202 is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processing unit 202 is configured to perform a failure rollback on the first transaction, and return a message that the transaction fails to the client.
  • processing unit 202 is further configured to:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction .
  • FIG. 4 illustrates the specific structure of the transaction processing apparatus from the perspective of the functional unit.
  • the specific structure of the transaction processing apparatus will be described from a hardware perspective with reference to the embodiment shown in FIG. 5:
  • the transaction device includes a receiver 301, a transmitter 302, a processor 303, and a memory 304.
  • Memory 304 can include read only memory and random access memory and provides instructions and data to processor 303.
  • a portion of the memory 304 may also include a non-volatile random access memory.
  • the memory 304 stores the following elements, executable modules or data structures, or subsets thereof, or their extended sets:
  • Operation instructions include various operation instructions for implementing various operations.
  • Operating system Includes a variety of system programs for implementing various basic services and handling hardware-based tasks.
  • the transaction manager may have more or less components than those shown in FIG. 5, may combine two or more components, or may have different component configurations or settings, and each component may be Hardware, software, or a combination of hardware and software, including one or more signal processing and/or application specific integrated circuits.
  • the processor 303 is configured to:
  • the second transaction is not committed, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the first transaction Wherein the second version number is a version number assigned to the second transaction;
  • the processor 303 allocates a first version number for the first transaction, and determines whether the second transaction has been submitted when determining that the second transaction has a conflict with the first transaction; Not submitting, determining, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, and executing the first transaction, where the first The second version number is the version number assigned to the second transaction; if the second transaction has been submitted, determining whether the first version number is smaller than the second version number, and if less, performing the first transaction
  • the transaction failure processing flow if it is greater, executes the first transaction.
  • the conflict includes a read/write conflict or a write-write conflict.
  • the processor 303 is configured to determine, according to the size of the first version number and the second version number, that the priority of the first transaction is higher than the priority of the second transaction, specifically:
  • the processor 303 configured to: when the absolute value of the first version number is less than an absolute value of the second version number, determine that the priority of the first transaction is higher than the priority of the second transaction;
  • the processor 303 is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processor 303 is configured to allocate a third version number to the first transaction, and restart the execution of the first transaction, where the third version number is greater than the first version number.
  • the processor 303 is configured to perform a transaction failure processing process on the first transaction, specifically:
  • the processor 303 is configured to perform a failure rollback on the first transaction, and return a message that the transaction fails to the client.
  • the processor 303 is further configured to:
  • the third transaction reads the data submitted by the first transaction, determining whether the final version number of the first transaction is smaller than the fourth version number, and if so, reading the data submitted by the first transaction .
  • the disclosed system, apparatus, and method may be implemented in other manners.
  • the device embodiments described above are merely illustrative.
  • the division of the unit is only a logical function division.
  • there may be another division manner for example, multiple units or components may be combined or Can be integrated into another system, or some features can be ignored or not executed.
  • the mutual coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interface, device or unit, and may be in an electrical, mechanical or other form.
  • the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of the embodiment.
  • each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
  • the above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
  • the integrated unit if implemented in the form of a software functional unit and sold or used as a standalone product, may be stored in a computer readable storage medium.
  • the technical solution of the present invention which is essential or contributes to the prior art, or all or part of the technical solution, may be embodied in the form of a software product stored in a storage medium.
  • a number of instructions are included to cause a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention.
  • the storage medium includes: a U disk, a removable hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, and the like, which can store program codes.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

一种事务处理方法及装置,用于提高事务的并发性能。所述事务处理方法包括:为第一事务分配第一版本号(101);当确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交(102);若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号(103);若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号(104),若小于,则对所述第一事务执行事务失败处理流程(105),若大于,则执行所述第一事务(106)。

Description

一种事务处理方法及装置 技术领域
本发明涉及数据处理领域,特别涉及一种事务处理方法及装置。
背景技术
数据库事务(以下简称事务),是数据库管理系统执行过程中的一个逻辑单元,由一个有限的数据库操作序列构成。如一个事务可以是一条SQL(Structured Query Language,结构化查询语言)语句(例如一个更新操作),也可以是一组SQL语句(例如若干个增删改查操作)。
事务是由一系列操作组成的一个完整的逻辑过程,这些操作不可拆分,比如在交易时,从一个账户的计减与另一个账户的计增,必须同时成功或者同时失败。这被概括为事务四个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。不同的数据库的事务实现方式各有不同,对行锁机制处理也不同。现在数据库的并发性能要求较高,使用多版本并发控制(MVCC,Multiversion Concurrency Control)技术,以避免很多情况下的加锁操作,使得在读取期间锁定需要的记录的同时,还允许非锁定的读取,从而提升系统性能。但无论是MySQL的悲观锁,还是Spanner的读写锁,由数据库事务并发带来的冲突问题在处理方面均不太理想。
MySQL数据库中采用锁(表锁、行锁等)的方法来避免数据库事务并发带来的冲突问题。该方法在事务的准备阶段,如对某一行进行增删改操作,就会获得该行的写锁;不同的事务之间相同的行写锁互斥,只能等待一事务释放该写锁后,其余的事务才能获得该行写锁,继续操作。但是,MySQL的写锁占用时间几乎等同于事务的持续时间,因此,其余的事务等待处理的时间较长,导致事务并发性能低下。
发明内容
本发明提供了一种事务处理方法及装置,用于提高事务的并发性能。
本发明一方面提供了一种事务处理方法,该方法包括:
事务发生时,事务处理装置需要为开始执行的事务分配版本号,例如为第一事务分配第一版本号;
如果在执行第二事务时,出现与所述第一事务存在冲突的情况,在确定所述冲突存在的情况下,事务处理装置判断所述第二事务是否已提交;
如果所述第二事务未提交,那么根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,然后执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
如果所述第二事务已提交,那么确定所述第一版本号是否小于所述第二版本号,如果小于,则对所述第一事务执行事务失败处理流程,如果大于,则执行所述第一事务。通过对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
在一个可能的实现方式中,所述冲突包括读写冲突或写写冲突。
在一个可能的实现方式中,具体可通过如下方式确定所述第一事务的优先级高于所述第二事务的优先级,该方式包括:
当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
在一个可能的实现方式中,所述对所述第一事务执行事务失败处理流程具体包括如下:
为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
在一个可能的实现方式中,所述对所述第一事务执行事务失败处理流程具体包括如下:
对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
对于读事务操作的事务处理方法,包括如下:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。因此,通过为第一事务提交的数据分配最终版本号方式满足对事务的查询以及读取。
又一方面,本发明提供了一种事务处理装置,包括:
分配单元,用于为第一事务分配第一版本号;
处理单元,用于在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
如果所述第二事务未提交,那么根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,然后执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
如果所述第二事务已提交,那么确定所述第一版本号是否小于所述第二版本号,如果小于,则对所述第一事务执行事务失败处理流程,如果大于,则执行所述第一事务。通过对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
在一个可能的实现方式中,所述冲突包括读写冲突或写写冲突。
在一个可能的实现方式中,所述处理单元用于根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体为:
所述处理单元,用于当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
在一个可能的实现方式中,所述处理单元用于对所述第一事务执行事务失败处理流程,具体为:
所述处理单元,用于为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
在一个可能的实现方式中,所述处理单元用于对所述第一事务执行事务失败处理流程,具体为:
所述处理单元,用于对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
在一个可能的实现方式中,所述处理单元还用于:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。因此,通过为第一事务提交的数据分配最终版本号方式满足对事务的查询以及读取。
另一方面,本发明提供了一种事务处理装置,包括:处理器以及存储器,其中,所述存储器用于存储指令,所述处理器用于执行所述指令,所述指令在被所述处理器执行时,使所述事务处理装置执行如下步骤:
为第一事务分配第一版本号;
在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
如果所述第二事务未提交,那么根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,然后执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
如果所述第二事务已提交,那么确定所述第一版本号是否小于所述第二版本号,如果小于,则对所述第一事务执行事务失败处理流程,如果大于,则执行所述第一事务。通过对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
在一个可能的实现方式中,所述冲突包括读写冲突或写写冲突。
在一个可能的实现方式中,所述处理器用于根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体为:
所述处理器,用于当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定 所述第一事务的优先级高于所述第二事务的优先级。
在一个可能的实现方式中,所述处理器用于对所述第一事务执行事务失败处理流程,具体为:
所述处理器,用于为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
在一个可能的实现方式中,所述处理器用于对所述第一事务执行事务失败处理流程,具体为:
所述处理器,用于对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
在一个可能的实现方式中,所述处理器还用于:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。通过为第一事务提交的数据分配最终版本号方式满足对事务的查询以及读取。
从以上技术方案可以看出,本发明实施例具有以下优点:事务处理装置为第一事务分配第一版本号;当确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。通过上述对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
附图说明
图1为本发明所提供的单服务器的数据库系统的架构示意图;
图2为本发明所提供的分布式数据库系统的架构示意图;
图3为本发明所提供的事务处理方法的一个实施例流程示意图;
图4为本发明所提供的事务处理装置的一个实施例结构示意图;
图5为本发明所提供的事务处理装置的另一实施例结构示意图。
具体实施方式
下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,下面所描述的实施例仅仅是本发明一部分实施例,而非全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。
应当理解,尽管在本发明实施例中可能采用术语第一、第二等来描述各个用户或终端,但用户或终端不应限于这些术语。这些术语仅用来将用户或终端彼此区分开。例如,在不脱离本发明实施例范围的情况下,第一用户也可以被称为第二用户,类似地,第二用户也可以被称为第一用户;同样的,第二用户也可以被称为第三用户等等,本发明实施例对此不做限制。
本发明实施例的技术方案可以应用于数据库系统中。图1和图2是本发明实施例的应用场景架构示意图,其中,图1为单服务器的数据库系统,图2为分布式数据库系统。
如图1所示,单服务器的数据库系统(即服务器)包括数据库管理系统和数据库,用于处理客户端的请求(例如增删改查)并向客户端返回请求结果,其中数据库管理系统包括事务管理器等管理组件,用于管理该数据库系统。针对该架构,本发明实施例的方法可以由服务器、或服务器中的数据库管理系统、或数据库管理系统中的事务管理器执行。
如图2所示,分布式数据库系统包括多个服务器和分布式管理系统,用于处理客户端的请求(例如增删改查)并向客户端返回请求结果,其中每个服务器包括数据库管理系统和数据库,分布式管理系统包括事务管理器等管理组件,用于管理该分布式数据库系统。针对该架构,本发明实施例的方法可以由分布式数据库系统、或分布式数据库系统中的分布式管理系统、或分布式管理系统中的事务管理器执行。
关于事务处理方法中事务的ACID特性的介绍如下:
原子性(Atomicity):一个事务是一个完整的不可分割的工作单元,事务内的一组操作要么都成功,要么都失败,如果在某一步之后必须中止,那么需要回滚到事务开始时的状态。
一致性(Consistency):说明事务开始时和结束后,没有破坏数据的完整性。
隔离性(Isolation):说明两个事务同时执行时的互相干扰程度,分为4级,从隔离程度最低到最高,依次为:(1)读未提交:不设置任何锁,所以数据的中间状态,可能被另外的事务读到,也被称为“脏读”。除非有很好的理由,这种级别很少实际使用。(2)读已提交:读锁在读取之后会被释放掉,所以这样其他事务可能会更改那些被释放了读锁的数据,这样当前事务再去读取的时候,就可能读取到被别的事务修改过的数据,所以一个事务读取到的某个数据,可能下次读取就变成别的数据。(3)可重复读:读锁会阻塞其他事务的写,只要是事务中读取到的数据,都被加了锁,其他事务没办法修改。可确保同一事务的多个实例在并发读取数据时,都读到同样的数据。但是有“幻读”问题(读取某一范围的数据时,另一个事务又在该范围内插入了新数据,当再读取该范围的数据行时,会发现有新的幻影行)。(4)可串行化:就是所有操作都顺序执行。由于没有并发支持能力,该级别也甚少使用。
持久性(Durability):在事务完成以后,该事务对数据所作的更改需要持久地保存起来,并且是完全的。
对于事务的处理通常分为两个阶段:事务准备(Prepare)阶段和事务提交(Commit)阶段。
事务准备阶段:表示事务开始,包括发送/执行各种数据库操作(例如增删改查)。该阶段的数据库操作没有提交到数据库,可以被撤销/回滚(rollback);当发生异常情况(如系统down机)时,该事务的操作可能完全丢失,另外,由于事务隔离性,该阶段的数据操作对于其他客户是不可见的。
事务提交阶段:包括对事务开始后的一个有限的数据库操作序列进行提交动作。该动作的结果有两种:1、成功,该事务的操作序列提交成功,由于事务的持久性,该事务被永久保存,也可以被其他客户读取到;2、失败,该事务中有任何一个操作提交失败,则整个事务都失败,所有的操作序列失效,回滚(rollback)至事务开始前状态。
本发明实施例提供了一种事务处理方法,本发明方法主要由事务处理装置执行,该事务处理装置可以是单服务器的数据库系统架构中的服务器、或服务器中的数据库管理系统、或数据库管理系统中的事务管理器;或者是分布式数据库系统架构中的分布式数据库系统、或分布式数据库系统中的分布式管理系统、或分布式管理系统中的事务管理器。请参阅图3,本发明所提供的事务处理方法的一个实施例包括:
101、为第一事务分配第一版本号。
需要说明的是,事务发生时,事务处理装置需要为开始执行的事务分配版本号,该版本号用于对事务发生冲突时进行冲突处理。其中,分配的版本号各不相同,例如,上述第一版本号与为第二事务分配的第二版本号不相同。
102、当确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;若未提交,则执行103,若已提交,则执行104。
可选的,该冲突包括读写冲突或写写冲突。
需要说明的是,事务并发会带来事务冲突的问题,主要包括读写冲突或写写冲突。该冲突主要包括:更新丢失、脏读、不可重复读、幻象读。
103、根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号。
如果该第二事务未提交,根据该第一版本号和第二版本号的大小确定该第一事务的优先级高于该第二事务的优先级,并执行该第一事务,其中,该第二版本号为该第二事务分配的版本号。而优先级低的该第二事务可能直接进行失败处理,或者等优先级高的该第一事务执行完后再判断是继续执行该第二事务还是进行失败处理。
例如,更新丢失:一个事务的更新覆盖了另一个事务的更新。例如第一事务为事务A,第一版本号为100;事务A:向银行卡存钱100元。第二事务为事务B,第二版本号为101;事务B:向银行卡存钱200元。A和B同时读到银行卡的余额,分别更新余额,后提交的事务B覆盖了事务A的更新。更新丢失本质上是写写操作的冲突。
因此,通过本发明实施例,如果该第二事务未提交,例如在上述事务A和 B提交之前(包括事务A、B的准备阶段或准备提交的阶段),解决事务A和B产生的冲突,以避免事务A、B在准备阶段和提交阶段时由于事务冲突,等待处理时间过长的问题。具体的,可以根据事务A的版本号100与事务B的版本号101的大小,进行事务优先级的确定,并根据确定的事务优先级执行优先级高的事务。本发明实施例中在存在两个事务的冲突,且冲突的事务未提交时,均将优先级高的事务作为第一事务,执行第一事务,优先级低的事务作为第二事务,可以对第二事务进行失败处理或等优先级高的第一事务执行完后再判断是继续执行第二事务还是进行失败处理。
可选的,所述根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体包括:
当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
例如,上述事务A的版本号100小于事务B的版本号101,如果规定当事务的版本号的绝对值小时,事务的优先级高,那么可以确定事务A的优先级高;或者,如果事务A的版本号为101,事务B的版本号为100,如果规定当事务的版本号的绝对值大时,事务的优先级高,事务A的版本号101大于事务B的版本号100,确定事务A的优先级高。
又例如,脏读:一个事务读取了另一个事务未提交的数据。事务A:张三妻子给张三转账100元。事务B:张三查询余额。事务A转账后(还未提交),事务B查询多了100元。事务A由于某种问题,比如超时,进行回滚。事务B查询到的数据是假数据。脏读本质上是读写操作的冲突,同样的,根据事务A的版本号100与事务B的版本号101的大小,可以进行事务优先级的确定,并根据确定的事务优先级执行优先级高的事务。其他的事务冲突问题例如不可重复读、幻象读也可参考以上方式,此处不再赘述。
104、确定所述第一版本号是否小于所述第二版本号;若小于,则执行105,若大于,则执行106。
105、对所述第一事务执行事务失败处理流程。
106、执行所述第一事务。
如果该第二事务已提交,此时,由于该第二事务已提交,因此已提交的该第二事务无法通过确定优先级的方式确定事务的执行顺序,但为了避免这种情况下的事务冲突问题,可以通过确定第一事务的第一版本号是否小于所述第二事务的第二版本号,如果小于,那么就对该第一事务执行事务失败处理流程;如果大于,那么执行该第一事务。可以理解的是,版本号较小的事务的发生时间早于版本号较大的事务。
可选的,所述对所述第一事务执行事务失败处理流程具体包括:
为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。可以理解的是,版本号较小的事务的发生时间早于版本号较大的事务。
可选的,所述对所述第一事务执行事务失败处理流程具体包括:
对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
本发明实施例中,事务处理装置为第一事务分配第一版本号;当确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。通过上述对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
上述实施例中描述的版本号用于解决事务冲突,且有效的减少事务等待处理的时长。事务的版本号一般用来替代事务发生时的实际时间;且每一次,开始一个新事务时,事务的版本号都会自动递增。现有技术中,根据版本号所代表的事务发生时的实际时间,可以查询并读取该事务处理的数据。因此,在上述第一事务准备提交时,本发明实施例中的事务处理装置可以为该第一事务分配最终版本号,该最终版本号会作为第一事务所处理的数据的版本号,用于对第一事务所处理的数据进行查询以及读取。可选的,所述方法还包括:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。
例如,事务处理装置为该第一事务分配的最终版本号为100,则该版本号100也会作为第一事务所处理的数据的版本号;用户针对该第一事务提交的数据进行读事务操作时,事务处理装置为该第三事务分配的第四版本号为150。那么在读取该第一事务处理的数据时,由于该第一事务处理的数据的最终版本号100小于读事务的版本号(即第四版本号)150,读取到的该第一事务所处理的数据即为所需读取的数据;如果该事务管理器为该第一事务分配的最终版本号为200,那么在读取该第一事务所处理的数据时,由于该第一事务的最终版本号200(即第一事务所处理的数据的版本号)大于读事务的版本号(即第四版本号)150,该情况下,则读取比该第一事务的最终版本号200要小的(即比该第一事务更早版本的)事务所处理的该数据(优先读取最接近版本号150的事务处理的数据,例如只存在版本号140、130的事务处理的数据,则优先读取版本号140的事务处理的数据),然后重新执行上述读事务的判断流程。
其中,如果第一事务的数据未提交,则不读取该第一事务处理的数据,而是读取比该第一事务的最终版本号(例如上述列举的版本号100)要小的(即比该第一事务更早版本的)事务处理的数据(优先读取最接近版本号100的事务,例如只存在版本号90、80的事务处理的数据,则优先读取版本号90的事务处理的数据),直至读取到数据已提交的事务处理的数据;然后重新判断该事务处理的数据的版本号是否小于读事务的版本号(即第四版本号,例如上述列举的版本号150),执行后续读事务的判断流程。
以上对本发明所提供的事务处理方法进行了说明,下面将从装置的角度对事务处理装置的结构进行说明,请参阅图4,本发明所提供的事务处理装置,包括:
分配单元201,用于为第一事务分配第一版本号;
处理单元202,用于在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。
本发明实施例中,分配单元201为第一事务分配第一版本号;在确定第二事务与所述第一事务存在冲突时,处理单元202判断所述第二事务是否已提交;若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。通过上述对事务赋版本号解决事务冲突的方式可以有效减少事务等待处理的时长,提高事务的并发性能。
可选的,所述冲突包括读写冲突或写写冲突。
可选的,所述处理单元202用于根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体为:
所述处理单元202,用于当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
可选的,所述处理单元202用于对所述第一事务执行事务失败处理流程,具体为:
所述处理单元202,用于为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
可选的,所述处理单元202用于对所述第一事务执行事务失败处理流程,具体为:
所述处理单元202,用于对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
可选的,所述处理单元202还用于:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。
图4所示的实施例从功能单元的角度对事务处理装置的具体结构进行了说明,以下结合图5所示的实施例从硬件角度对事务处理装置的具体结构进行说明:
在一个实施方式中,如图5所示,该事务处理装置包括:接收器301、发射器302、处理器303和存储器304。存储器304可以包括只读存储器和随机存取存储器,并向处理器303提供指令和数据。存储器304的一部分还可以包括非易失性随机存取存储器。
存储器304存储了如下的元素,可执行模块或者数据结构,或者它们的子集,或者它们的扩展集:
操作指令:包括各种操作指令,用于实现各种操作。
操作系统:包括各种系统程序,用于实现各种基础业务以及处理基于硬件的任务。
本发明实施例涉及的事务管理器可以具有比图5所示出的更多或更少的部件,可以组合两个或更多个部件,或者可以具有不同的部件配置或设置,各个部件可以在包括一个或多个信号处理和/或专用集成电路在内的硬件、软件或硬件和软件的组合实现。
本发明实施例中,处理器303用于:
为第一事务分配第一版本号;
在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。
本发明实施例中,处理器303为第一事务分配第一版本号;在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。通过上述对事务赋版本号解决事务冲突的方式可以有效的减少事务等待处理的时长,提高事务的并发性能。
可选的,所述冲突包括读写冲突或写写冲突。
可选的,所述处理器303用于根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体为:
所述处理器303,用于当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
可选的,所述处理器303用于对所述第一事务执行事务失败处理流程,具体为:
所述处理器303,用于为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
可选的,所述处理器303用于对所述第一事务执行事务失败处理流程,具体为:
所述处理器303,用于对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
可选的,所述处理器303还用于:
在所述第一事务准备提交时,为所述第一事务分配最终版本号;
为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数 据的读事务操作;
当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。
上述装置的相关描述可以对应参阅方法实施例部分的相关描述和效果进行理解,本处不做过多赘述。
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统,装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。
在本申请所提供的几个实施例中,应该理解到,所揭露的系统,装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。
另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例所述方法的全部或部分步骤。而前述 的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储程序代码的介质。
以上所述,以上实施例仅用以说明本发明的技术方案,而非对其限制;尽管参照前述实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的范围。

Claims (13)

  1. 一种事务处理方法,其特征在于,包括:
    为第一事务分配第一版本号;
    当确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
    若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
    若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。
  2. 根据权利要求1所述的方法,其特征在于,所述冲突包括读写冲突或写写冲突。
  3. 根据权利要求1所述的方法,其特征在于,所述根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体包括:
    当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
    或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
  4. 根据权利要求1至3任一项所述的方法,其特征在于,所述对所述第一事务执行事务失败处理流程具体包括:
    为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
  5. 根据权利要求1至3任一项所述的方法,其特征在于,所述对所述第一事务执行事务失败处理流程具体包括:
    对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
  6. 根据权利要求1至3任一项所述的方法,其特征在于,所述方法还包括:
    在所述第一事务准备提交时,为所述第一事务分配最终版本号;
    为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
    当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。
  7. 一种事务处理装置,其特征在于,包括:
    分配单元,用于为第一事务分配第一版本号;
    处理单元,用于在确定第二事务与所述第一事务存在冲突时,判断所述第二事务是否已提交;
    若所述第二事务未提交,根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,并执行所述第一事务,其中,所述第二版本号为所述第二事务分配的版本号;
    若所述第二事务已提交,确定所述第一版本号是否小于所述第二版本号,若小于,则对所述第一事务执行事务失败处理流程,若大于,则执行所述第一事务。
  8. 根据权利要求7所述的事务处理装置,其特征在于,所述冲突包括读写冲突或写写冲突。
  9. 根据权利要求7所述的事务处理装置,其特征在于,所述处理单元用于根据所述第一版本号和第二版本号的大小确定所述第一事务的优先级高于所述第二事务的优先级,具体为:
    所述处理单元,用于当所述第一版本号的绝对值小于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级;
    或者,当所述第一版本号的绝对值大于所述第二版本号的绝对值时,确定所述第一事务的优先级高于所述第二事务的优先级。
  10. 根据权利要求7至9任一项所述的事务处理装置,其特征在于,所述处理单元用于对所述第一事务执行事务失败处理流程,具体为:
    所述处理单元,用于为所述第一事务分配第三版本号,并重新开始执行所述第一事务,所述第三版本号大于所述第一版本号。
  11. 根据权利要求7至9任一项所述的事务处理装置,其特征在于,所述 处理单元用于对所述第一事务执行事务失败处理流程,具体为:
    所述处理单元,用于对所述第一事务执行失败回滚,并向客户端返回事务失败的消息。
  12. 根据权利要求7至9任一项所述的事务处理装置,其特征在于,所述处理单元还用于:
    在所述第一事务准备提交时,为所述第一事务分配最终版本号;
    为第三事务分配第四版本号,所述第三事务是针对所述第一事务提交的数据的读事务操作;
    当所述第三事务读取所述第一事务提交的数据时,判断所述第一事务的最终版本号是否小于所述第四版本号,若是,则读取所述第一事务提交的数据。
  13. 一种事务处理装置,包括:处理器以及存储器,其特征在于,所述存储器用于存储指令,所述处理器用于执行所述指令,所述指令在被所述处理器执行时,使所述事务处理装置执行如权利要求1至6中任一项所述的方法。
PCT/CN2016/072091 2016-01-26 2016-01-26 一种事务处理方法及装置 WO2017128028A1 (zh)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN201680000995.9A CN107533474B (zh) 2016-01-26 2016-01-26 一种事务处理方法及装置
PCT/CN2016/072091 WO2017128028A1 (zh) 2016-01-26 2016-01-26 一种事务处理方法及装置

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2016/072091 WO2017128028A1 (zh) 2016-01-26 2016-01-26 一种事务处理方法及装置

Publications (1)

Publication Number Publication Date
WO2017128028A1 true WO2017128028A1 (zh) 2017-08-03

Family

ID=59396878

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2016/072091 WO2017128028A1 (zh) 2016-01-26 2016-01-26 一种事务处理方法及装置

Country Status (2)

Country Link
CN (1) CN107533474B (zh)
WO (1) WO2017128028A1 (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109814891A (zh) * 2017-11-16 2019-05-28 中兴通讯股份有限公司 一种数据更新方法及装置
CN111475585A (zh) * 2020-06-22 2020-07-31 阿里云计算有限公司 数据处理方法、装置和系统

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110347481A (zh) * 2019-07-17 2019-10-18 北京搜狐新媒体信息技术有限公司 一种实现分布式事务的方法及系统
CN115098228B (zh) * 2021-05-19 2023-04-14 腾讯科技(深圳)有限公司 事务处理方法、装置、计算机设备及存储介质
CN114297217A (zh) * 2021-12-31 2022-04-08 深圳市兆珑科技有限公司 事务并发控制方法、装置、电子设备及可读存储介质

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101216783A (zh) * 2007-12-29 2008-07-09 中国建设银行股份有限公司 一种针对多事务进行优化排序处理的方法
CN102272744A (zh) * 2009-01-02 2011-12-07 国际商业机器公司 用于事务存储器管理中的冲突仲裁的优先化
CN104714836A (zh) * 2013-12-12 2015-06-17 国际商业机器公司 用于聚结内存事务的方法和系统
US20150212851A1 (en) * 2014-01-24 2015-07-30 International Business Machines Corporation Indicating a low priority transaction
CN105045563A (zh) * 2015-06-19 2015-11-11 陕西科技大学 一种推测嵌套软件事务存储的冲突管理方法

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090183159A1 (en) * 2008-01-11 2009-07-16 Michael Maged M Managing concurrent transactions using bloom filters

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101216783A (zh) * 2007-12-29 2008-07-09 中国建设银行股份有限公司 一种针对多事务进行优化排序处理的方法
CN102272744A (zh) * 2009-01-02 2011-12-07 国际商业机器公司 用于事务存储器管理中的冲突仲裁的优先化
CN104714836A (zh) * 2013-12-12 2015-06-17 国际商业机器公司 用于聚结内存事务的方法和系统
US20150212851A1 (en) * 2014-01-24 2015-07-30 International Business Machines Corporation Indicating a low priority transaction
CN105045563A (zh) * 2015-06-19 2015-11-11 陕西科技大学 一种推测嵌套软件事务存储的冲突管理方法

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109814891A (zh) * 2017-11-16 2019-05-28 中兴通讯股份有限公司 一种数据更新方法及装置
CN109814891B (zh) * 2017-11-16 2023-08-08 中兴通讯股份有限公司 一种数据更新方法及装置
CN111475585A (zh) * 2020-06-22 2020-07-31 阿里云计算有限公司 数据处理方法、装置和系统
CN111475585B (zh) * 2020-06-22 2021-06-01 阿里云计算有限公司 数据处理方法、装置和系统

Also Published As

Publication number Publication date
CN107533474A (zh) 2018-01-02
CN107533474B (zh) 2020-07-07

Similar Documents

Publication Publication Date Title
US10810268B2 (en) High-throughput distributed transaction management for globally consistent sharded OLTP system and method of implementing
CN107771321B (zh) 数据中心中的恢复
KR102437664B1 (ko) 멀티테넌트 어플리케이션 서버 환경에서 트랜잭션 복구를 위한 시스템 및 방법
CN104793988B (zh) 跨数据库分布式事务的实现方法和装置
US9760584B2 (en) Systems and methods for supporting inline delegation of middle-tier transaction logs to database
WO2017128028A1 (zh) 一种事务处理方法及装置
US11132350B2 (en) Replicable differential store data structure
US10769128B2 (en) Delegated key-level locking for a transactional multi-version key-value store
KR101993432B1 (ko) 2-단계 커미트 호출들의 엄격한 순서화에 근거하여 트랜잭션 복구를 지원하는 시스템들 및 방법들
CN110196856B (zh) 一种分布式数据读取方法及装置
KR102121157B1 (ko) 동시 블록체인 트랜잭션 실패를 해결하기 위한 넌스 테이블의 이용
EP3824397B1 (en) Version-based table locking
US20230315721A1 (en) Snapshot isolation query transactions in distributed systems
CN113168371A (zh) 多主共享存储数据库的写-写冲突检测
CN112214649A (zh) 一种时态图数据库分布式事务解决系统
EP3377970B1 (en) Multi-version removal manager
CN110377614B (zh) 一种分布式环境下的订单处理锁系统
WO2024098363A1 (zh) 一种基于多核处理器的并发事务处理方法及其系统
CN111240891A (zh) 基于数据库多表间数据一致性的数据恢复方法及装置
CN117076147B (zh) 死锁检测方法、装置、设备和存储介质
US11914613B2 (en) Data visibility for nested transactions in distributed systems
CN117348977A (zh) 一种数据库中事务并发控制的方法、装置、设备及介质

Legal Events

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

Ref document number: 16886915

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 16886915

Country of ref document: EP

Kind code of ref document: A1