CN115756765A - Distributed transaction processing method and device - Google Patents

Distributed transaction processing method and device Download PDF

Info

Publication number
CN115756765A
CN115756765A CN202211487672.0A CN202211487672A CN115756765A CN 115756765 A CN115756765 A CN 115756765A CN 202211487672 A CN202211487672 A CN 202211487672A CN 115756765 A CN115756765 A CN 115756765A
Authority
CN
China
Prior art keywords
transaction
sub
execution
distributed
execution data
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211487672.0A
Other languages
Chinese (zh)
Inventor
苗海柱
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Bank of China Ltd
Original Assignee
Bank of China Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Bank of China Ltd filed Critical Bank of China Ltd
Priority to CN202211487672.0A priority Critical patent/CN115756765A/en
Publication of CN115756765A publication Critical patent/CN115756765A/en
Pending legal-status Critical Current

Links

Images

Landscapes

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

Abstract

The invention discloses a distributed transaction processing method and a distributed transaction processing device, which can be used in the financial field or other technical fields. The distributed transaction processing method comprises the following steps: sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence; when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data. The apparatus is configured to perform the method. The distributed transaction processing method and the device provided by the embodiment of the invention can realize the distributed transaction completely based on the microservice, do not need any additional service participation of other non-business functions, and do not need additional database support, so the processing performance is very high, and the invasion to the application logic is very small.

Description

Distributed transaction processing method and device
Technical Field
The invention relates to the technical field of computers, in particular to a distributed transaction processing method and device.
Background
In a distributed microservice architecture, each service generally has its own independent database, while one main business process generally requires multiple microservices to jointly complete business functions, and a single database transaction cannot solve the consistency of transactions across databases and microservices, which requires the distributed transactions to coordinate distributed transaction management. Saga is a solution for distributed transaction, and the industry also has many implementation components based on Saga distributed transaction, but they are generally complex and highly intrusive to applications.
Disclosure of Invention
Embodiments of the present invention provide a distributed transaction processing method and apparatus, so that a distributed transaction can be completely implemented based on a microservice itself, without any additional service participation of other non-business functions, and without an additional database for support, so that the processing performance is very high, and the intrusiveness to application logic is very small.
In one aspect, the present invention provides a distributed transaction processing method, including:
sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence;
when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved;
and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
In some embodiments, said sequentially executing each sub-transaction in the distributed transaction according to the preset sub-transaction execution order includes:
and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
In some embodiments, when executing each sub-transaction, if the sub-transaction has a preset characteristic, the saving the execution data of the sub-transaction includes:
when executing each sub-transaction, if the interface implementation class of the sub-transaction is marked with the sub-transaction tangent plane annotation, the execution data of the sub-transaction is saved.
In some embodiments, when executing each sub-transaction, if the sub-transaction has a preset characteristic, the saving the execution data of the sub-transaction includes:
when each sub-transaction is executed, if the sub-transaction has the preset characteristic, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
In some embodiments, if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the saved execution data and the saving order of the saved execution data includes:
and if the sub-transaction execution is abnormal, sequentially acquiring the executed data of the executed sub-transaction from the stack structure object of the local thread variable according to the last-in first-out sequence to perform transaction compensation on the distributed transaction.
In some embodiments, the stack structure object of the local thread variable is initialized according to the cut-plane annotation of the main transaction of the distributed transaction before each sub-transaction in the distributed transaction is executed in sequence according to a preset sub-transaction execution order.
In another aspect, the present invention provides a distributed transaction processing apparatus, including:
the execution module is used for sequentially executing each sub-transaction in the distributed transactions according to a preset sub-transaction execution sequence;
the storage module is used for storing the execution data of each sub-transaction if the sub-transaction has the preset characteristics when executing the sub-transaction;
and the compensation module is used for performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data if the sub-transaction is abnormal in execution.
In some embodiments, the execution module is specifically configured to:
and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
In some embodiments, the saving module is specifically configured to:
when executing each sub-transaction, if the interface implementation class of the sub-transaction marks the annotation of the sub-transaction section, the execution data of the sub-transaction is saved.
In some embodiments, the saving module is specifically configured to:
when each sub-transaction is executed, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
In some embodiments, the compensation module is specifically configured to:
and if the sub-transaction execution is abnormal, acquiring the execution data of the executed sub-transaction from the stack structure object of the local thread variable in sequence according to a last-in first-out sequence to perform transaction compensation on the distributed transaction.
In some embodiments, the stack structure object of the local thread variable is initialized according to the cut-plane annotation of the main transaction of the distributed transaction before each sub-transaction in the distributed transaction is executed in sequence according to a preset sub-transaction execution order.
The embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and when the processor executes the computer program, the method according to any of the above embodiments is implemented.
An embodiment of the present invention further provides a computer-readable storage medium, where a computer program is stored, and when the computer program is executed by a processor, the computer program implements the method described in any of the above embodiments.
An embodiment of the present invention further provides a computer program product, where the computer program product includes a computer program, and when the computer program is executed by a processor, the computer program implements the method according to any of the above embodiments.
According to the distributed transaction processing method and device provided by the embodiment of the invention, each sub-transaction in the distributed transaction is sequentially executed according to the preset sub-transaction execution sequence; when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data. Therefore, the distributed transaction can be realized completely based on the microservice, any additional service participation of other non-business functions is not needed, and additional databases are not needed for support, so that the processing performance is high, and the invasiveness to the application logic is small.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts. In the drawings:
fig. 1 is a flowchart illustrating a distributed transaction processing method according to an embodiment of the present invention.
Fig. 2 is a schematic structural diagram of a distributed transaction processing system according to an embodiment of the present invention.
Fig. 3 is a schematic structural diagram of a distributed transaction processing apparatus according to an embodiment of the present invention.
Fig. 4 is a schematic physical structure diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. The exemplary embodiments and descriptions of the present invention are provided to explain the present invention, but not to limit the present invention. It should be noted that the embodiments and features of the embodiments in the present application may be arbitrarily ordered with each other without conflict.
The terms "first," "second," "8230," "8230," and the like as used herein do not particularly denote any order or sequence, nor are they intended to limit the invention, but rather are used to distinguish one element from another or from another element described in the same technical term.
As used herein, the terms "comprising," "including," "having," "containing," and the like are open-ended terms that mean including, but not limited to.
As used herein, "and/or" includes any or all of the ordering of the described things.
For a better understanding of the present invention, the following detailed description is made of the research background of the present invention:
the Saga concept: a Paper Sacas was published in 1987 by Hector Garcia-Molina and Kenneth Salem at Princeton university, which teaches how to handle long live transactions. Saga is a set of sub-transactions that can be broken up into interleaved runs, where each sub-transaction is a real transaction that maintains database consistency. Each Saga consists of a series of Sub-transactions Ti, each Ti having a corresponding compensation action Ci for undoing the result caused by the Ti, and has no "reserve" action, its Ti being submitted directly to the library. There are two execution sequences of Saga, one being T1, T2, T3,. The other is T1, T2, ·, tj, cj,. And C2, C1, where 0-j-n, i.e. failure in execution to the jth sub-transaction, sequentially compensates the successfully executed sub-transactions from the jth sub-transaction onwards. The advantages of Saga are no lock wait, very high performance handling; the disadvantage is that atomicity and isolation of long transactions are not supported.
An open-source distributed transaction solution Seata in Ali provides a Saga model implemented based on a state machine engine, wherein the state machine is generally a service which needs to be additionally and independently deployed and needs an additional database table (3 transaction tables) with non-business functions for support; the sub-transactions are distributed transactions composed by means of service orchestration. Seata's such approach requires a separate service like a state machine to support recording of sub-transaction execution states during long transaction execution to decide whether to perform compensation operations, and processing performance is not optimal. Moreover, in order to support the final consistency compensation, an additional database table is generally needed to support the distributed transaction, so the development cost and the operation and maintenance cost of the implementation are relatively high.
Based on this, the invention provides a distributed transaction processing method, which has no state machine service, all the execution processes are stored in the memory object of the micro-service, and the compensation operation is also completed by the micro-service of the main transaction through asynchronization, so the processing performance is better. The invention advocates that all distributed transaction functions are realized by the microservice of the main business logic (generally, microservices processed by the first function), and all sub-transaction microservices are insensitive to the distributed transactions, so that the invasiveness to the application is small. The method is more excellent than the existing scheme in the aspects of performance processing, development, operation and maintenance cost and the like.
The execution subject of the distributed transaction processing method provided by the embodiment of the invention includes but is not limited to a computer. Specifically, a command/coordination manner may be adopted: the Saga decision and execution order logic is integrated into a Saga controller (implemented as a distributed transaction manager function, to differentiate between database transaction managers, referred to herein as the Saga controller) that communicates with each service in a call/reply manner telling them which operations should be performed. The Saga controller is on the main business service (generally the first invoked micro-service), and other sub-transaction services do not need to care about the technical implementation of any distributed transaction.
Fig. 1 is a schematic flowchart of a distributed transaction processing method according to an embodiment of the present invention, and as shown in fig. 1, the distributed transaction processing method according to the embodiment of the present invention includes:
s101, sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence;
s102, when each sub-transaction is executed, if the sub-transaction has preset characteristics, the execution data of the sub-transaction is saved;
s103, if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
Specifically, the developer needs to define a Saga controller method by self-programming in advance, then program-code the sub-transaction execution logic sequence into the controller method, and mark the preset characteristics on some specific sub-transaction methods. For example, a developer has encoded all sub-transactions into the Saga controller method, in the order of T1, T2,.., tn, and when executing the Saga controller method, a distributed transaction is first opened, and then the sub-transactions T1, T2,. Once, tn are sequentially executed, and each time a sub-transaction Tj is executed, the object Bean and the generic parameter (Tt) of Tj are saved; if all the sub-transactions are successfully executed, emptying the stored data; if the execution fails when the sub-transaction Tj is executed, the asynchronous processing mode is used, the Tj, tj-1.. T1 are sequentially taken out from the backward to the forward mode according to the storage sequence, and the compensation operation of the sub-transaction is sequentially executed.
The distributed transaction realized by the embodiment of the invention is completely realized based on the microservice, does not need any additional service participation of other non-business functions, and does not need additional database support, so the processing performance is very high, and the invasion to the application logic is very small.
In some embodiments, the sequentially executing each sub-transaction in the distributed transaction according to the preset sub-transaction execution order includes: and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
Specifically, in the present embodiment, a generic interface for sub-transactions is defined, and the interface provides two methods, "R execute (Tt)", "void cache (Tt)", where execute is used for submitting a transaction, cache is used for compensating a transaction, and a common generic interface is used to implement unified management of sub-transactions. All sub-transactions participating in a distributed transaction need to implement this interface and are put into the way of the Saga controller, with the order of the sub-transactions defined by the developer. When the Saga controller method is executed, the execute method is executed every time one sub-transaction Tj is executed; when performing the compensation operation for a sub-transaction, a cancel method is performed.
In some embodiments, when executing each sub-transaction, if the sub-transaction has a preset characteristic, the saving the execution data of the sub-transaction includes: when executing each sub-transaction, if the interface implementation class of the sub-transaction is marked with the sub-transaction tangent plane annotation, the execution data of the sub-transaction is saved.
Specifically, the distributed transaction management function is realized by a Spring annotation plane mode. For sub-transaction annotations: the interface method is used for marking the interface of the sub-transaction, and when and only when the interface implementation class of the sub-transaction marks the annotation of the sub-transaction, the execution data of the interface method is saved when the interface method is executed. The annotated tangent plane realizes that when the sub-transaction is executed, whether the distributed transaction is started or not is judged, if the distributed transaction is started, the execution data of the current sub-transaction is saved, otherwise, the sub-transaction does not participate in the distributed transaction management.
In some embodiments, when executing each sub-transaction, if the sub-transaction has a preset characteristic, the saving the execution data of the sub-transaction includes: when each sub-transaction is executed, if the sub-transaction has the preset characteristic, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
Specifically, a stack object, defined as a local thread variable, is pre-initialized to store subsequent sub-transaction execution data. When and only when the interface implementation class of the sub-transaction marks the sub-transaction annotation, the execution data of the sub-transaction is recorded into the stack object of the defined local thread variable in the structural order of the stack when the interface method is executed, otherwise, the sub-transaction annotation does not participate in the distributed transaction management.
In some embodiments, if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the saved execution data and the saving order of the saved execution data includes: and if the sub-transaction execution is abnormal, acquiring the execution data of the executed sub-transaction from the stack structure object of the local thread variable in sequence according to a last-in first-out sequence to perform transaction compensation on the distributed transaction.
Specifically, if no error is reported after the Saga controller method is executed (whether an error is reported is judged by throwing an exception or not), all the sub-transactions are successfully submitted, the stack object of the local thread variable stored in the memory before is cleared, and the main transaction is ended; if the Saga controller throws the exception (the exception is captured by section programming) after the execution of the method is finished, the information of the previous push sub-transaction data is sequentially acquired from the stack object of the local thread variable in the order of the stack to carry out compensation operation, and the order of the transaction compensation from back to front can be realized because the order of 'last in first out' of the stack structure is followed.
In some embodiments, all compensation operations are implemented using asynchronous threads, which may reduce the performance impact on the original transaction processing; all compensation methods need to support idempotent processing, so that theoretically, the compensation methods cannot be failed to execute, if the compensation failure condition still occurs, the compensation times can be preset for carrying out multiple compensation, if the compensation is still unsuccessful after multiple times, manual intervention is generally needed for investigation, and finally, data information which cannot be successfully compensated is output to a specific file or a database so as to be manually checked.
In some embodiments, the stack structure object of the local thread variable is initialized according to the cut-plane annotation of the main transaction of the distributed transaction before each sub-transaction in the distributed transaction is executed in sequence according to a preset sub-transaction execution order.
Specifically, the distributed transaction management function is realized by a Spring comment interface mode. Two annotation facets are required to support the completion of the Saga distributed transaction management work. For example:
the first section annotation "StartSagaTransaction" opens the Master transaction annotation: the distributed transaction is started by an annotation section (the function realization code woven into the section before the annotation method is executed can be realized through section programming, and another part of function realization code woven into the section after the annotation method is executed or when the annotation method throws out an exception is realized), and a stack object defined as a local thread variable is initialized and used for storing the execution data of the subsequent sub-transaction. The developer needs to define a method for the Saga controller to annotate with this annotation to open the distributed transaction.
The second section annotates the "SubTransactional" sub-transaction annotation: the method is used for marking the interface of the sub-transaction, and when and only when the interface implementation class of the sub-transaction marks the annotation of the sub-transaction, the execution data of the interface method is recorded into the stack object of the local thread variable defined in the last step according to the structural sequence of the stack. The annotation section judges whether the distributed transaction is started or not every time when the sub-transaction is executed, if the distributed transaction is started, the current bean object and the parameters thereof are pushed into the stack object of the local thread variable, otherwise, the current bean object does not participate in the distributed transaction management.
In order to better understand the present invention, a distributed transaction processing method proposed by the present invention is described below with a specific embodiment.
In this embodiment, all functions of the Saga controller are realized by annotating the tangent plane, and developers only need to define and develop the method of the Saga controller and the business function realized by the sub-affairs according to the rules of the business function. In general, the remote methods (i.e. other micro services) called by the sub-transaction all need to provide corresponding compensation methods, and the compensation methods for the remote micro services do not require specific interface implementation, so that the intrusiveness on the application is small. But all compensation methods or services need to support idempotent processing, i.e. to support repeated execution of the compensation methods or services. The details are as follows:
as shown in fig. 2, a distributed transaction management system provided in an embodiment of the present invention mainly includes 3 modules: saga controller 21, sub-transaction management module 22, compensation management module 23:
the Saga controller 21 realizes the distributed transaction management function by a Spring annotation tangent plane mode. Two annotation planes are required to support the completion of the Saga distributed transaction management work.
The first section annotation "StartSagaTransaction" opens the Master transaction annotation: the distributed transaction is started by an annotation section (the function realization code woven into the section before the annotation method is executed can be realized through section programming, and another part of function realization code woven into the section after the annotation method is executed or when the annotation method throws out an exception is realized), and a stack object defined as a local thread variable is initialized and used for storing the execution data of the subsequent sub-transaction. The developer needs to define a method for the Saga controller to annotate with this annotation to open the distributed transaction.
The second section annotates the "SubTransactional" sub-transaction annotation: the method is used for marking the interface of the sub-transaction, and when and only when the interface implementation class of the sub-transaction marks the annotation of the sub-transaction, the execution data of the interface method is recorded into the stack object of the local thread variable defined in the last step according to the structural sequence of the stack. The annotation section judges whether the distributed transaction is started or not every time when the sub-transaction is executed, if the distributed transaction is started, the current bean object and the parameters thereof are pushed into the stack object of the local thread variable, otherwise, the current bean object does not participate in the distributed transaction management.
The developer needs to define a Saga controller method by self-programming, then program the execution logic sequence of the sub-transaction into the controller method, and mark "startsaga transaction" on the Saga controller method to open the main transaction note. All sub-transaction implementations need to follow the next module "sub-transaction management module".
The sub-transaction management module 22: each sub-transaction requires the use of a separate bean object and implements a common sub-transaction generic interface that provides two methods, "R execute (Tt)", "void cancel (Tt)", execute for committing the transaction and cancel for transaction compensation. The common generic interface is used for realizing the uniform management of the sub-transactions, and the 'sub-transaction annotation' is marked on the submitted transaction execute method by default. Then, through the above-mentioned section description, when the business logic executes to a sub-transaction, the bean object, method parameter, execution state, etc. executed by the sub-transaction will be first pushed into the stack object of the predefined local thread variable.
The compensation management module 23: for deciding whether to perform a transaction compensation function. If the Saga controller method does not report errors after the execution is finished (whether the error is reported is judged by throwing the exception or not), all the sub-transactions are represented to be successfully submitted, the stack object of the local thread variable stored in the memory before is emptied, and the main transaction is finished; if the Saga controller throws the exception (the exception is captured by section programming) after the execution is finished, the information of the previous push sub-transaction data is sequentially acquired from the stack object of the local thread variable in the order of the stack to carry out the compensation operation, and the order of the transaction compensation from back to front can be realized because the order of 'last in first out' of the stack structure is followed. All compensation operations are realized by using asynchronous threads, so that the performance influence on the original transaction processing can be reduced; all compensation methods need to support idempotent processing, so that theoretically, the compensation methods cannot be failed to execute, if the compensation failure condition still occurs, the compensation times can be preset for carrying out multiple compensation, if the compensation is still unsuccessful after multiple times, manual intervention is generally needed for investigation, and finally, data information which cannot be successfully compensated is output to a specific file or a database so as to be manually checked.
The distributed transaction annotation tangent plane is programmed and realized, the coordination management functions of all distributed transactions are realized in the tangent plane, no influence is caused on business function codes, the decoupling with business logic can be realized, and the development cost of programmers can be greatly reduced.
The invention realizes all distributed transaction management on the service of the main transaction, basically noninductive to the sub-transaction formed by the remote service needing to be called, realizes the logic decoupling of the inter-service transaction in the development process, and provides great convenience for a plurality of development groups to develop organization structures of different micro-services in parallel.
The following describes in detail the distributed transaction processing method provided by the present invention in terms of actually executed transaction steps:
this is performed as long as the developer has encoded all the sub-transactions into the Saga controller method, in the order T1, T2.
Step 1: and starting a distributed transaction (only by using a variable identifier of a bootean type), and initializing a stack structure object of a local thread variable.
Step 2: when the sub-transaction T1 is executed, whether the distributed transaction is started or not is judged. If the distributed transaction is started, pushing the object bean and the method parameter of the T1 into the stack object of the local thread variable in the previous step; if the distributed transaction is not opened, the result indicates that T1 is not included in the distributed transaction for management, and there are no subsequent transaction management operations. The transaction commit method of T1 is performed upon completion. And then sequentially executing T2, the other two, tn in the same way, jumping to the step 5 if all execution is successful, and jumping to the step 3 if the execution fails to Tj.
And step 3: and creating an independent asynchronous thread and executing transaction compensation. And sequentially acquiring the data of Tj, tj-1, the T1 from the stack object of the local thread variable according to the structure sequence of the stack, and sequentially executing the compensation operation. Because the compensation method must support idempotent (repeated) operations, it should generally be successful. And if the special case execution fails, jumping to the step 4, otherwise jumping to the step 5.
And 4, step 4: the maximum execution compensation times can be preset, and compensation operation which fails to be executed can be compensated for multiple times; if the compensation fails, serializing the sub-transaction data which is not compensated, and outputting the serialized sub-transaction data to a file or a database, wherein the data of the part needs manual intervention and manual data adjustment after being checked. Manual troubleshooting and manual data adjustment depend on the specific business scenario and are therefore outside the scope of the present invention.
And 5: and completing the distributed transaction, and clearing the local thread variable stack object in the memory.
The distributed transaction processing method provided by the embodiment of the invention at least has the following advantages:
(1) The coordination management work of all distributed transactions is realized through an annotation tangent plane mode, the decoupling of the distributed transaction function and the normal business logic function is realized, the remote sub-transactions can not even care about the transaction management, the invasion influence on the business logic is reduced, and the development cost of programmers can be greatly reduced on software development.
(2) And any independent middleware service, database and the like are not used for supporting distributed transactions, so that the software and hardware resource cost and the operation and maintenance cost are greatly reduced, and the operation and maintenance are simpler.
(3) The distributed transaction function realized by the invention is completely realized based on the memory of the microserver, and the processing performance is very high-efficient because the storage structure is simple and the large memory overhead is not increased.
Fig. 3 is a schematic structural diagram of a distributed transaction processing apparatus according to an embodiment of the present invention, and as shown in fig. 3, the distributed transaction processing apparatus according to the embodiment of the present invention includes:
the execution module 31 is configured to sequentially execute each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence;
a saving module 32, configured to, when executing each sub-transaction, if the sub-transaction has a preset characteristic, save the execution data of the sub-transaction;
and a compensation module 33, configured to perform transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data if the sub-transaction is abnormal in execution.
The distributed transaction processing device provided by the embodiment of the invention sequentially executes each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence; when each sub-transaction is executed, if the sub-transaction has preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data. Therefore, the distributed transaction can be completely realized based on the microservice, any additional service participation of other non-business functions is not needed, and no additional database is needed for supporting, so that the processing performance is high, and the invasiveness to the application logic is small.
In some embodiments, the execution module is specifically configured to:
and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
In some embodiments, the saving module is specifically configured to:
when executing each sub-transaction, if the interface implementation class of the sub-transaction marks the annotation of the sub-transaction section, the execution data of the sub-transaction is saved.
In some embodiments, the saving module is specifically configured to:
when each sub-transaction is executed, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
In some embodiments, the compensation module is specifically configured to:
and if the sub-transaction execution is abnormal, sequentially acquiring the executed data of the executed sub-transaction from the stack structure object of the local thread variable according to the last-in first-out sequence to perform transaction compensation on the distributed transaction.
In some embodiments, the stack structure object of the local thread variable is initialized according to the tangent-plane annotation of the main transaction of the distributed transaction before each sub-transaction in the distributed transaction is sequentially executed according to a preset sub-transaction execution order.
Embodiments of the apparatus provided in the embodiments of the present invention may be specifically configured to execute the processing flow of each of the embodiments of the distributed transaction processing method, and the functions of the embodiments are not described herein again, and refer to the detailed description of the embodiments of the method.
It should be noted that the distributed transaction processing method and apparatus provided in the embodiment of the present invention may be used in the financial field, and may also be used in any technical field other than the financial field.
Fig. 4 is a schematic physical structure diagram of an electronic device according to an embodiment of the present invention, and as shown in fig. 4, the electronic device may include: a processor (processor) 401, a communication Interface (communication Interface) 402, a memory (memory) 403 and a communication bus 404, wherein the processor 401, the communication Interface 402 and the memory 403 complete communication with each other through the communication bus 404. Processor 401 may call logic instructions in memory 403 to perform a method as described in any of the above embodiments, including, for example: sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence; when each sub-transaction is executed, if the sub-transaction has preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
In addition, the logic instructions in the memory 403 may be implemented in the form of software functional units and stored in a computer readable storage medium when the software functional units are sold or used as independent products. Based on such understanding, the technical solution of the present invention or a part thereof which substantially contributes to the prior art may be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
The present embodiment discloses a computer program product comprising a computer program stored on a non-transitory computer readable storage medium, the computer program comprising program instructions which, when executed by a computer, enable the computer to perform the method provided by the above-mentioned method embodiments, for example, comprising: sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence; when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
The present embodiment provides a computer-readable storage medium, which stores a computer program, where the computer program causes the computer to execute the method provided by the above method embodiments, for example, the method includes: sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence; when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved; and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In the description herein, reference to the description of the terms "one embodiment," "a particular embodiment," "some embodiments," "for example," "an example," "a particular example," or "some examples," etc., means that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the invention. In this specification, the schematic representations of the terms used above do not necessarily refer to the same embodiment or example. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples.
The above-mentioned embodiments are provided to further explain the objects, technical solutions and advantages of the present invention in detail, and it should be understood that the above-mentioned embodiments are only examples of the present invention and should not be used to limit the scope of the present invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (15)

1. A distributed transaction processing method, comprising:
sequentially executing each sub-transaction in the distributed transaction according to a preset sub-transaction execution sequence;
when executing each sub-transaction, if the sub-transaction has the preset characteristics, the execution data of the sub-transaction is saved;
and if the sub-transaction execution is abnormal, performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data.
2. The method of claim 1, wherein executing each sub-transaction in the distributed transaction in turn according to the preset execution order of the sub-transactions comprises:
and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
3. The method of claim 2, wherein when executing each sub-transaction, if the sub-transaction has a predetermined characteristic, the saving the execution data of the sub-transaction comprises:
when executing each sub-transaction, if the interface implementation class of the sub-transaction marks the annotation of the sub-transaction section, the execution data of the sub-transaction is saved.
4. The method according to any one of claims 1 to 3, wherein, when executing each sub-transaction, if the sub-transaction has a preset characteristic, the saving the execution data of the sub-transaction comprises:
when each sub-transaction is executed, if the sub-transaction has the preset characteristic, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
5. The method of claim 4, wherein performing transaction compensation processing on the distributed transaction according to the saved execution data and the saving order of the saved execution data if the sub-transaction execution is abnormal comprises:
and if the sub-transaction execution is abnormal, sequentially acquiring the executed data of the executed sub-transaction from the stack structure object of the local thread variable according to the last-in first-out sequence to perform transaction compensation on the distributed transaction.
6. The method of claim 4, wherein the stack structure object of the local thread variable is initialized according to a tangent-plane annotation of a main transaction of the distributed transaction before each sub-transaction in the distributed transaction is executed in sequence according to a preset sub-transaction execution order.
7. A distributed transaction processing apparatus, comprising:
the execution module is used for sequentially executing each sub-transaction in the distributed transactions according to a preset sub-transaction execution sequence;
the storage module is used for storing the execution data of each sub-transaction if the sub-transaction has the preset characteristics when executing the sub-transaction;
and the compensation module is used for performing transaction compensation processing on the distributed transaction according to the stored execution data and the storage sequence of the stored execution data if the sub-transaction is abnormal in execution.
8. The apparatus of claim 7, wherein the execution module is specifically configured to:
and sequentially calling the method in the interface implementation class of each sub-transaction in the distributed transaction to execute the sub-transaction according to a preset sub-transaction execution sequence.
9. The apparatus of claim 8, wherein the saving module is specifically configured to:
when executing each sub-transaction, if the interface implementation class of the sub-transaction marks the annotation of the sub-transaction section, the execution data of the sub-transaction is saved.
10. The apparatus according to any one of claims 7 to 9, wherein the saving module is specifically configured to:
when each sub-transaction is executed, if the sub-transaction has the preset characteristic, the execution data of the sub-transaction is written into a stack structure object of a local thread variable which is initialized in advance.
11. The apparatus of claim 10, wherein the compensation module is specifically configured to:
and if the sub-transaction execution is abnormal, acquiring the execution data of the executed sub-transaction from the stack structure object of the local thread variable in sequence according to a last-in first-out sequence to perform transaction compensation on the distributed transaction.
12. The apparatus of claim 10, wherein the stack structure object of the local thread variable is initialized according to a tangent-plane annotation of a main transaction of the distributed transaction before each sub-transaction in the distributed transaction is executed in turn according to a preset sub-transaction execution order.
13. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the method of any of claims 1 to 6 when executing the computer program.
14. A computer-readable storage medium, characterized in that the computer-readable storage medium stores a computer program which, when executed by a processor, implements the method of any one of claims 1 to 6.
15. A computer program product, characterized in that the computer program product comprises a computer program which, when being executed by a processor, carries out the method of any one of claims 1 to 6.
CN202211487672.0A 2022-11-25 2022-11-25 Distributed transaction processing method and device Pending CN115756765A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211487672.0A CN115756765A (en) 2022-11-25 2022-11-25 Distributed transaction processing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211487672.0A CN115756765A (en) 2022-11-25 2022-11-25 Distributed transaction processing method and device

Publications (1)

Publication Number Publication Date
CN115756765A true CN115756765A (en) 2023-03-07

Family

ID=85337683

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211487672.0A Pending CN115756765A (en) 2022-11-25 2022-11-25 Distributed transaction processing method and device

Country Status (1)

Country Link
CN (1) CN115756765A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115964198A (en) * 2023-03-17 2023-04-14 北京徐工汉云技术有限公司 Distributed flexible transaction processing method and device based on long transaction

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115964198A (en) * 2023-03-17 2023-04-14 北京徐工汉云技术有限公司 Distributed flexible transaction processing method and device based on long transaction

Similar Documents

Publication Publication Date Title
Whittle et al. MATA: A unified approach for composing UML aspect models based on graph transformation
US7472379B2 (en) Flexible navigation of a workflow graph
EP0554854A2 (en) System and method for executing, tracking and recovering long running computations
JPH0683841A (en) Work flow management system and method
CN111400011B (en) Real-time task scheduling method, system, equipment and readable storage medium
CN110413428B (en) Account checking data processing method, electronic equipment and storage medium
CN111752957A (en) Sale locking method and system based on caching
DE4216871A1 (en) EXECUTIVE RULES TO ENSURE SERIAL PRODUCTION OF DISTRIBUTED TRANSACTIONS
CN109684057A (en) Task processing method and device and storage medium
US20060004882A1 (en) Custom Atomic Transactions in Programming Environments
CN114253673A (en) Transaction processing method and transaction processing device of distributed system
CN115756765A (en) Distributed transaction processing method and device
US10768974B2 (en) Specifying an order of a plurality of resources in a transaction according to distance
CN112905613A (en) Data bidirectional synchronization method and device for heterogeneous database
CN112148436B (en) Decentralised TCC transaction management method, device, equipment and system
Gaaloul et al. Mining workflow recovery from event based logs
JP2004164594A (en) Method and system for managing long-running transaction
CN113052707A (en) Application production method and device, computer equipment and storage medium
CN115934272A (en) Online batch task processing method and device
CN110874713A (en) Service state management method and device
CN112381650B (en) Cross-chain interoperation transaction processing method, device, electronic equipment and storage medium
CN113900840A (en) Distributed transaction final consistency processing method and device
CN109901933B (en) Operation method and device of business system, storage medium and electronic device
CN111538491A (en) Data event processing method, device, equipment and storage medium
CN113448493A (en) Method, electronic device and computer program product for backing up data

Legal Events

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