TWI805544B - Database operation method and device - Google Patents

Database operation method and device Download PDF

Info

Publication number
TWI805544B
TWI805544B TW106102692A TW106102692A TWI805544B TW I805544 B TWI805544 B TW I805544B TW 106102692 A TW106102692 A TW 106102692A TW 106102692 A TW106102692 A TW 106102692A TW I805544 B TWI805544 B TW I805544B
Authority
TW
Taiwan
Prior art keywords
database
database operation
execution
instruction
transaction
Prior art date
Application number
TW106102692A
Other languages
Chinese (zh)
Other versions
TW201828117A (en
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 TW106102692A priority Critical patent/TWI805544B/en
Publication of TW201828117A publication Critical patent/TW201828117A/en
Application granted granted Critical
Publication of TWI805544B publication Critical patent/TWI805544B/en

Links

Images

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Vehicle Body Suspensions (AREA)

Abstract

本發明提供一種資料庫操作方法及裝置。一種方法包括:在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令;對資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器以供應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄資料庫操作指令以及在資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄預測執行產生的預測執行資料;當獲取到目標事務中的事務提交指令時,根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。本發明可以提高事務執行效率,提高吞吐量。 The invention provides a database operation method and device. A method includes: during the process of the application program server executing the target transaction, sequentially obtaining the database operation instructions in the target transaction executed by the application program server; performing predictive execution on the database operation instructions, and returning the prediction execution result to the application The program server is used for the application program server to determine the next database operation command that needs to be executed, and to record the database operation command locally, and the database operation command belongs to the modification database type instruction and the locked non-modification database type When one of the instructions is recorded, the predicted execution data generated by the predicted execution is recorded; when the transaction submission command in the target transaction is obtained, the actual execution target of the database corresponding to the application server is controlled according to the locally recorded database operation command and the predicted execution data affairs. The invention can improve transaction execution efficiency and throughput.

Description

資料庫操作方法及裝置 Database operation method and device

本發明涉及資料庫技術領域,尤其涉及一種資料庫操作方法及裝置。 The invention relates to the technical field of databases, in particular to a database operation method and device.

資料庫事務(Database Transaction),是指作為單個邏輯工作單元執行的一系列操作,要麼完全地執行,要麼完全地不執行。一個事務由一系列資料庫操作指令構成,例如語句。 Database Transaction (Database Transaction) refers to a series of operations performed as a single logical unit of work, either completely executed or not executed at all. A transaction consists of a series of database operation instructions, such as statements.

在資料庫操作中,為了保證併發讀取資料的正確性,提出了事務隔離級別的概念。在標準結構化查詢語言(Structured Query Language,SQL)規範中,定義了4個事務隔離級別,其中,讀已提交(Read Committed)是多數應用程式優選考慮的隔離級別,它可以避免髒讀取,而且具有較好的併發性能,但仍會遇到不可重複讀的問題。 In the database operation, in order to ensure the correctness of concurrent read data, the concept of transaction isolation level is proposed. In the standard Structured Query Language (SQL) specification, four transaction isolation levels are defined, among which, Read Committed (Read Committed) is the preferred isolation level for most applications, which can avoid dirty reads, And it has better concurrency performance, but still encounters the problem of non-repeatable read.

對於讀已提交隔離級別事務面臨的不可重複讀問題,可以透過for update語句臨時性的提升讀已提交隔離級別事務中某個資料的隔離級別,以保證該資料可重複讀取, 但這會嚴重降低系統的併發性能,導致執行效率較低,事務吞吐量較低。 For the non-repeatable read problem faced by the read committed isolation level transaction, you can temporarily increase the isolation level of a certain data in the read committed isolation level transaction through the for update statement to ensure that the data can be read repeatedly, but this will seriously reduce the The concurrent performance of the system leads to low execution efficiency and low transaction throughput.

本發明的多個方面提供一種資料庫操作方法及裝置,用以保證資料庫系統的併發性能,提高事務執行效率並增大事務吞吐量。 Aspects of the present invention provide a database operation method and device, which are used to ensure the concurrent performance of the database system, improve transaction execution efficiency and increase transaction throughput.

本發明的一方面,提供一種資料庫操作方法,包括:在應用程式伺服器執行目標事務的過程中,依次獲取所述應用程式伺服器執行到的所述目標事務中的資料庫操作指令;對所述資料庫操作指令進行預測執行,將預測執行結果返回給所述應用程式伺服器以供所述應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄所述資料庫操作指令以及在所述資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄所述預測執行產生的預測執行資料;當獲取到所述目標事務中的事務提交指令時,根據本地記錄的所述資料庫操作指令以及所述預測執行資料,控制所述應用程式伺服器對應的資料庫實際執行所述目標事務。 In one aspect of the present invention, a method for operating a database is provided, including: during the process of the application server executing the target transaction, sequentially obtaining the database operation instructions in the target transaction executed by the application server; The database operation instruction performs predictive execution, returns the predicted execution result to the application server for the application server to determine the next database operation instruction to be executed, and records the database operation locally instruction and record the predicted execution data generated by the predictive execution when the database operation instruction belongs to one of the modified database type instruction and the locked non-modified database type instruction; when the transaction in the target transaction is obtained When submitting an instruction, control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instruction and the predicted execution data.

本發明的另一方面,提供一種資料庫操作方法,包括:在應用程式伺服器執行目標事務的過程中,依次獲取 所述應用程式伺服器執行到的所述目標事務中的資料庫操作指令;對所述資料庫操作指令進行預測執行,將預測執行結果返回給所述應用程式伺服器以供所述應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄所述資料庫操作指令以及在所述資料庫操作指令屬於被加鎖的非修改資料庫類指令時記錄所述預測執行產生的預測執行資料;當獲取到所述目標事務中的事務提交指令時,根據本地記錄的所述資料庫操作指令以及所述預測執行資料,控制所述應用程式伺服器對應的資料庫實際執行所述目標事務。 Another aspect of the present invention provides a method for operating a database, including: during the process of executing a target transaction by an application server, sequentially acquiring database operation instructions in the target transaction executed by the application server; Perform predictive execution on the database operation command, return the predictive execution result to the application program server for the application program server to determine the next database operation command to be executed, and record the database locally Operating instructions and recording the predicted execution data generated by the predicted execution when the database operation instruction belongs to the locked non-modified database instruction; when the transaction commit instruction in the target transaction is obtained, according to the local record The database operation instruction and the forecast execution data control the database corresponding to the application server to actually execute the target transaction.

本發明的又一方面,提供一種資料庫操作裝置,包括:獲取模組,用於在應用程式伺服器執行目標事務的過程中,依次獲取所述應用程式伺服器執行到的所述目標事務中的資料庫操作指令;預測執行模組,用於對所述資料庫操作指令進行預測執行,將預測執行結果返回給所述應用程式伺服器以供所述應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄所述資料庫操作指令以及在所述資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄所述預測執行產生的預測執行資料; 控制執行模組,用於當獲取到所述目標事務中的事務提交指令時,根據本地記錄的所述資料庫操作指令以及所述預測執行資料,控制所述應用程式伺服器對應的資料庫實際執行所述目標事務。 Still another aspect of the present invention provides a database operation device, including: an acquisition module, used to sequentially acquire the target transaction executed by the application server during the process of executing the target transaction by the application server The database operation instruction; the predictive execution module is used to perform predictive execution on the database operation instruction, and return the predictive execution result to the application program server for the application program server to determine the next one that needs to be executed Database operation instructions, and record the database operation instructions locally and record the results generated by the speculative execution when the database operation instructions belong to one of the modified database instructions and the locked non-modified database instructions predictive execution data; a control execution module, configured to control the application server corresponding to the The database actually executes the target transaction.

本發明的又一方面,提供一種資料庫操作裝置,包括:獲取模組,用於在應用程式伺服器執行目標事務的過程中,依次獲取所述應用程式伺服器執行到的所述目標事務中的資料庫操作指令;預測執行模組,用於對所述資料庫操作指令進行預測執行,將預測執行結果返回給所述應用程式伺服器以供所述應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄所述資料庫操作指令以及在所述資料庫操作指令屬於被加鎖的非修改資料庫類指令時記錄所述預測執行產生的預測執行資料;控制執行模組,用於當獲取到所述目標事務中的事務提交指令時,根據本地記錄的所述資料庫操作指令以及所述預測執行資料,控制所述應用程式伺服器對應的資料庫實際執行所述目標事務。 Still another aspect of the present invention provides a database operation device, including: an acquisition module, used to sequentially acquire the target transaction executed by the application server during the process of executing the target transaction by the application server The database operation instruction; the predictive execution module is used to perform predictive execution on the database operation instruction, and return the predictive execution result to the application program server for the application program server to determine the next one that needs to be executed Database operation instruction, and record the database operation instruction locally and record the prediction execution data generated by the prediction execution when the database operation instruction belongs to the locked non-modified database instruction; control the execution module , used to control the database corresponding to the application server to actually execute the target according to the locally recorded database operation command and the predicted execution data when the transaction commit command in the target transaction is obtained affairs.

在本發明中,資料庫操作裝置與應用程式伺服器相互配合,在應用程式伺服器執行目標事務的過程加入預測執行過程,預先獲得並記錄目標事務需要執行的全部資料庫操作指令,並僅針對屬於修改資料庫類指令和被加鎖的非修改資料庫類指令的資料庫操作指令,或者僅針對屬於被 加鎖的非修改資料庫類指令的資料庫操作指令記錄預測執行產生的預測執行資料,以適應於讀已提交隔離級別下的加鎖方案,為真實執行事務提供了條件,之後根據記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行該目標事務,有利於提高執行效率,進而增大事務吞吐量。 In the present invention, the database operation device and the application server cooperate with each other, adding a predictive execution process in the process of executing the target transaction on the application server, obtaining and recording in advance all the database operation instructions that the target transaction needs to execute, and only for Database operation instructions that belong to the database operation instructions that belong to the modified database type instructions and the locked non-modified database type instructions, or only record the speculative execution data generated by the speculative execution for the database operation instructions that belong to the locked non-modified database type instructions , to adapt to the locking scheme under the read-committed isolation level, providing conditions for the actual execution of transactions, and then controlling the database corresponding to the application server to actually execute the target transaction according to the recorded database operation instructions and predicted execution data , which is conducive to improving execution efficiency and thus increasing transaction throughput.

10‧‧‧應用程式伺服器 10‧‧‧Application server

20‧‧‧資料庫 20‧‧‧database

30‧‧‧資料庫操作裝置 30‧‧‧Database operation device

51‧‧‧獲取模組 51‧‧‧Acquiring modules

52‧‧‧預測執行模組 52‧‧‧Forecast Execution Module

53‧‧‧控制執行模組 53‧‧‧Control Execution Module

61‧‧‧獲取模組 61‧‧‧Acquiring modules

62‧‧‧預測執行模組 62‧‧‧Forecast Execution Module

63‧‧‧控制執行模組 63‧‧‧Control Execution Module

為了更清楚地說明本發明實施例中的技術方案,下面將對實施例或現有技術描述中所需要使用的圖式作一簡單地介紹,顯而易見地,下面描述中的圖式是本發明的一些實施例,對於本領域普通技術人員來講,在不付出創造性勞動性的前提下,還可以根據這些圖式獲得其他的圖式。 In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings that need to be used in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following descriptions are some of the present invention. Embodiments, for those of ordinary skill in the art, other drawings can also be obtained based on these drawings without paying creative labor.

圖1為現有技術中資料庫應用系統的部署圖;圖2為本發明一實施例提供的資料庫應用系統的部署圖;圖3為本發明另一實施例提供的資料庫操作方法的流程示意圖;圖4為本發明又一實施例提供的資料庫操作方法的流程示意圖;圖5為本發明又一實施例提供的資料庫操作裝置的結構示意圖;圖6為本發明又一實施例提供的資料庫操作裝置的結構示意圖。 Fig. 1 is a deployment diagram of a database application system in the prior art; Fig. 2 is a deployment diagram of a database application system provided by an embodiment of the present invention; Fig. 3 is a schematic flowchart of a database operation method provided by another embodiment of the present invention ; Fig. 4 is a schematic flow chart of a database operation method provided by another embodiment of the present invention; Fig. 5 is a schematic structural diagram of a database operation device provided by another embodiment of the present invention; Fig. 6 is provided by another embodiment of the present invention Schematic diagram of the structure of the database operation device.

為使本發明實施例的目的、技術方案和優點更加清楚,下面將結合本發明實施例中的圖式,對本發明實施例中的技術方案進行清楚、完整地描述,顯然,所描述的實施例是本發明一部分實施例,而不是全部的實施例。基於本發明中的實施例,本領域普通技術人員在沒有作出創造性勞動前提下所獲得的所有其他實施例,都屬於本發明保護的範圍。 In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments It is a part of embodiments of the present invention, but not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by persons of ordinary skill in the art without creative efforts fall within the protection scope of the present invention.

圖1為現有技術中資料庫應用系統的部署圖。如圖1所示,該系統包括:應用程式伺服器10和資料庫20。其中,應用程式伺服器10可以為一個,也可以為多個。資料庫20可以為一個,也可以為多個。圖1中僅示例性的示出一個應用程式伺服器10和一個資料庫20。當需要以事務方式對資料庫20進行存取時,應用程式伺服器10執行事務邏輯,依次將事務中的資料庫操作指令作用於資料庫20上。由於無法預先獲知事務中需要執行的資料庫操作指令,導致事務執行效率較低,尤其是在配置讀已提交隔離級別下透過for update語句對資料進行加鎖的方案中,其執行效率會更低,事務吞吐量較低。 FIG. 1 is a deployment diagram of a database application system in the prior art. As shown in FIG. 1 , the system includes: an application server 10 and a database 20 . Wherein, there may be one application server 10 or multiple application servers. There can be one database 20, or multiple databases. FIG. 1 only shows an application server 10 and a database 20 as examples. When it is necessary to access the database 20 in a transactional manner, the application server 10 executes the transactional logic, and sequentially applies the database operation commands in the transaction to the database 20 . Since the database operation instructions that need to be executed in the transaction cannot be known in advance, the execution efficiency of the transaction is low, especially in the scheme that locks the data through the for update statement under the configured read committed isolation level, the execution efficiency will be lower , the transaction throughput is low.

針對現有技術存在的缺陷,本發明提供一種新的資料庫應用系統,如圖2所示。該系統在應用程式伺服器10和資料庫20之間增加資料庫操作裝置30。資料庫操作裝置30用以執行本發明提供的資料庫操作方法,在讀已提 交這一隔離級別下的加鎖方案中實現一種新的事務執行邏輯,即先對事務進行預測執行,提前獲得該事務中需要執行的資料庫操作指令,實現執行路徑的預測,並且結合讀已提交這一隔離級別且加鎖的情況,僅僅針對屬於修改資料庫類指令和被加鎖的非修改資料庫類指令的資料庫操作指令,或者僅僅針對屬於被加鎖的非修改資料庫類指令的資料庫操作指令記錄對其進行預測執行所產生的預測執行資料,進而基於預測執行得到的資料庫操作指令以及預測執行資料在資料庫20上實際執行事務,有利於提高事務執行效率,增大事務吞吐量。事務吞吐量是指單位時間內處理事務的數量。 Aiming at the defects existing in the prior art, the present invention provides a new database application system, as shown in FIG. 2 . In this system, a database operation device 30 is added between the application server 10 and the database 20 . The database operation device 30 is used to execute the database operation method provided by the present invention, and implement a new transaction execution logic in the locking scheme under the read-committed isolation level, that is, perform predictive execution on the transaction first, and obtain the transaction in advance The database operation instructions that need to be executed to realize the prediction of the execution path, and combined with the read submitted isolation level and locked, only for the instructions that belong to the modified database class and the locked non-modified database class instructions Database operation instructions, or only for the database operation instructions that belong to the locked non-modified database instructions, record the predicted execution data generated by predictive execution, and then obtain the database operation instructions and predictive execution based on the predicted execution The data actually executes the transaction on the database 20, which is beneficial to improve the transaction execution efficiency and increase the transaction throughput. Transaction throughput refers to the number of transactions processed per unit time.

值得說明的是,資料庫操作裝置30實際上是一種邏輯處理裝置,其可以單獨部署,位於應用程式伺服器10和資料庫20之間,也可以部署于應用程式伺服器10一端實現,或者也可以部署於資料庫20一端實現。 It should be noted that the database operation device 30 is actually a logic processing device, which can be deployed independently, located between the application program server 10 and the database 20, or can be deployed at the end of the application program server 10 for implementation, or also It can be deployed at one end of the database 20 for implementation.

下面實施例將具體說明本發明技術方案的方法流程。 The following examples will specifically illustrate the method flow of the technical solution of the present invention.

圖3為本發明另一實施例提供的資料庫操作方法的流程示意圖。如圖3所示,該方法包括: FIG. 3 is a schematic flowchart of a database operation method provided by another embodiment of the present invention. As shown in Figure 3, the method includes:

301、在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令。 301. During the execution of the target transaction by the application server, sequentially acquire database operation instructions in the target transaction executed by the application server.

302、對資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器以供應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄資料庫操作 指令以及在資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄預測執行產生的預測執行資料。 302. Predictively execute the database operation command, return the predictive execution result to the application server for the application server to determine the next database operation command to be executed, and record the database operation command locally and store it in the database When the operation instruction belongs to one of the modified database instruction and the locked non-modified database instruction, the predicted execution data generated by the predicted execution is recorded.

303、當獲取到目標事務中的事務提交指令時,根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 303. When the transaction submission instruction in the target transaction is obtained, control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instruction and predicted execution data.

首先說明,為便於描述,本實施例將應用程式伺服器需要執行的事務稱為目標事務。目標事務主要包括用於對資料庫進行操作的操作指令,除資料庫操作指令之外還包括一些用於控制目標事務執行狀態的控制指令,例如事務開始指令、事務提交指令、事務轉返(rollback)指令等。這些指令實際上是由資料庫語言編寫的語句。根據資料庫語言的不同,這些指令可以是不同語言編寫的語句。舉例說明,若採用SQL,則上述資料庫操作指令和控制指令實際上是一系列的SQL語句。 Firstly, for the convenience of description, in this embodiment, the transaction to be executed by the application server is referred to as the target transaction. The target transaction mainly includes operation instructions for operating the database. In addition to the database operation instructions, it also includes some control instructions for controlling the execution status of the target transaction, such as transaction start instructions, transaction commit instructions, and transaction rollbacks. ) instructions, etc. These instructions are actually statements written in the language of the database. Depending on the language of the database, these instructions can be statements written in different languages. For example, if SQL is adopted, the above-mentioned database operation instructions and control instructions are actually a series of SQL statements.

另外,在一些應用場景中,事務的處理需要先選中一些記錄,再對這些記錄進行處理,因此需要對選擇的記錄加排他鎖。這意味著,本實施例中的資料庫操作指令可能是加鎖的資料庫操作指令。例如,假設從Bob的帳戶裡轉出100元,這個操作由三步完成,查詢Bob的帳戶,判斷該帳戶是否夠100元,將Bob的帳戶裡的錢減少100塊。如果有兩個轉帳操作並行進行,當第一個轉帳操作查出Bob的帳戶的餘額,發現有100塊,如果這時候有另外一個轉帳操作也去查了Bob的帳戶,也發現Bob的帳戶裡 有100塊,於是這兩個轉帳操作都會進行轉帳,於是,第一個操作從Bob的帳戶減去100塊錢,另一個操作也從Bob的帳戶減去100塊錢,這時候,Bob的帳戶餘額就可能是負數。為了避免這種情況,需要使用for update來對Bob的帳戶加鎖,保證在第一個轉帳操作進行過程中,第二個轉帳操作無法獲得這個鎖,也就無法進行查詢。 In addition, in some application scenarios, transaction processing needs to select some records first, and then process these records, so it is necessary to add an exclusive lock to the selected records. This means that the database operation command in this embodiment may be a locked database operation command. For example, assuming that 100 yuan is transferred from Bob's account, this operation is completed in three steps, query Bob's account, determine whether the account is enough for 100 yuan, and reduce the money in Bob's account by 100 yuan. If there are two transfer operations in parallel, when the first transfer operation finds out the balance of Bob's account, it is found that there is 100 yuan. If there is another transfer operation at this time, it also checks Bob's account, and it is also found in Bob's account There are 100 yuan, so the two transfer operations will transfer money, so the first operation subtracts 100 yuan from Bob's account, and the other operation also subtracts 100 yuan from Bob's account. At this time, Bob's account The balance may be negative. In order to avoid this situation, you need to use for update to lock Bob's account to ensure that during the first transfer operation, the second transfer operation cannot obtain this lock, and thus cannot be queried.

本實施例提供的方法適用於對隔離級別為讀已提交的資料庫進行操作。其中,有些資料庫內定是讀已提交這一隔離級別,例如,Oracle資料庫內定是讀已提交這一隔離級別的,有些資料庫內定不是讀已提交這一隔離級別,例如MySQL資料庫內定是可重複讀這一隔離級別。基於此,在執行本實施例提供的方法之前,可以確定資料庫的隔離級別是否是讀已提交,若不是,則需要預先配置資料庫為讀已提交這一隔離級別。例如,可以在應用程式伺服器執行目標事務之前,配置資料庫為讀已提交這一隔離級別。 The method provided in this embodiment is suitable for operating the database whose isolation level is read committed. Among them, some databases default to the read-committed isolation level. For example, the Oracle database defaults to the read-committed isolation level. Some databases default to the read-committed isolation level. For example, the MySQL database defaults to Repeatable read is the isolation level. Based on this, before executing the method provided by this embodiment, it can be determined whether the isolation level of the database is read committed, and if not, the isolation level of the database needs to be preconfigured as read committed. For example, you can configure the database to read-committed isolation level before the application server executes the target transaction.

在資料庫的隔離級別為讀已提交的前提下,可以執行本實施例提供的方法以在資料庫中執行目標事務。具體流程如下:應用程式伺服器控制整個目標事務的執行邏輯,可以按照現有方式執行目標事務。資料庫操作裝置在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令。 On the premise that the isolation level of the database is read committed, the method provided by this embodiment can be executed to execute the target transaction in the database. The specific process is as follows: the application server controls the execution logic of the entire target transaction, and can execute the target transaction in an existing manner. The database operation device sequentially obtains the database operation instructions in the target transaction executed by the application server during the execution of the target transaction by the application server.

在一可選實施方式中,應用程式伺服器按照現有方式 向資料庫發送資料庫操作指令,以實現對資料庫的存取。資料庫操作裝置可以對應用程式伺服器與資料庫之間的通信進行監控,以攔截應用程式伺服器發往資料庫的資料庫操作指令。 In an optional implementation manner, the application program server sends database operation instructions to the database in an existing manner, so as to realize access to the database. The database operation device can monitor the communication between the application program server and the database, so as to intercept the database operation commands sent by the application program server to the database.

在另一可選實施方式中,對應用程式伺服器的處理邏輯進行稍微修改,將原來向資料庫發送資料庫操作指令的處理邏輯改為向資料庫操作裝置發送。基於此,資料庫操作裝置可以接收應用程式伺服器主動下發的資料庫操作指令。 In another optional implementation manner, the processing logic of the application server is slightly modified, and the original processing logic of sending database operation instructions to the database is changed to sending to the database operation device. Based on this, the database operation device can receive the database operation instruction issued by the application server.

在此說明,若目標事務是顯式事務,則目標事務中的首個指令是事務開始指令,如開始事務(begin transaction),基於此,資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令之前,可以攔截應用程式伺服器發往資料庫的目標事務中的事務開始指令,根據該事務開始指令,獲知需要執行目標事務。或者,資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令之前,可以接收應用程式伺服器主動下發的目標事務中的事務開始指令,根據該事務開始指令,獲知需要執行目標事務。若目標事務是隱式事務,則目標事務的首個指令就是資料庫操作指令,不包括獲取事務開始指令的步驟。 It is explained here that if the target transaction is an explicit transaction, the first command in the target transaction is a transaction start command, such as begin transaction. Based on this, the database operation device obtains the data executed by the application server Before the library operation command, the transaction start command in the target transaction sent by the application server to the database can be intercepted, and according to the transaction start command, it is known that the target transaction needs to be executed. Alternatively, before the database operation device obtains the database operation command executed by the application server, it may receive the transaction start command in the target transaction actively issued by the application server, and learn that the target transaction needs to be executed according to the transaction start command. . If the target transaction is an implicit transaction, the first command of the target transaction is the database operation command, excluding the step of obtaining the transaction start command.

資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令後,對獲取到的資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器,以供應用程式伺服器確定下一個需要執行的資料庫操作指令。其中, 預測執行結果決定著目標事務的執行路徑,這裡的執行路徑是指資料庫操作指令之間的跳轉邏輯。透過將預測執行結果返回給應用程式伺服器,從而達到由應用程式伺服器控制整個目標事務的執行邏輯的目的。 After the database operation device obtains the database operation instructions executed by the application program server, it predictively executes the obtained database operation instructions, and returns the prediction execution results to the application program server for the application program server to determine A database operation command to execute. Wherein, the predicted execution result determines the execution path of the target transaction, where the execution path refers to the jump logic between database operation instructions. By returning the predictive execution result to the application server, the application server controls the execution logic of the entire target transaction.

另外,資料庫操作裝置還需要在本地記錄所獲取到的資料庫操作指令,以及在該資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時,記錄對該資料庫操作指令進行預測執行產生的預測執行資料。所述修改資料庫類指令是指對資料庫有修改的資料庫操作指令,例如以update、insert等開頭的指令;一般以select開頭的指令不屬於修改資料庫類指令(稱為非修改資料庫類指令)。雖然從本質而言,讀已提交隔離級別只保證寫入多個資料是原子的,不在讀取的時候加鎖,但為了保證事務的一致性,降低轉返概率,有些非修改資料庫類指令可能需要透過for update語句進行加鎖,例如在上面轉帳舉例中,select指令需要加鎖,形成select......forupdate形式的資料庫操作指令,這種指令可稱為被加鎖的非修改資料庫類指令。所述預測執行資料主要是指對資料庫操作指令進行預測執行過程中的一些資料,例如資料庫操作指令所操作的資料以及資料的主鍵ID和主鍵ID對應的版本號資訊(version)等。 In addition, the database operation device also needs to record the obtained database operation instruction locally, and when the database operation instruction belongs to one of the database modification instruction and the locked non-modification database instruction, the record is correct. The speculative execution data generated by the speculative execution of the database operation instruction. Described modification database class instruction refers to the database operation instruction that database has modification, such as the instruction beginning with update, insert etc.; Generally the instruction beginning with select does not belong to modification database class instruction (referred to as non-modification database class instructions). Although in essence, the read-committed isolation level only guarantees that writing multiple data is atomic and does not lock when reading, but in order to ensure transaction consistency and reduce the probability of return, some non-modifying database instructions It may be necessary to lock through the for update statement. For example, in the above account transfer example, the select command needs to be locked to form a database operation command in the form of select...forupdate. This kind of command can be called a locked non- Modify the database class directive. The predictive execution data mainly refers to some data during the predictive execution process of the database operation instruction, such as the data operated by the database operation instruction, the primary key ID of the data and the version number information (version) corresponding to the primary key ID, etc.

值得說明的是,對於同一資料庫操作指令來說,要麼屬於修改資料庫類指令,要麼屬於被加鎖的非修改資料庫類指令,要麼屬於未加鎖的非修改資料庫類指令。但是, 從整個目標事務來看,對於該目標事務中所有修改資料庫類指令以及所有被加鎖的非修改資料庫類指令預測執行產生的預測執行資料都會被記錄在本地。 It is worth noting that, for the same database operation command, either it belongs to the database modification command, or it belongs to the locked non-modification database command, or it belongs to the unlocked non-modification database command. However, from the point of view of the entire target transaction, the predictive execution data generated by the speculative execution of all database-modifying instructions and all locked non-modifying database-type instructions in the target transaction will be recorded locally.

例如,資料庫操作裝置可以在本地創建記憶體庫,將所獲取到的資料庫操作指令以及預測執行資料存入記憶體庫中。進一步,若預測執行資料不包括預測執行結果,則也可以將預測執行結果存入記憶體庫中。 For example, the database operating device can create a memory bank locally, and store the obtained database operation instructions and predicted execution data into the memory bank. Further, if the predictive execution data does not include the predictive execution result, the predictive execution result may also be stored in the memory bank.

為了在預測執行這一核心思想的基礎上實現對讀已提交這一隔離級別下的加鎖方案的模擬,本實施例針對屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一的資料庫操作指令,記錄其預測執行產生的預測執行資料,卻不記錄屬於未被加鎖的非修改資料庫類指令的資料庫操作指令對應的預測執行資料。由此可見,在實現讀已提交這一隔離級別下的加鎖方案的情況下,所記錄的資料量相對較少,這樣在後續基於本地記錄的資料庫操作指令和預測執行資料在資料庫中真實執行目標事務的過程中,所需處理的資料量相對較少,有利於進一步提高執行效率和性能,增大事務吞吐量。 In order to realize the simulation of the locking scheme under the read-committed isolation level on the basis of the core idea of predictive execution, this embodiment is aimed at the instructions belonging to the modified database class and the locked non-modified database class instructions. One of the database operation instructions records the speculative execution data generated by its speculative execution, but does not record the speculative execution data corresponding to the database operation instructions that belong to the unlocked non-modifying database instructions. It can be seen that in the case of implementing the locking scheme under the isolation level of read committed, the amount of recorded data is relatively small, so that the subsequent database operation instructions and predicted execution data based on local records will be stored in the database. In the process of actually executing the target transaction, the amount of data to be processed is relatively small, which is conducive to further improving execution efficiency and performance, and increasing transaction throughput.

其中,資料庫操作指令對資料庫的操作主要是存取資料庫中的資料,則可以模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行。進一步,考慮到屬於非修改資料庫類指令的資料庫操作指令不會引起資料庫的變化,因此可以直接在資料庫中對該資料庫操作指令進行預測執行,這樣可以節約模擬資料環 境的操作,實現相對簡單,有利於節約資源,提高執行效率。但是,對於屬於修改資料庫類指令的資料庫操作指令,由於其會引起資料庫的變化(主要是指資料庫中的資料被改變),因此需要模擬資料環境,在所模擬的資料環境中進行預測執行。 Wherein, the operation of the database operation command on the database is mainly to access the data in the database, so the data environment of the database operation command can be simulated, and the database operation command can be predicted and executed based on the simulated data environment. Further, considering that the database operation instructions belonging to non-modifying database instructions will not cause changes in the database, so the database operation instructions can be directly predicted and executed in the database, which can save the operation of the simulated data environment, The implementation is relatively simple, which is conducive to saving resources and improving execution efficiency. However, as for the database operation command belonging to the database modification command, because it will cause the change of the database (mainly refers to the change of the data in the database), it is necessary to simulate the data environment and perform the operation in the simulated data environment. Predictive execution.

基於上述分析,一種對資料庫操作指令進行預測執行的方式可以是:對獲取到的資料庫操作指令的類別進行識別;若資料庫操作指令屬於修改資料庫類指令,則在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行;若資料庫操作指令屬於非修改資料庫類指令,在資料庫中執行該資料庫操作指令,以對該資料庫操作指令進行預測執行。 Based on the above analysis, a method of predictive execution of database operation instructions can be: identify the type of the acquired database operation instructions; The database simulates the data environment of the database operation command in the database, and predicts and executes the database operation command based on the simulated data environment; if the database operation command is a non-modified database command, execute the database operation command in the database, to perform speculative execution on the database operation instruction.

進一步,一種在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行的實施方式包括:將獲取到的資料庫操作指令拆分為一個讀指令和一個寫指令,將讀指令運行於真正的資料庫上,即在資料庫中執行該讀指令以獲取讀資料集(ReadSet),將讀資料集存入本地記憶體庫中,以模擬資料庫操作指令所需的資料環境。然後,將寫指令作用於記憶體庫上,以實現對資料庫操作指令的預測執行,即在記憶體庫中執行寫指令以修改讀資料集,例如對讀資料集中的有關資料進行更新或查 詢處理等。其中,執行寫指令以修改讀資料集可以產生結果資料集(affectRowInMemdb),該結果資料集包括預測執行結果。 Further, a data environment for simulating database operation instructions in a locally created memory bank, the implementation of predictive execution of database operation instructions based on the simulated data environment includes: splitting the obtained database operation instructions into A read command and a write command, run the read command on the real database, that is, execute the read command in the database to obtain the read data set (ReadSet), store the read data set in the local memory bank, and Simulate the data environment required by the database operation command. Then, the write command acts on the memory bank to realize the predictive execution of the database operation command, that is, execute the write command in the memory bank to modify the read data set, such as updating or querying the relevant data in the read data set processing etc. Wherein, executing the write command to modify the read data set can generate a result data set (affectRowInMemdb), and the result data set includes the predicted execution result.

在上述預測執行過程中,若資料庫操作指令屬於修改資料庫類指令,則資料庫操作裝置可以在本地記錄讀指令所讀取的讀資料集以及執行寫指令產生的結果資料集。由此可見,所述預測執行資料可以包括讀資料集和結果資料集,或者也可以包括讀資料集和結果資料集中的部分資料,例如能夠對在資料庫中實際執行目標事務產生有益效果的一些資料,例如一些數值的各種索引,例如一級索引、二級索引、主鍵ID、版本號等。 In the above-mentioned predictive execution process, if the database operation instruction belongs to the database modification instruction, the database operation device can locally record the read data set read by the read command and the result data set generated by executing the write command. It can be seen that the predicted execution data may include the read data set and the result data set, or may also include part of the data in the read data set and the result data set, for example, some data that can have a beneficial effect on the actual execution of the target transaction in the database. Data, such as various indexes of some values, such as primary index, secondary index, primary key ID, version number, etc.

較優的,為了進一步減少記錄的資料量,在資料庫操作指令屬於被加鎖的非修改資料庫類指令時,可以僅記錄該資料庫操作指令所操作的資料的主鍵ID和版本號作為預測執行資料。這樣在後續基於本地記錄的資料庫操作指令和預測執行資料在資料庫中真實執行目標事務的過程中,所需處理的資料量會進一步較少,有利於進一步提高執行效率和性能,增大事務吞吐量。另外,有時資料在事務過程中可能會經過多次修改,但最終又回到初始值,但資料的主鍵ID和版本號不會發生變化。因此,若採用記錄資料本身的方式,在預測執行與真實執行進行比較時資料可能不一樣,就要發生轉返,而轉返的代價較大。但是,本實施例採用只記錄資料的主鍵ID和版本號的方式,在預測執行與真實執行進行比較時出現不一致的概率 較低,轉返概率較低。 Preferably, in order to further reduce the amount of recorded data, when the database operation instruction is a locked non-modified database instruction, only the primary key ID and version number of the data operated by the database operation instruction can be recorded as a prediction Executive data. In this way, in the process of actually executing the target transaction in the database based on the locally recorded database operation instructions and predicted execution data, the amount of data to be processed will be further reduced, which is conducive to further improving execution efficiency and performance, and increasing the number of transactions. throughput. In addition, sometimes the data may be modified many times during the transaction process, but eventually returns to the initial value, but the primary key ID and version number of the data will not change. Therefore, if the method of recording the data itself is used, the data may be different when the predicted execution is compared with the actual execution, and a reversal will occur, and the cost of the reversal is relatively high. However, this embodiment adopts the method of only recording the primary key ID and version number of the data, and the probability of inconsistency is low when the predicted execution is compared with the actual execution, and the probability of return is low.

當資料庫操作裝置獲取到目標事務中的事務提交指令(即應用程式伺服器執行到事務提交指令)時,則可以根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 When the database operation device obtains the transaction submission command in the target transaction (that is, the application program server executes the transaction submission command), it can control the corresponding application program server according to the locally recorded database operation command and predicted execution data. The database actually executes the target transaction.

上述根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務的一種可選實施方式包括:資料庫操作裝置向資料庫下發本地記錄的所述資料庫操作指令,以指示資料庫執行資料庫操作指令,並接收資料庫返回的資料庫操作指令的實際執行結果;然後,將資料庫操作指令的實際執行結果與預測執行結果進行比較,若實際執行結果與預測執行結果相同,向資料庫下發事務提交指令,以供資料庫提交該目標事務;若實際執行結果與預測執行結果不相同,向資料庫下發事務轉返指令,以供資料庫轉返該目標事務。 An optional implementation manner of controlling the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instructions and predicted execution data includes: the database operation device sends the locally recorded data to the database library operation instruction to instruct the database to execute the database operation instruction, and receive the actual execution result of the database operation instruction returned by the database; then, compare the actual execution result of the database operation instruction with the predicted execution result, if the actual execution If the result is the same as the predicted execution result, issue a transaction commit command to the database for the database to submit the target transaction; if the actual execution result is different from the predicted execution result, issue a transaction return command to the database for the database Forward the target transaction.

在上述實施方式中,考慮到事務的原子性,透過將實際執行結果與預測執行結果進行比較,可以避免事務執行過程出錯,有利於提高事務執行的成功概率。 In the above embodiments, considering the atomicity of the transaction, by comparing the actual execution result with the predicted execution result, errors in the transaction execution process can be avoided, which is beneficial to improve the success probability of the transaction execution.

進一步,在現有技術中,由於無法預先知道事務需要執行的全部資料庫操作指令,所以只能按照事務的執行邏輯依次執行資料庫操作指令,這會導致應用程式伺服器與資料庫之間的頻繁互動,這在遠程應用場景中將會消耗大量網路資源。但在本實施例中,透過預測執行過程已經預 先獲得目標事務需要執行的全部資料庫操作指令,即本地記錄的資料庫操作指令,因此在向資料庫下發本地記錄的資料庫操作指令,以指示資料庫執行資料庫操作指令的過程中,可以將本地記錄的資料庫操作指令同時(或者一併)下發到資料庫,有利於節約網路資源。另外,對於一些並行的資料庫操作指令,資料庫還可以並行操作,有利於進一步提高事務執行效率,增大事務吞吐量。 Furthermore, in the prior art, since it is impossible to know in advance all the database operation instructions that the transaction needs to execute, the database operation instructions can only be executed sequentially according to the execution logic of the transaction, which will lead to frequent interaction between the application server and the database , which will consume a lot of network resources in remote application scenarios. However, in this embodiment, all database operation instructions that need to be executed by the target transaction, that is, locally recorded database operation instructions, have been obtained in advance through the predictive execution process, so the locally recorded database operation instructions are sent to the database to During the process of instructing the database to execute the database operation instruction, the database operation instructions recorded locally can be sent to the database at the same time (or together), which is beneficial to save network resources. In addition, for some parallel database operation instructions, the database can also be operated in parallel, which is conducive to further improving transaction execution efficiency and increasing transaction throughput.

進一步,還可以將本地記錄的所有資料庫操作指令連同本地記錄的預測執行資料一併下發給資料庫。 Furthermore, all the database operation instructions recorded locally together with the predicted execution data recorded locally can also be issued to the database.

進一步,在一種根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務的可選實施方式中,資料庫操作裝置可以根據本地記錄的資料庫操作指令,判斷目標事務是否為單機事務;若判斷結果為是,則根據本地記錄的資料庫操作指令以及預測執行資料,以單機事務處理邏輯控制資料庫實際執行目標事務;若判斷結果為否,則根據本地記錄的資料庫操作指令以及預測執行資料,以分布式事務處理邏輯控制資料庫實際執行目標事務。 Further, in an optional implementation mode in which the database corresponding to the application server is controlled to actually execute the target transaction according to the locally recorded database operation instruction and the predicted execution data, the database operation device may use the locally recorded database operation instruction , to judge whether the target transaction is a stand-alone transaction; if the judgment result is yes, then according to the locally recorded database operation instructions and predicted execution data, the stand-alone transaction processing logic is used to control the database to actually execute the target transaction; if the judgment result is no, then according to The locally recorded database operation instructions and forecast execution data control the database to actually execute the target transaction with the distributed transaction processing logic.

其中,基於預測執行過程中獲得的資料庫操作指令可以識別出目標事務是單機事務還是分布式事務。例如,可以判斷本地記錄的資料庫操作指令的操作對象是否作用於同一實體設備上,若判斷結果為是,則可以確定該目標事務為單機事務;若判斷結果為否,則可以確定該目標事務為分布式事務。 Wherein, it can be identified whether the target transaction is a stand-alone transaction or a distributed transaction based on the database operation instructions obtained during the predictive execution process. For example, it can be judged whether the operation object of the locally recorded database operation command acts on the same physical device, if the judgment result is yes, it can be determined that the target transaction is a stand-alone transaction; if the judgment result is no, it can be determined that the target transaction for distributed transactions.

由於分布式事務的處理邏輯和單機事務的處理邏輯有所不同,單機事務的處理邏輯相對簡單,例如不涉及讀寫加鎖等問題,所以所消耗的資源也相對較少。基於此,本實施例透過識別目標事務是否為單機事務,在識別為單機事務時採用單機事務的處理邏輯進行處理,而不是像現有技術那樣統一採用分布式事務處理邏輯進行處理,有利於提高處理效率,節約處理成本。 Because the processing logic of distributed transactions is different from that of stand-alone transactions, the processing logic of stand-alone transactions is relatively simple, for example, it does not involve issues such as read-write locks, so the resources consumed are relatively small. Based on this, in this embodiment, by identifying whether the target transaction is a stand-alone transaction, when it is identified as a stand-alone transaction, the processing logic of the stand-alone transaction is used for processing, instead of uniformly adopting the distributed transaction processing logic for processing as in the prior art, which is beneficial to improve the processing efficiency. efficiency and save processing costs.

在一可選實施方式中,若資料庫操作裝置獲取到目標事務中的事務轉返指令(即應用程式伺服器執行到事務轉返指令)時,可以刪除本地記錄的資料庫操作指令以及預測執行資料,例如可以直接清空本地記憶體庫,實現轉返操作。在該情況下,由於目標事務並未在資料庫中真正執行,所以資料庫無需執行轉返操作,可見,在事務轉返這種情況下,採用本實施例方法也可以提高事務執行效率。 In an optional implementation, if the database operation device obtains the transaction return command in the target transaction (that is, the application server executes the transaction return command), it can delete the locally recorded database operation command and predictive execution data, for example, the local memory bank can be cleared directly to realize the transfer operation. In this case, since the target transaction is not actually executed in the database, the database does not need to perform a rollback operation. It can be seen that in the case of transaction rollback, the method of this embodiment can also improve transaction execution efficiency.

由上述可見,在本實施例提供的方法中,資料庫操作裝置與應用程式伺服器相互配合,在應用程式伺服器執行目標事務的過程加入預測執行過程,在預測執行過程中,可以提前獲得目標事務的執行路徑,即真正需要執行的資料庫操作指令,並僅針對屬於修改資料庫類指令的資料庫操作指令,或者僅針對屬於被加鎖的非修改資料庫類指令的資料庫操作指令記錄預測執行產生的預測執行資料,以適應於讀已提交隔離級別下的加鎖方案,為真實執行事務提供了條件,之後在根據記錄的資料庫操作指令以及預測執行資料實際執行該目標事務時,例如基於預測執行資料 可以提前獲取有關資料資訊並且可以減少指令之間的跳轉等,因此可以提高執行效率,增大事務吞吐量。 It can be seen from the above that in the method provided by this embodiment, the database operation device and the application server cooperate with each other, and the predictive execution process is added to the process of executing the target transaction on the application server. During the predictive execution process, the target can be obtained in advance. The execution path of the transaction, that is, the database operation command that really needs to be executed, and only for the database operation command that belongs to the database modification command, or only for the database operation command record that belongs to the locked non-modification database command The predicted execution data generated by the predicted execution is adapted to the locking scheme under the read-committed isolation level, which provides conditions for the real execution of the transaction, and then when the target transaction is actually executed according to the recorded database operation instructions and the predicted execution data, For example, based on predictive execution data, relevant data information can be obtained in advance and jumps between instructions can be reduced, so execution efficiency can be improved and transaction throughput can be increased.

下面以減庫存事務為例,詳細說明本發明技術方案的工作流程。其中,資料庫的隔離級別為讀已提交這一隔離級別。 Taking the inventory reduction business as an example, the workflow of the technical solution of the present invention will be described in detail below. Among them, the isolation level of the database is the isolation level of read committed.

減庫存事務的SQL代碼如下,括號內的文字為注釋:begin transaction(開始事務)select * from inventory where itemId=?for update(用商品id查看當前庫存,注意這裡用for update外部顯示加鎖)if(item.inventory>0)item.inventory--;update inventory set item.inventory=$item.inventory where itemId=?commit;else rollback;(如果當前庫存大於0,則減少、更新商品庫存並提交,否則轉返) The SQL code for the inventory reduction transaction is as follows, and the text in brackets is a comment: begin transaction (start transaction) select * from inventory where itemId=? for update (use the product id to view the current inventory, note that the external display of for update is used here to lock) if (item.inventory>0) item.inventory--; update inventory set item.inventory=$item.inventory where itemId=? commit; else rollback; (if the current inventory is greater than 0, reduce and update the commodity inventory and submit, otherwise return)

在實際執行過程中,應用程式伺服器負責該減庫存事務的整個邏輯,資料庫操作裝置(或者也可稱為執行伺服器)只會獲取應用程式伺服器執行到的開始事務,需要執行的SQL語句,以及提交/轉返指令。 In the actual execution process, the application server is responsible for the entire logic of the inventory reduction transaction, and the database operation device (or it can also be called the execution server) only obtains the initial transaction executed by the application server, and the SQL to be executed statement, and commit/return instructions.

首先,開始事務後,資料庫操作裝置在本地創建記憶體庫;當資料庫操作裝置獲取到select * from inventory where itemId=?for update這條SQL語句時,識別出這條SQL語句不會修改資料庫,因此將這條SQL語句直接作用在實際資料庫中,即在資料庫中執行該條SQL語句以獲得查詢結果,即讀資料集,將全部查詢結果返回給應用程式伺服器,以供應用程式伺服器確定下一個需要執行的SQL語句,並在本地記憶體庫中記錄這條SQL語句。雖然是讀已隔離級別,但由於該語句被加鎖,故需要記錄執行該條SQL語句有關的資料,優選的可以僅記錄該SQL語句所查詢的資料的主鍵ID和版本號(version)。 First, after starting the transaction, the database operation device creates a memory bank locally; when the database operation device obtains select * from inventory where itemId=? When the SQL statement for update, it is recognized that this SQL statement will not modify the database, so this SQL statement is directly applied to the actual database, that is, the SQL statement is executed in the database to obtain the query result, that is Read the data set, and return all query results to the application program server, so that the application program server can determine the next SQL statement to be executed, and record this SQL statement in the local memory bank. Although it is the read isolation level, because the statement is locked, it is necessary to record the relevant data of executing the SQL statement. Preferably, only the primary key ID and the version number (version) of the data queried by the SQL statement can be recorded.

應用程式伺服器收到資料庫操作裝置返回的查詢結果後,判斷查詢結果中當前庫存量是否大於0,若判斷結果為是,則將當前庫存量減少,並執行更新語句,即update inventory set item.inventory=$item.inventory where itemId=?;若判斷結果為否,則執行rollback語句。 After the application server receives the query result returned by the database operation device, it judges whether the current inventory in the query result is greater than 0. If the judgment result is yes, it reduces the current inventory and executes the update statement, that is, update inventory set item .inventory=$item.inventory where itemId=? ; If the judgment result is no, execute the rollback statement.

第一種情況,假設應用程式伺服器執行了rollback語句,則資料庫操作裝置會獲取到rollback語句。一旦資料庫儲存裝置獲取到rollback語句,則清空本地記憶體庫,主要是指清空之前記錄的select * from inventory where itemId=?for update這條SQL語句。在這種情況下,因為事務並沒有在資料庫中執行,所以無需資料庫執行轉返操作。 In the first case, assuming that the application server executes the rollback statement, the database operation device will obtain the rollback statement. Once the database storage device obtains the rollback statement, clear the local memory bank, mainly referring to clearing the previously recorded select * from inventory where itemId=? for update this SQL statement. In this case, there is no need for the database to perform a rollback because the transaction is not executed in the database.

第二種情況,假設應用程式伺服器執行了update inventory set item.inventory=$item.inventory where itemId=?這條語句,則資料庫操作裝置會獲取到update inventory set item.inventory=$item.inventory where itemId=?這條語句。則資料庫操作裝置獲取到update inventory set item.inventory=?where itemId=?這條SQL語句時,識別出該SQL語句會修改資料庫,於是將這條SQL語句拆解為一個讀指令,即select * from inventory where itemId=?和一個寫指令,即update inventory set item.inventory=?where itemId=?;接著,將select * from inventory where itemId=?這個語句下發到資料庫中,獲得讀資料集,將讀資料集中的資料寫入本地記憶體庫中;其中,讀資料集中包括主鍵列表(itemId)和該主鍵對應的版本號(version)。然後,根據update inventory set item.inventory=?where itemId=?這個語句在本地記憶體庫中進行更新操作,以獲得結果資料集,並將結果資料集返回給應用程式伺服器。該結果資料集可以是類似修改了幾條記錄這樣的修改記錄資訊,例如,affectRow=1表示修改了一條記錄;或者,結果資料集也可以是記錄修改詳細資訊的修改詳情資訊。其中,結果資料集和讀資料集構成預測執行資料。 In the second case, suppose the application server executes update inventory set item.inventory=$item.inventory where itemId=? With this statement, the database operation device will obtain update inventory set item.inventory=$item.inventory where itemId=? this sentence. Then the database operation device obtains update inventory set item.inventory=? where itemId=? When this SQL statement is recognized, the SQL statement will modify the database, so this SQL statement is disassembled into a read command, that is, select * from inventory where itemId=? and a write command, update inventory set item.inventory=? where itemId=? ; Next, select * from inventory where itemId=? This statement is sent to the database to obtain the read data set, and write the data in the read data set to the local memory bank; wherein, the read data set includes the primary key list (itemId) and the version number (version) corresponding to the primary key. Then, according to update inventory set item.inventory=? where itemId=? This statement performs an update operation in local memory to obtain a result dataset and returns the result dataset to the application server. The result data set can be modification record information such as how many records have been modified, for example, affectRow=1 means that a record has been modified; or, the result data set can also be modification details information that records modification details. Among them, the result data set and the read data set constitute the forecast execution data.

應用程式伺服器此時執行commit指令。 The application server executes the commit command at this time.

當資料庫操作裝置獲取到commit指令後,基於本地記憶體庫中的SQL語句以及預測執行資料,在資料庫中執行該減庫存事務,這次事務執行過程是真實執行的。例如,本地記錄的SQL語句以及預測執行資料如下所示: {select * from inventory where itemId=?update inventory set item.inventory=?where itemId=?【readVersion:itemId=xxx and version=xxx /affectRow=1】commit()} After the database operating device obtains the commit command, it executes the inventory reduction transaction in the database based on the SQL statement in the local memory database and the predicted execution data. This transaction execution process is real. For example, the locally recorded SQL statements and predicted execution data are as follows: {select * from inventory where itemId=? update inventory set item.inventory=? where itemId=? [readVersion: itemId=xxx and version=xxx /affectRow=1] commit()}

優選的,資料庫操作裝置可以一次性將本地記憶體庫中的SQL語句全部提交到資料庫中,資料庫基於此進行執行,有利於節約傳輸SQL語句所消耗的網路資源。 Preferably, the database operating device can submit all the SQL statements in the local memory database to the database at one time, and the database executes based on this, which is beneficial to save network resources consumed by transmitting SQL statements.

這次真實執行也會返回執行結果,可以將預測執行結果與實際執行結果進行比較,例如可以先比較兩個結果中的主鍵ID和版本號,然後再比較兩個結果中的affectRow;如果比較結果為相同,則可以在資料庫中提交這次事務,而如果比較結果為不相同,則認為提交是失敗的,由於在這種情況下,事務本身其實一直都沒有真正的在資料庫中執行提交,所以可以簡單地轉返整個請求,讓應用程式伺服器重新提交即可。 This actual execution will also return the execution result. You can compare the predicted execution result with the actual execution result. For example, you can first compare the primary key ID and version number in the two results, and then compare the affectRow in the two results; if the comparison result is If they are the same, the transaction can be submitted in the database, and if the comparison result is not the same, the submission is considered to have failed. In this case, the transaction itself has not actually been submitted in the database, so The entire request can simply be redirected and the application server resubmitted.

其中,經過上述預測執行,已經獲得了所有的需要執行的SQL語句,以及相應的預測執行資料,例如全部切分條件,數值的索引、版本號等,而這些資料足以讓我們提前對事務是否是單機事務做出判斷。基於此,可以採用相應的事務處理邏輯進行處理,有利於節約資源。 Among them, after the above-mentioned predictive execution, we have obtained all the SQL statements that need to be executed, as well as the corresponding predictive execution data, such as all segmentation conditions, numerical indexes, version numbers, etc., and these data are enough for us to know in advance whether the transaction is Stand-alone transactions to make judgments. Based on this, corresponding transaction processing logic can be used for processing, which is beneficial to save resources.

圖4為本發明又一實施例提供的資料庫操作方法的流程示意圖。如圖4所示,該方法包括: FIG. 4 is a schematic flowchart of a database operation method provided by another embodiment of the present invention. As shown in Figure 4, the method includes:

401、在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令。 401. During the process of executing the target transaction by the application server, sequentially acquire database operation instructions in the target transaction executed by the application server.

402、對資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器以供應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄資料庫操作指令以及在資料庫操作指令屬於被加鎖的非修改資料庫類指令時記錄預測執行產生的預測執行資料。 402. Predictively execute the database operation command, return the predictive execution result to the application server so that the application server can determine the next database operation command to be executed, and record the database operation command locally and store it in the database Record the speculative execution data generated by speculative execution when the operation command belongs to the locked non-modifying database command.

403、當獲取到目標事務中的事務提交指令時,根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 403. When the transaction submission instruction in the target transaction is acquired, control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instruction and predicted execution data.

本實施例與圖3所示實施例相類似,區別在於:僅記錄目標事務中被加鎖的非修改資料庫類指令的預測執行產生的預測執行資料,對於修改資料庫類指令的預測執行產生的預測執行資料以及未加鎖的非修改資料庫類指令的預測執行產生的預測執行資料不作記錄。另外,本實施例適用於目標事務包括被加鎖的非修改資料庫類指令的情況,所操作的資料庫不僅可以是隔離級別為讀已提交的資料庫場景,也可以是其它各種資料庫場景。 This embodiment is similar to the embodiment shown in FIG. 3 , the difference is that only the predicted execution data generated by the predicted execution of the locked non-modified database type instruction in the target transaction is recorded, and the predicted execution data generated by the modified database type instruction is recorded. and the speculative execution data generated by the speculative execution of unlocked non-modified database instructions are not recorded. In addition, this embodiment is applicable to the situation where the target transaction includes locked non-modified database instructions, and the database to be operated can be not only the database scenario whose isolation level is read committed, but also various other database scenarios .

若將本實施例提供的方法應用於隔離級別為讀已提交的資料庫,則考慮到有些資料庫內定是讀已提交這一隔離級別,例如,Oracle資料庫內定是讀已提交這一隔離級別的,有些資料庫內定不是讀已提交這一隔離級別,例如MySQL資料庫內定是可重複讀這一隔離級別,於是在執 行本實施例提供的方法之前,可以確定資料庫的隔離級別是否是讀已提交,若不是,則需要預先配置資料庫為讀已提交這一隔離級別。例如,可以在應用程式伺服器執行目標事務之前,配置資料庫為讀已提交這一隔離級別。 If the method provided by this embodiment is applied to a database whose isolation level is read and submitted, then it is considered that some databases default to read and submit this isolation level, for example, the Oracle database defaults to read and submit this isolation level Yes, some databases do not set the read-committed isolation level by default. For example, the MySQL database defaults to the repeatable read isolation level, so before executing the method provided by this embodiment, it can be determined whether the isolation level of the database is read. Submitted, if not, you need to pre-configure the database to read submitted this isolation level. For example, you can configure the database to read-committed isolation level before the application server executes the target transaction.

在一可選實施方式中,應用程式伺服器按照現有方式向資料庫發送資料庫操作指令,以實現對資料庫的存取。資料庫操作裝置可以對應用程式伺服器與資料庫之間的通信進行監控,以攔截應用程式伺服器發往資料庫的資料庫操作指令。 In an optional implementation manner, the application program server sends database operation instructions to the database in an existing manner, so as to realize access to the database. The database operation device can monitor the communication between the application program server and the database, so as to intercept the database operation commands sent by the application program server to the database.

在另一可選實施方式中,對應用程式伺服器的處理邏輯進行稍微修改,將原來向資料庫發送資料庫操作指令的處理邏輯改為向資料庫操作裝置發送。基於此,資料庫操作裝置可以接收應用程式伺服器主動下發的資料庫操作指令。 In another optional implementation manner, the processing logic of the application server is slightly modified, and the original processing logic of sending database operation instructions to the database is changed to sending to the database operation device. Based on this, the database operation device can receive the database operation instruction issued by the application server.

在此說明,若目標事務是顯式事務,則目標事務中的首個指令是事務開始指令,如開始事務(begin transaction),基於此,資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令之前,可以攔截應用程式伺服器發往資料庫的目標事務中的事務開始指令,根據該事務開始指令,獲知需要執行目標事務。或者,資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令之前,可以接收應用程式伺服器主動下發的目標事務中的事務開始指令,根據該事務開始指令,獲知需要執行目標事務。若目標事務是隱式事務,則目標事務的首個指令就是 資料庫操作指令,不包括獲取事務開始指令的步驟。 It is explained here that if the target transaction is an explicit transaction, the first command in the target transaction is a transaction start command, such as begin transaction. Based on this, the database operation device obtains the data executed by the application server Before the library operation command, the transaction start command in the target transaction sent by the application server to the database can be intercepted, and according to the transaction start command, it is known that the target transaction needs to be executed. Alternatively, before the database operation device obtains the database operation command executed by the application server, it may receive the transaction start command in the target transaction actively issued by the application server, and learn that the target transaction needs to be executed according to the transaction start command. . If the target transaction is an implicit transaction, the first command of the target transaction is the database operation command, excluding the step of obtaining the transaction start command.

資料庫操作裝置在獲取應用程式伺服器執行到的資料庫操作指令後,對獲取到的資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器,以供應用程式伺服器確定下一個需要執行的資料庫操作指令。其中,預測執行結果決定著目標事務的執行路徑,這裡的執行路徑是指資料庫操作指令之間的跳轉邏輯。透過將預測執行結果返回給應用程式伺服器,從而達到由應用程式伺服器控制整個目標事務的執行邏輯的目的。 After the database operation device obtains the database operation instructions executed by the application program server, it predictively executes the obtained database operation instructions, and returns the prediction execution results to the application program server for the application program server to determine A database operation command to execute. Among them, the predicted execution result determines the execution path of the target transaction, and the execution path here refers to the jump logic between database operation instructions. By returning the predictive execution result to the application server, the application server controls the execution logic of the entire target transaction.

另外,資料庫操作裝置還需要在本地記錄所獲取到的資料庫操作指令,以及在該資料庫操作指令屬於被加鎖的非修改資料庫類指令時,記錄對該資料庫操作指令進行預測執行產生的預測執行資料。所述修改資料庫類指令是指對資料庫有修改的資料庫操作指令,例如以update、insert等開頭的指令;一般以select開頭的指令不屬於修改資料庫類指令(稱為非修改資料庫類指令)。有時,為了保證事務的一致性,降低轉返概率,有些非修改資料庫類指令可能需要透過for update語句進行加鎖,例如在上面轉帳舉例中,select指令需要加鎖,形成select......for update形式的資料庫操作指令,這種指令可稱為被加鎖的非修改資料庫類指令。所述預測執行資料主要是指對資料庫操作指令進行預測執行過程中的一些資料,例如資料庫操作指令所操作的資料以及資料的主鍵ID(itemId)和主鍵ID對應的版本號資訊(version)。 In addition, the database operation device also needs to record the obtained database operation instruction locally, and when the database operation instruction belongs to the locked non-modified database instruction, record the predictive execution of the database operation instruction Generated forecast execution data. Described modification database class instruction refers to the database operation instruction that database has modification, such as the instruction beginning with update, insert etc.; Generally the instruction beginning with select does not belong to modification database class instruction (referred to as non-modification database class instructions). Sometimes, in order to ensure the consistency of the transaction and reduce the probability of return, some non-modifying database commands may need to be locked through the for update statement. For example, in the above account transfer example, the select command needs to be locked to form a select... ...for update database operation instruction, this instruction can be called as locked non-modification database instruction. The predicted execution data mainly refers to some data in the process of predicting and executing the database operation command, such as the data operated by the database operation command and the primary key ID (itemId) of the data and the version number information (version) corresponding to the primary key ID .

值得說明的是,對於同一資料庫操作指令來說,要麼屬於修改資料庫類指令,要麼屬於被加鎖的非修改資料庫類指令,要麼屬於未加鎖的非修改資料庫類指令。在該實施例中,對於目標事務中所有被加鎖的非修改資料庫類指令預測執行產生的預測執行資料都會被記錄在本地。 It is worth noting that, for the same database operation command, either it belongs to the database modification command, or it belongs to the locked non-modification database command, or it belongs to the unlocked non-modification database command. In this embodiment, the speculative execution data generated by speculative execution of all locked non-modifying database instructions in the target transaction will be recorded locally.

例如,資料庫操作裝置可以在本地創建記憶體庫,將所獲取到的資料庫操作指令以及預測執行資料存入記憶體庫中。進一步,若預測執行資料不包括預測執行結果,則也可以將預測執行結果存入記憶體庫中。 For example, the database operating device can create a memory bank locally, and store the obtained database operation instructions and predicted execution data into the memory bank. Further, if the predictive execution data does not include the predictive execution result, the predictive execution result may also be stored in the memory bank.

為了在預測執行這一核心思想的基礎上實現對加鎖方案的模擬,本實施例針對被加鎖的非修改資料庫類指令的資料庫操作指令,記錄其預測執行產生的預測執行資料,卻不記錄其它資料庫操作指令對應的預測執行資料。由此可見,在實現加鎖方案的情況下,所記錄的資料量相對較少,這樣在後續基於本地記錄的資料庫操作指令和預測執行資料在資料庫中真實執行目標事務的過程中,所需處理的資料量相對較少,有利於進一步提高執行效率和性能,增大事務吞吐量。 In order to realize the simulation of the locking scheme on the basis of the core idea of predictive execution, this embodiment records the predictive execution data generated by the predictive execution for the database operation instructions of the locked non-modified database instructions, but Predictive execution data corresponding to other database operation instructions are not recorded. It can be seen that in the case of implementing the locking scheme, the amount of recorded data is relatively small, so that in the subsequent process of actually executing the target transaction in the database based on locally recorded database operation instructions and predicted execution data, all The amount of data to be processed is relatively small, which is conducive to further improving execution efficiency and performance, and increasing transaction throughput.

較優選的,在資料庫操作指令屬於被加鎖的非修改資料庫類指令時,可以僅記錄該資料庫操作指令所操作的資料的主鍵ID和版本號作為預測執行資料。這樣在後續基於本地記錄的資料庫操作指令和預測執行資料在資料庫中真實執行目標事務的過程中,所需處理的資料量會進一步較少,有利於進一步提高執行效率和性能,增大事務吞吐 量。另外,有時資料在事務過程中可能會經過多次修改,但最終又回到初始值,但資料的主鍵ID和版本號不會發生變化。因此,若採用記錄資料本身的方式,在預測執行與真實執行進行比較時資料可能不一樣,就要發生轉返,而轉返的代價較大。但是,本實施例採用只記錄主鍵ID和版本號的方式,在預測執行與真實執行進行比較時出現不一致的概率較低,轉返概率較低。 More preferably, when the database operation instruction is a locked non-modifying database instruction, only the primary key ID and version number of the data operated by the database operation instruction can be recorded as the predictive execution data. In this way, in the process of actually executing the target transaction in the database based on the locally recorded database operation instructions and predicted execution data, the amount of data to be processed will be further reduced, which is conducive to further improving execution efficiency and performance, and increasing the number of transactions. throughput. In addition, sometimes the data may be modified many times during the transaction process, but eventually returns to the initial value, but the primary key ID and version number of the data will not change. Therefore, if the method of recording the data itself is used, the data may be different when the predicted execution is compared with the actual execution, and a reversal will occur, and the cost of the reversal is relatively high. However, in this embodiment, only the primary key ID and the version number are recorded, so the probability of inconsistency and the turnaround probability are relatively low when the predicted execution is compared with the actual execution.

其中,資料庫操作指令對資料庫的操作主要是存取資料庫中的資料,則可以模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行。進一步,考慮到屬於非修改資料庫類指令的資料庫操作指令不會引起資料庫的變化,因此可以直接在資料庫中對該資料庫操作指令進行預測執行,這樣可以節約模擬資料環境的操作,實現相對簡單,有利於節約資源,提高執行效率。但是,對於屬於修改資料庫類指令的資料庫操作指令,由於其會引起資料庫的變化(主要是指資料庫中的資料被改變),因此需要模擬資料環境,在所模擬的資料環境中進行預測執行。 Wherein, the operation of the database operation command on the database is mainly to access the data in the database, so the data environment of the database operation command can be simulated, and the database operation command can be predicted and executed based on the simulated data environment. Further, considering that the database operation instructions belonging to non-modifying database instructions will not cause changes in the database, so the database operation instructions can be directly predicted and executed in the database, which can save the operation of the simulated data environment, The implementation is relatively simple, which is conducive to saving resources and improving execution efficiency. However, as for the database operation command belonging to the database modification command, because it will cause the change of the database (mainly refers to the change of the data in the database), it is necessary to simulate the data environment and perform the operation in the simulated data environment. Predictive execution.

基於上述分析,一種對資料庫操作指令進行預測執行的方式可以是:對獲取到的資料庫操作指令的類別進行識別;若資料庫操作指令屬於修改資料庫類指令,則在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行; 若資料庫操作指令屬於非修改資料庫類指令,在資料庫中執行該資料庫操作指令,以對該資料庫操作指令進行預測執行。 Based on the above analysis, a method of predictive execution of database operation instructions can be: identify the type of the acquired database operation instructions; The database simulates the data environment of the database operation command in the database, and predicts and executes the database operation command based on the simulated data environment; if the database operation command is a non-modifying database command, execute the database operation command in the database, to perform speculative execution on the database operation instruction.

進一步,一種在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於所模擬的資料環境對資料庫操作指令進行預測執行的實施方式包括:將獲取到的資料庫操作指令拆分為一個讀指令和一個寫指令,將讀指令運行於真正的資料庫上,即在資料庫中執行該讀指令以獲取讀資料集(ReadSet),將讀資料集存入本地記憶體庫中,以模擬資料庫操作指令所需的資料環境。然後,將寫指令作用於記憶體庫上,以實現對資料庫操作指令的預測執行,即在記憶體庫中執行寫指令以修改讀資料集,例如對讀資料集中的有關資料進行更新或查詢處理等。其中,執行寫指令以修改讀資料集可以產生結果資料集(affectRowInMemdb),該結果資料集包括預測執行結果。 Further, a data environment for simulating database operation instructions in a locally created memory bank, the implementation of predictive execution of database operation instructions based on the simulated data environment includes: splitting the obtained database operation instructions into A read command and a write command, run the read command on the real database, that is, execute the read command in the database to obtain the read data set (ReadSet), store the read data set in the local memory bank, and Simulate the data environment required by the database operation command. Then, the write command acts on the memory bank to realize the predictive execution of the database operation command, that is, execute the write command in the memory bank to modify the read data set, such as updating or querying the relevant data in the read data set processing etc. Wherein, executing the write command to modify the read data set can generate a result data set (affectRowInMemdb), and the result data set includes the predicted execution result.

當資料庫操作裝置獲取到目標事務中的事務提交指令(即應用程式伺服器執行到事務提交指令)時,則可以根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 When the database operation device obtains the transaction submission command in the target transaction (that is, the application program server executes the transaction submission command), it can control the corresponding application program server according to the locally recorded database operation command and predicted execution data. The database actually executes the target transaction.

上述根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務的一種可選實施方式包括:資料庫操作裝置向資料庫下發本地記錄的資料庫操作 指令,以指示資料庫執行資料庫操作指令,並接收資料庫返回的資料庫操作指令的實際執行結果;然後,將資料庫操作指令的實際執行結果與預測執行結果進行比較,若實際執行結果與預測執行結果相同,向資料庫下發事務提交指令,以供資料庫提交該目標事務;若實際執行結果與預測執行結果不相同,向資料庫下發事務轉返指令,以供資料庫轉返該目標事務。 An optional implementation manner of controlling the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instructions and the predicted execution data includes: the database operation device sends the locally recorded database operation to the database command to instruct the database to execute the database operation command, and receive the actual execution result of the database operation command returned by the database; then, compare the actual execution result of the database operation command with the predicted execution result, if the actual execution result is the same as If the predicted execution result is the same, issue a transaction submission command to the database for the database to submit the target transaction; if the actual execution result is different from the predicted execution result, issue a transaction return command to the database for the database to return the target transaction.

在上述實施方式中,考慮到事務的原子性,透過將實際執行結果與預測執行結果進行比較,可以避免事務執行過程出錯,有利於提高事務執行的成功概率。 In the above embodiments, considering the atomicity of the transaction, by comparing the actual execution result with the predicted execution result, errors in the transaction execution process can be avoided, which is beneficial to improve the success probability of the transaction execution.

進一步,在現有技術中,由於無法預先知道事務需要執行的全部資料庫操作指令,所以只能按照事務的執行邏輯依次執行資料庫操作指令,這會導致應用程式伺服器與資料庫之間的頻繁互動,這在遠程應用場景中將會消耗大量網路資源。但在本實施例中,透過預測執行過程已經預先獲得目標事務需要執行的全部資料庫操作指令,即本地記錄的資料庫操作指令,因此在向資料庫下發本地記錄的資料庫操作指令,以指示資料庫執行資料庫操作指令的過程中,可以將本地記錄的資料庫操作指令同時(或者一併)下發到資料庫,有利於節約網路資源。另外,對於一些並行的資料庫操作指令,資料庫還可以並行操作,有利於進一步提高事務執行效率,增大事務吞吐量。 Furthermore, in the prior art, since it is impossible to know in advance all the database operation instructions that the transaction needs to execute, the database operation instructions can only be executed sequentially according to the execution logic of the transaction, which will lead to frequent interaction between the application server and the database , which will consume a lot of network resources in remote application scenarios. However, in this embodiment, all database operation instructions that need to be executed by the target transaction, that is, locally recorded database operation instructions, have been obtained in advance through the predictive execution process, so the locally recorded database operation instructions are sent to the database to During the process of instructing the database to execute the database operation instruction, the database operation instructions recorded locally can be sent to the database at the same time (or together), which is beneficial to save network resources. In addition, for some parallel database operation instructions, the database can also be operated in parallel, which is conducive to further improving transaction execution efficiency and increasing transaction throughput.

進一步,還可以將本地記錄的所有資料庫操作指令連同本地記錄的預測執行資料一併下發給資料庫。 Furthermore, all the database operation instructions recorded locally together with the predicted execution data recorded locally can also be issued to the database.

進一步,在一種根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務的可選實施方式中,資料庫操作裝置可以根據本地記錄的資料庫操作指令,判斷目標事務是否為單機事務;若判斷結果為是,則根據本地記錄的資料庫操作指令以及預測執行資料,以單機事務處理邏輯控制資料庫實際執行目標事務;若判斷結果為否,則根據本地記錄的資料庫操作指令以及預測執行資料,以分布式事務處理邏輯控制資料庫實際執行目標事務。 Further, in an optional implementation mode in which the database corresponding to the application server is controlled to actually execute the target transaction according to the locally recorded database operation instruction and the predicted execution data, the database operation device may use the locally recorded database operation instruction , to judge whether the target transaction is a stand-alone transaction; if the judgment result is yes, then according to the locally recorded database operation instructions and predicted execution data, the stand-alone transaction processing logic is used to control the database to actually execute the target transaction; if the judgment result is no, then according to The locally recorded database operation instructions and forecast execution data control the database to actually execute the target transaction with the distributed transaction processing logic.

其中,基於預測執行過程中獲得的資料庫操作指令可以識別出目標事務是單機事務還是分布式事務。例如,可以判斷本地記錄的資料庫操作指令的操作對象是否作用於同一實體設備上,若判斷結果為是,則可以確定該目標事務為單機事務;若判斷結果為否,則可以確定該目標事務為分布式事務。 Wherein, it can be identified whether the target transaction is a stand-alone transaction or a distributed transaction based on the database operation instructions obtained during the predictive execution process. For example, it can be judged whether the operation object of the locally recorded database operation command acts on the same physical device, if the judgment result is yes, it can be determined that the target transaction is a stand-alone transaction; if the judgment result is no, it can be determined that the target transaction for distributed transactions.

由於分布式事務的處理邏輯和單機事務的處理邏輯有所不同,單機事務的處理邏輯相對簡單,例如不涉及讀寫加鎖等問題,所以所消耗的資源也相對較少。基於此,本實施例透過識別目標事務是否為單機事務,在識別為單機事務時採用單機事務的處理邏輯進行處理,而不是像現有技術那樣統一採用分布式事務處理邏輯進行處理,有利於提高處理效率,節約處理成本。 Because the processing logic of distributed transactions is different from that of stand-alone transactions, the processing logic of stand-alone transactions is relatively simple, for example, it does not involve issues such as read-write locks, so the resources consumed are relatively small. Based on this, in this embodiment, by identifying whether the target transaction is a stand-alone transaction, when it is identified as a stand-alone transaction, the processing logic of the stand-alone transaction is used for processing, instead of uniformly adopting the distributed transaction processing logic for processing as in the prior art, which is beneficial to improve the processing efficiency. efficiency and save processing costs.

在一可選實施方式中,若資料庫操作裝置獲取到目標事務中的事務轉返指令(即應用程式伺服器執行到事務轉 返指令)時,可以刪除本地記錄的資料庫操作指令以及預測執行資料,例如可以直接清空本地記憶體庫,實現轉返操作。在該情況下,由於目標事務並未在資料庫中真正執行,所以資料庫無需執行轉返操作,可見,在事務轉返這種情況下,採用本實施例方法也可以提高事務執行效率。 In an optional implementation, if the database operation device obtains the transaction return command in the target transaction (that is, the application server executes the transaction return command), it can delete the locally recorded database operation command and predictive execution data, for example, the local memory bank can be cleared directly to realize the transfer operation. In this case, since the target transaction is not actually executed in the database, the database does not need to perform a rollback operation. It can be seen that in the case of transaction rollback, the method of this embodiment can also improve transaction execution efficiency.

由上述可見,在本實施例提供的方法中,資料庫操作裝置與應用程式伺服器相互配合,在應用程式伺服器執行目標事務的過程加入預測執行過程,在預測執行過程中,可以提前獲得目標事務的執行路徑,即真正需要執行的資料庫操作指令,並僅針對屬於被加鎖的非修改資料庫類指令的資料庫操作指令記錄預測執行產生的預測執行資料,以適應於加鎖方案,為真實執行事務提供了條件,之後在根據記錄的資料庫操作指令以及預測執行資料實際執行該目標事務時,例如基於預測執行資料可以提前獲取有關資料資訊並且可以減少指令之間的跳轉等,因此可以提高執行效率,增大事務吞吐量。 It can be seen from the above that in the method provided by this embodiment, the database operation device and the application server cooperate with each other, and the predictive execution process is added to the process of executing the target transaction on the application server. During the predictive execution process, the target can be obtained in advance. The execution path of the transaction, that is, the database operation instructions that really need to be executed, and only record the predicted execution data generated by the predicted execution for the database operation instructions that belong to the locked non-modified database instructions, so as to adapt to the locking scheme. Provides conditions for the real execution of transactions, and then when the target transaction is actually executed according to the recorded database operation instructions and predicted execution data, for example, based on the predicted execution data, relevant data information can be obtained in advance and jumps between instructions can be reduced, etc., so It can improve execution efficiency and increase transaction throughput.

需要說明的是,對於前述的各方法實施例,為了簡單描述,故將其都表述為一系列的動作組合,但是本領域技術人員應該知悉,本發明並不受所描述的動作順序的限制,因為依據本發明,某些步驟可以採用其他順序或者同時進行。其次,本領域技術人員也應該知悉,說明書中所描述的實施例均屬於優選實施例,所涉及的動作和模組並不一定是本發明所必須的。 It should be noted that for the foregoing method embodiments, for the sake of simple description, they are expressed as a series of action combinations, but those skilled in the art should know that the present invention is not limited by the described action sequence. Because of the present invention, certain steps may be performed in other orders or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification belong to preferred embodiments, and the actions and modules involved are not necessarily required by the present invention.

在上述實施例中,對各個實施例的描述都各有側重, 某個實施例中沒有詳述的部分,可以參見其他實施例的相關描述。 In the foregoing embodiments, the descriptions of each embodiment have their own emphases, and for the parts not described in detail in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.

圖5為本發明又一實施例提供的資料庫操作裝置的結構示意圖。如圖5所示,該裝置包括:獲取模組51、預測執行模組52和控制執行模組53。 Fig. 5 is a schematic structural diagram of a database operation device provided by another embodiment of the present invention. As shown in FIG. 5 , the device includes: an acquisition module 51 , a prediction execution module 52 and a control execution module 53 .

獲取模組51,用於在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令。 The obtaining module 51 is used for sequentially obtaining the database operation instructions in the target transaction executed by the application server during the execution of the target transaction by the application server.

預測執行模組52,用於對資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器以供應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄資料庫操作指令以及在資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄預測執行產生的預測執行資料。 The predictive execution module 52 is used to predictively execute the database operation instructions, return the predictive execution results to the application server for the application server to determine the next database operation instruction to be executed, and record the database locally The operation instruction and the predictive execution data generated by the predictive execution are recorded when the database operation instruction belongs to one of the modified database type instruction and the locked non-modified database type instruction.

控制執行模組53,用於當獲取到目標事務中的事務提交指令時,根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 The control execution module 53 is used to control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instructions and predicted execution data when the transaction commit command in the target transaction is obtained.

在一可選實施方式中,預測執行模組52在資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄預測執行產生的預測執行資料的過程中,具體可用於:在資料庫操作指令屬於被加鎖的非修改資料庫類指令時,僅記錄該資料庫操作指令所操作的資料的主鍵ID和 資料版本號作為預測執行資料。 In an optional implementation, the predictive execution module 52 is in the process of recording the predictive execution data generated by the predictive execution when the database operation instruction belongs to one of the modified database type instruction and the locked non-modified database type instruction, Specifically, it can be used for: when the database operation instruction belongs to the locked non-modifying database instruction, only the primary key ID and the data version number of the data operated by the database operation instruction are recorded as the predictive execution data.

在一可選實施方式中,該裝置還包括:配置模組,用於在應用程式伺服器執行目標事務之前,配置資料庫為讀已提交隔離級別。 In an optional implementation manner, the device further includes: a configuration module, configured to configure the database as a read-committed isolation level before the application server executes the target transaction.

在一可選實施方式中,預測執行模組52具體用於:若資料庫操作指令屬於修改資料庫類指令,在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於模擬的資料環境對資料庫操作指令進行預測執行;若資料庫操作指令屬於非修改資料庫類指令,在資料庫中執行資料庫操作指令,以對資料庫操作指令進行預測執行。 In an optional implementation manner, the predictive execution module 52 is specifically used for: if the database operation instruction belongs to the instruction to modify the database, simulate the data environment of the database operation instruction in the locally created memory library, based on the simulated data The environment performs predictive execution on the database operation instruction; if the database operation instruction is a non-modifying database instruction, the database operation instruction is executed in the database to predictively execute the database operation instruction.

進一步,預測執行模組52在資料庫操作指令屬於修改資料庫類指令的情況下,在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於模擬的資料環境對資料庫操作指令進行預測執行時,具體用於:將資料庫操作指令拆分為讀指令和寫指令;在資料庫中執行讀指令以獲取讀資料集,將讀資料集存入記憶體庫中以模擬資料庫操作指令的資料環境;在記憶體庫中執行寫指令以修改讀資料集。 Further, the predictive execution module 52 simulates the data environment of the database operation instruction in the locally created memory bank when the database operation instruction belongs to the database modification instruction, and executes the database operation instruction based on the simulated data environment. During predictive execution, it is specifically used to: split database operation instructions into read instructions and write instructions; execute read instructions in the database to obtain the read data set, and store the read data set in the memory bank to simulate database operations The data context of the command; executes the write command in the memory bank to modify the read data set.

在一可選實施方式中,控制執行模組53具體可用於:向資料庫下發本地記錄的資料庫操作指令,以指示資料庫執行資料庫操作指令,並接收資料庫返回的資料庫操作指令的實際執行結果; 若實際執行結果與預測執行結果相同,向資料庫下發事務提交指令,以供資料庫提交目標事務;若實際執行結果與預測執行結果不相同,向資料庫下發事務轉返指令,以供資料庫轉返目標事務。 In an optional implementation, the control execution module 53 can be specifically configured to: issue locally recorded database operation instructions to the database to instruct the database to execute the database operation instructions, and receive the database operation instructions returned by the database The actual execution result; if the actual execution result is the same as the predicted execution result, issue a transaction submission command to the database for the database to submit the target transaction; if the actual execution result is different from the predicted execution result, issue a transaction submission command to the database Return command for the database to return the target transaction.

本實施例提供的資料庫操作裝置,與應用程式伺服器相互配合,在應用程式伺服器執行目標事務的過程加入預測執行過程,預先獲得並記錄目標事務需要執行的全部資料庫操作指令,並僅針對屬於修改資料庫類指令和被加鎖的非修改資料庫類指令的資料庫操作指令,或者僅針對屬於被加鎖的非修改資料庫類指令的資料庫操作指令記錄預測執行產生的預測執行資料,以適應於讀已提交隔離級別下的加鎖方案,為真實執行事務提供了條件,之後根據記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行該目標事務,有利於提高執行效率,進而增大事務吞吐量。 The database operation device provided in this embodiment cooperates with the application server, adds a predictive execution process in the process of the application server executing the target transaction, obtains and records in advance all the database operation instructions that the target transaction needs to execute, and only Speculative executions resulting from record speculative execution for database operation instructions that belong to the modifying database class instruction and the locked non-modifying database class instruction, or only for database operation instructions that belong to the locked non-modifying database class instruction Data, to adapt to the locking scheme under the read-committed isolation level, provides conditions for the actual execution of transactions, and then controls the database corresponding to the application server to actually execute the target according to the recorded database operation instructions and predicted execution data Transactions are conducive to improving execution efficiency and thereby increasing transaction throughput.

圖6為本發明又一實施例提供的資料庫操作裝置的結構示意圖。如圖6所示,該裝置包括:獲取模組61、預測執行模組62和控制執行模組63。 FIG. 6 is a schematic structural diagram of a database operating device provided by another embodiment of the present invention. As shown in FIG. 6 , the device includes: an acquisition module 61 , a prediction execution module 62 and a control execution module 63 .

獲取模組61,用於在應用程式伺服器執行目標事務的過程中,依次獲取應用程式伺服器執行到的目標事務中的資料庫操作指令。 The obtaining module 61 is used for sequentially obtaining the database operation instructions in the target transaction executed by the application server during the execution of the target transaction by the application server.

預測執行模組62,用於對資料庫操作指令進行預測執行,將預測執行結果返回給應用程式伺服器以供應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本 地記錄資料庫操作指令以及在資料庫操作指令屬於被加鎖的非修改資料庫類指令時記錄預測執行產生的預測執行資料。 The predictive execution module 62 is used to predictively execute the database operation instructions, return the predictive execution results to the application server for the application server to determine the next database operation instruction to be executed, and record the database locally The operation instruction and the speculative execution data generated by the speculative execution are recorded when the database operation instruction belongs to the locked non-modifying database instruction.

控制執行模組63,用於當獲取到目標事務中的事務提交指令時,根據本地記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行目標事務。 The control execution module 63 is used to control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instructions and predicted execution data when the transaction commit command in the target transaction is obtained.

在一可選實施方式中,預測執行模組62具體可用於:在資料庫操作指令屬於被加鎖的非修改資料庫類指令時,僅記錄該資料庫操作指令所操作的資料的主鍵ID和版本號作為預測執行資料。 In an optional implementation, the predictive execution module 62 can be specifically configured to: when a database operation instruction belongs to a locked non-modified database instruction, only record the primary key ID and ID of the data operated by the database operation instruction. The version number is used as forecast execution data.

在一可選實施方式中,該裝置還包括:配置模組,用於在應用程式伺服器執行目標事務之前,配置資料庫為讀已提交隔離級別。 In an optional implementation manner, the device further includes: a configuration module, configured to configure the database as a read-committed isolation level before the application server executes the target transaction.

在一可選實施方式中,預測執行模組62具體用於:若資料庫操作指令屬於修改資料庫類指令,在本地創建的記憶體庫中模擬資料庫操作指令的資料環境,基於模擬的資料環境對資料庫操作指令進行預測執行;若資料庫操作指令屬於非修改資料庫類指令,在資料庫中執行資料庫操作指令,以對資料庫操作指令進行預測執行。 In an optional implementation manner, the predictive execution module 62 is specifically used to: if the database operation instruction belongs to the instruction to modify the database, simulate the data environment of the database operation instruction in the locally created memory library, based on the simulated data The environment performs predictive execution on the database operation instruction; if the database operation instruction is a non-modifying database instruction, the database operation instruction is executed in the database to predictively execute the database operation instruction.

進一步,預測執行模組62在資料庫操作指令屬於修改資料庫類指令的情況下,在本地創建的記憶體庫中模擬 資料庫操作指令的資料環境,基於模擬的資料環境對資料庫操作指令進行預測執行時,具體用於:將資料庫操作指令拆分為讀指令和寫指令;在資料庫中執行讀指令以獲取讀資料集,將讀資料集存入記憶體庫中以模擬資料庫操作指令的資料環境;在記憶體庫中執行寫指令以修改讀資料集。 Further, the predictive execution module 62 simulates the data environment of the database operation instruction in the locally created memory bank when the database operation instruction belongs to the database modification instruction, and executes the database operation instruction based on the simulated data environment. During predictive execution, it is specifically used to: split database operation instructions into read instructions and write instructions; execute read instructions in the database to obtain the read data set, and store the read data set in the memory bank to simulate database operations The data context of the command; executes the write command in the memory bank to modify the read data set.

在一可選實施方式中,控制執行模組63具體可用於:向資料庫下發本地記錄的資料庫操作指令,以指示資料庫執行資料庫操作指令,並接收資料庫返回的資料庫操作指令的實際執行結果;若實際執行結果與預測執行結果相同,向資料庫下發事務提交指令,以供資料庫提交目標事務;若實際執行結果與預測執行結果不相同,向資料庫下發事務轉返指令,以供資料庫轉返目標事務。 In an optional implementation, the control execution module 63 can be specifically configured to: issue locally recorded database operation instructions to the database to instruct the database to execute the database operation instructions, and receive the database operation instructions returned by the database The actual execution result; if the actual execution result is the same as the predicted execution result, issue a transaction submission command to the database for the database to submit the target transaction; if the actual execution result is different from the predicted execution result, issue a transaction submission command to the database Return command for the database to return the target transaction.

本實施例提供的資料庫操作裝置,與應用程式伺服器相互配合,在應用程式伺服器執行目標事務的過程加入預測執行過程,預先獲得並記錄目標事務需要執行的全部資料庫操作指令,並僅針對屬於被加鎖的非修改資料庫類指令的資料庫操作指令記錄預測執行產生的預測執行資料,以適應於加鎖方案,為真實執行事務提供了條件,之後根據記錄的資料庫操作指令以及預測執行資料,控制應用程式伺服器對應的資料庫實際執行該目標事務,有利於提高執行效率,進而增大事務吞吐量。 The database operation device provided in this embodiment cooperates with the application server, adds a predictive execution process in the process of the application server executing the target transaction, obtains and records in advance all the database operation instructions that the target transaction needs to execute, and only For the database operation instructions that belong to the locked non-modified database instructions, record the prediction execution data generated by the prediction execution, so as to adapt to the locking scheme, provide conditions for the real execution of the transaction, and then according to the recorded database operation instructions and Predicting the execution data and controlling the database corresponding to the application server to actually execute the target transaction is beneficial to improve the execution efficiency and further increase the transaction throughput.

所屬領域的技術人員可以清楚地瞭解到,為描述的方便和簡潔,上述描述的系統,裝置和單元的具體工作過程,可以參考前述方法實施例中的對應過程,在此不再贅述。 Those skilled in the art can clearly understand that for the convenience and brevity of the description, the specific working process of the above-described system, device and unit can refer to the corresponding process in the foregoing method embodiment, which will not be repeated here.

在本發明所提供的幾個實施例中,應該理解到,所揭露的系統,裝置和方法,可以透過其它的方式實現。例如,以上所描述的裝置實施例僅僅是示意性的,例如,所述單元的劃分,僅僅為一種邏輯功能劃分,實際實現時可以有另外的劃分方式,例如多個單元或組件可以結合或者可以整合到另一個系統,或一些特徵可以忽略,或不執行。另一點,所顯示或討論的相互之間的耦合或直接耦合或通信連接可以是透過一些介面,裝置或單元的間接耦合或通信連接,可以是電性,機械或其它的形式。 In the several embodiments provided by the present invention, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or can be Integrate into another system, or some features may be ignored, or not implemented. In another point, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, and the indirect coupling or communication connection of devices or units may be in electrical, mechanical or other forms.

所述作為分離部件說明的單元可以是或者也可以不是實體上分開的,作為單元顯示的部件可以是或者也可以不是實體單元,即可以位於一個地方,或者也可以分佈到多個網路單元上。可以根據實際的需要選擇其中的部分或者全部單元來實現本實施例方案的目的。 The unit described as a separate component may or may not be physically separated, and the component displayed as a unit may or may not be a physical unit, that is, it may be located in one place, or may also be distributed to multiple network units . Part or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

另外,在本發明各個實施例中的各功能單元可以整合在一個處理單元中,也可以是各個單元單獨實體存在,也可以兩個或兩個以上單元整合在一個單元中。上述整合的單元既可以採用硬體的形式實現,也可以採用硬體加軟體功能單元的形式實現。 In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, each unit may exist separately, or two or more units may be integrated into one unit. The above-mentioned integrated units can be implemented not only in the form of hardware, but also in the form of hardware plus software functional units.

上述以軟體功能單元的形式實現的整合的單元,可以 儲存在一個計算機可讀取儲存媒體中。上述軟體功能單元儲存在一個儲存媒體中,包括若干指令用以使得一台電腦設備(可以是個人電腦,伺服器,或者網路設備等)或處理器(processor)執行本發明各個實施例所述方法的部分步驟。而前述的儲存媒體包括:USB隨身碟、移動硬碟、唯讀記憶體(Read-Only Memory,ROM)、隨機存取記憶體(Random Access Memory,RAM)、磁碟或者光碟等各種可以儲存程式碼的媒體。 The above-mentioned integrated units realized in the form of software functional units can be stored in a computer-readable storage medium. The above-mentioned software functional units are stored in a storage medium, and include several instructions to enable a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor (processor) to execute the various embodiments of the present invention. part of the method. The aforementioned storage media include: USB flash drive, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), magnetic disk or optical disk, etc., which can store programs. coded media.

最後應說明的是:以上實施例僅用以說明本發明的技術方案,而非對其限制;儘管參照前述實施例對本發明進行了詳細的說明,本領域的普通技術人員應當理解:其依然可以對前述各實施例所記載的技術方案進行修改,或者對其中部分技術特徵進行等同替換;而這些修改或者替換,並不使相應技術方案的本質脫離本發明各實施例技術方案的精神和範圍。 Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: it can still be Modifications are made to the technical solutions described in the foregoing embodiments, or equivalent replacements are made to some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the various embodiments of the present invention.

Claims (10)

一種資料庫操作方法,其特徵在於,包括:資料庫操作裝置在應用程式伺服器執行目標事務的過程中,依次獲取該應用程式伺服器執行到的該目標事務中的資料庫操作指令;該資料庫操作裝置對該資料庫操作指令進行預測執行,將預測執行結果返回給該應用程式伺服器以供該應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄該資料庫操作指令以及在該資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄該預測執行產生的預測執行資料;當該資料庫操作裝置獲取到該目標事務中的事務提交指令時,根據本地記錄的該資料庫操作指令以及使用被記錄的該預測執行產生的該預測執行資料,控制該應用程式伺服器對應的資料庫實際執行該目標事務,包括以下步驟:向該資料庫下發本地記錄的該資料庫操作指令,以指示該資料庫執行該資料庫操作指令,並接收該資料庫返回的該資料庫操作指令的實際執行結果;若該實際執行結果與該預測執行結果相同,向該資料庫下發該事務提交指令,以供該資料庫提交該目標事務;若該實際執行結果與該預測執行結果不相同,向該資料庫下發事務轉返(rollback)指令,以供該資料庫轉返該目標事務。 A database operation method, characterized in that it comprises: the database operation device sequentially obtains the database operation instructions in the target transaction executed by the application server during the process of executing the target transaction by the application server; the data The library operation device performs predictive execution on the database operation command, returns the predictive execution result to the application server for the application server to determine the next database operation command to be executed, and records the database operation locally Instructions and when the database operation instruction belongs to one of the modified database instruction and the locked non-modified database instruction, record the predictive execution data generated by the predictive execution; when the database operation device obtains the target transaction When submitting an instruction for a transaction, control the database corresponding to the application server to actually execute the target transaction according to the locally recorded database operation instruction and the predicted execution data generated by using the recorded predicted execution, including the following steps: issuing the database operation instruction recorded locally to the database to instruct the database to execute the database operation instruction, and receiving the actual execution result of the database operation instruction returned by the database; if the actual execution result is the same as If the predicted execution result is the same, issue the transaction submission command to the database for the database to submit the target transaction; if the actual execution result is different from the predicted execution result, issue a transaction return ( rollback) command for the database to roll back the target transaction. 根據申請專利範圍第1項的方法,其中,該在該資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄該預測執行產生的預測執行資料,包括:在該資料庫操作指令屬於被加鎖的非修改資料庫類指令時,僅記錄該資料庫操作指令所操作的資料的主鍵ID和版本號作為該預測執行資料。 According to the method of item 1 of the scope of patent application, wherein, when the database operation instruction belongs to one of the modified database type instruction and the locked non-modified database type instruction, the predicted execution data generated by the predicted execution is recorded, including : When the database operation instruction is a locked non-modifying database instruction, only record the primary key ID and version number of the data operated by the database operation instruction as the predictive execution data. 根據申請專利範圍第1項的方法,其中,還包括:在該應用程式伺服器執行該目標事務之前,配置該資料庫為讀已提交隔離級別。 The method according to item 1 of the scope of the patent application further includes: before the application server executes the target transaction, configuring the database as a read-committed isolation level. 根據申請專利範圍第1項的方法,其中,該對該資料庫操作指令進行預測執行,包括:若該資料庫操作指令屬於修改資料庫類指令,在本地創建的記憶體庫中模擬該資料庫操作指令的資料環境,基於該模擬的資料環境對該資料庫操作指令進行預測執行;若該資料庫操作指令屬於非修改資料庫類指令,在該資料庫中執行該資料庫操作指令,以對該資料庫操作指令進行預測執行。 According to the method of item 1 of the scope of the patent application, wherein the predictive execution of the database operation instruction includes: if the database operation instruction belongs to the instruction of modifying the database, simulating the database in the locally created memory bank The data environment of the operation command, based on the simulated data environment, the database operation command is predicted and executed; if the database operation command belongs to the non-modified database type command, the database operation command is executed in the database to The database operation instruction performs speculative execution. 根據申請專利範圍第4項的方法,其中,該在本地創建的記憶體庫中模擬該資料庫操作指令所需的資料環境,基於該模擬的資料環境對該資料庫操作指令進行預測執行,包括:將該資料庫操作指令拆分為讀指令和寫指令; 在該資料庫中執行該讀指令以獲取讀資料集,將該讀資料集存入該記憶體庫中以模擬該資料庫操作指令的資料環境;在該記憶體庫中執行該寫指令以修改該讀資料集。 According to the method of item 4 of the scope of patent application, wherein, the data environment required by the database operation instruction is simulated in the locally created memory library, and the database operation instruction is predicted and executed based on the simulated data environment, including : Split the database operation command into a read command and a write command; Execute the read command in the database to obtain a read data set, store the read data set in the memory bank to simulate the data environment of the database operation command; execute the write command in the memory bank to modify It's time to read the dataset. 一種資料庫操作裝置,其特徵在於,包括:獲取模組,用於在應用程式伺服器執行目標事務的過程中,依次獲取該應用程式伺服器執行到的該目標事務中的資料庫操作指令;預測執行模組,用於對該資料庫操作指令進行預測執行,將預測執行結果返回給該應用程式伺服器以供該應用程式伺服器確定下一個需要執行的資料庫操作指令,並在本地記錄該資料庫操作指令以及在該資料庫操作指令屬於修改資料庫類指令和被加鎖的非修改資料庫類指令其中之一時記錄該預測執行產生的預測執行資料;控制執行模組,用於當獲取到該目標事務中的事務提交指令時,根據本地記錄的該資料庫操作指令以及使用被記錄的該預測執行產生的該預測執行資料,控制該應用程式伺服器對應的資料庫實際執行該目標事務,其中,該控制執行模組具體用於:向該資料庫下發本地記錄的該資料庫操作指令,以指示該資料庫執行該資料庫操作指令,並接收該資料庫返回的該資料庫操作指令的實際執行結果;若該實際執行結果與該預測執行結果相同,向該資料庫下發該事務提交指令,以供該資料庫提交該目標事務; 若該實際執行結果與該預測執行結果不相同,向該資料庫下發事務轉返(rollback)指令,以供該資料庫轉返該目標事務。 A database operation device, characterized in that it includes: an acquisition module, used to sequentially acquire database operation instructions in the target transaction executed by the application server during the execution of the target transaction by the application server; The predictive execution module is used to predictively execute the database operation command, return the predictive execution result to the application program server for the application program server to determine the next database operation command to be executed, and record it locally The database operation instruction and when the database operation instruction belongs to one of the database modification instruction and the locked non-modification database instruction, record the prediction execution data generated by the prediction execution; the control execution module is used for when When the transaction commit command in the target transaction is obtained, control the database corresponding to the application server to actually execute the target according to the locally recorded database operation command and the predicted execution data generated by using the recorded predicted execution transaction, wherein the control execution module is specifically used to: issue the database operation instruction recorded locally to the database, instruct the database to execute the database operation instruction, and receive the database operation instruction returned by the database The actual execution result of the operation instruction; if the actual execution result is the same as the predicted execution result, issue the transaction submission instruction to the database for the database to submit the target transaction; If the actual execution result is different from the predicted execution result, issue a transaction rollback (rollback) command to the database for the database to roll back the target transaction. 根據申請專利範圍第6項的裝置,其中,該預測執行模組具體用於:在該資料庫操作指令屬於被加鎖的非修改資料庫類指令時,僅記錄該資料庫操作指令所操作的資料的主鍵ID和版本號作為該預測執行資料。 The device according to item 6 of the scope of the patent application, wherein the predictive execution module is specifically used for: when the database operation instruction belongs to a locked non-modified database instruction, only record the database operation instruction operated The primary key ID and version number of the data are used as the forecast execution data. 根據申請專利範圍第6項的裝置,其中,還包括:配置模組,用於在該應用程式伺服器執行該目標事務之前,配置該資料庫為讀已提交隔離級別。 The device according to item 6 of the scope of the patent application further includes: a configuration module configured to configure the database as a read-committed isolation level before the application server executes the target transaction. 根據申請專利範圍第6項的裝置,其中,該預測執行模組具體用於:若該資料庫操作指令屬於修改資料庫類指令,在本地創建的記憶體庫中模擬該資料庫操作指令的資料環境,基於該模擬的資料環境對該資料庫操作指令進行預測執行;若該資料庫操作指令屬於非修改資料庫類指令,在該資料庫中執行該資料庫操作指令,以對該資料庫操作指令進行預測執行。 According to the device according to item 6 of the scope of the patent application, the predictive execution module is specifically used for: if the database operation instruction belongs to the modification database instruction, simulate the data of the database operation instruction in the locally created memory bank Environment, based on the simulated data environment, predictively execute the database operation instruction; if the database operation instruction is a non-modified database instruction, execute the database operation instruction in the database to operate the database instructions are speculatively executed. 根據申請專利範圍第9項的裝置,其中,該預測執行模組具體用於:將該資料庫操作指令拆分為讀指令和寫指令;在該資料庫中執行該讀指令以獲取讀資料集,將該讀 資料集存入該記憶體庫中以模擬該資料庫操作指令的資料環境;在該記憶體庫中執行該寫指令以修改該讀資料集。 The device according to item 9 of the scope of the patent application, wherein the predictive execution module is specifically used to: split the database operation instruction into a read instruction and a write instruction; execute the read instruction in the database to obtain a read data set , which will read The data set is stored in the memory bank to simulate the data environment of the database operation command; the write command is executed in the memory bank to modify the read data set.
TW106102692A 2017-01-24 2017-01-24 Database operation method and device TWI805544B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
TW106102692A TWI805544B (en) 2017-01-24 2017-01-24 Database operation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
TW106102692A TWI805544B (en) 2017-01-24 2017-01-24 Database operation method and device

Publications (2)

Publication Number Publication Date
TW201828117A TW201828117A (en) 2018-08-01
TWI805544B true TWI805544B (en) 2023-06-21

Family

ID=63960251

Family Applications (1)

Application Number Title Priority Date Filing Date
TW106102692A TWI805544B (en) 2017-01-24 2017-01-24 Database operation method and device

Country Status (1)

Country Link
TW (1) TWI805544B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040078559A1 (en) * 2002-10-22 2004-04-22 Kabushiki Kaisha Toshiba Speculative execution control device for computer instructions and method for the same
US20120215751A1 (en) * 2008-01-21 2012-08-23 International Business Machines Corporation Transaction prediction modeling method
TW201346524A (en) * 2011-12-30 2013-11-16 Intel Corp Configurable reduced instruction set core
US20160070573A1 (en) * 2014-09-10 2016-03-10 International Business Machines Corporation Condition code generation

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040078559A1 (en) * 2002-10-22 2004-04-22 Kabushiki Kaisha Toshiba Speculative execution control device for computer instructions and method for the same
US20120215751A1 (en) * 2008-01-21 2012-08-23 International Business Machines Corporation Transaction prediction modeling method
TW201346524A (en) * 2011-12-30 2013-11-16 Intel Corp Configurable reduced instruction set core
US20160070573A1 (en) * 2014-09-10 2016-03-10 International Business Machines Corporation Condition code generation

Also Published As

Publication number Publication date
TW201828117A (en) 2018-08-01

Similar Documents

Publication Publication Date Title
US10649995B2 (en) Methods and systems for optimizing queries in a multi-tenant store
WO2017107811A1 (en) Database operating method and device
US11386065B2 (en) Database concurrency control through hash-bucket latching
WO2017041639A1 (en) Database operating method and device
EP3396542B1 (en) Database operating method and device
TWI774643B (en) Database operation method and device
WO2017041637A1 (en) Database operating method and device
CN111949673B (en) Hbase storage-based distributed pessimistic lock and implementation method thereof
US7908268B2 (en) Predictive database pool preparation
TWI805544B (en) Database operation method and device
TWI725110B (en) Database operation method and device
US10929395B2 (en) Data modification in hypothetical planning
TWI735512B (en) Database operation method and device
TWI790991B (en) Database operation method and device
US20240134838A1 (en) Flexible routing of database operations accessing remote tables
US20240086387A1 (en) Delta transition table for database triggers
CN117687807A (en) Data processing method, device, electronic equipment and storage medium
CN116204508A (en) Multi-version concurrency control method, device, equipment and storage medium