Distributed transaction processing method and device, equipment and storage medium
By determining the execution order and status query of branch transactions in a distributed transaction, the problems of low efficiency and low reliability in distributed transaction processing are solved, and efficient processing and consistency guarantee are achieved in the case of branch transaction timeout.
Patent Information
- Application Number
- CN202310622704.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-29
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2043-05-29
AI Technical Summary
Existing distributed transaction processing methods suffer from low processing efficiency and low reliability. In particular, during the process of banking institutions migrating from mainframe applications to microservice systems, the lack of idempotency of mainframe functions can cause distributed transactions to be suspended, increasing inconsistency and extending the project time.
By determining the execution order of multiple branch transactions of the target distributed transaction, each branch transaction is executed one by one, and the next branch transaction continues when the branch transaction succeeds, and the previous branch transaction is compensated in reverse when it fails; when the branch transaction call times out, the query interface is used to determine the current status and, if necessary, the transaction is compensated in reverse or suspended to avoid moving all functions down to the host.
It improves the processing efficiency and reliability of distributed transactions, reduces the probability of inconsistent transactions, and increases the success rate of distributed transaction processing.
Smart Images

Figure CN116700963B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of distributed technology, and in particular to a method, apparatus, device, and storage medium for processing distributed transactions. Background Technology
[0002] A transaction is a logical unit in database execution, consisting of a finite sequence of operations. The operations in this sequence either all succeed or all fail. With the rise of distributed microservice architectures, enterprises have already split or are in the process of service-oriented database architecture. Completing a business function under a microservice architecture may require crossing multiple services. These services can operate on their respective business databases, and all services must either succeed or fail simultaneously; otherwise, data inconsistency will occur. Ensuring the consistency of business data across these services involves distributed transactions.
[0003] Existing technologies often employ SAGA to handle long-lived distributed transactions, decomposing a distributed transaction into multiple branch transactions. Each branch transaction has corresponding forward and reverse interfaces. When any branch transaction fails, the reverse interface is called to reverse the successfully executed branch transaction. Microservice architectures require microservices participating in distributed transactions to achieve business idempotency to avoid duplicate execution of business logic due to retries after service call timeouts. However, during the phased migration of mainframe applications to microservice systems in banking institutions, the coexistence of mainframe applications and microservice systems leads to instances where microservice business processes call mainframe business functions. Since mainframe functions do not guarantee business idempotency, and non-idempotent distributed transactions cannot be re-called, even if a non-idempotent distributed transaction succeeds after a retry, it will still return a failure result, causing the distributed transaction to be suspended. Existing technologies, during the migration of mainframe applications, aim to avoid mainframe application functions participating in distributed transactions within the microservice application system, or immediately interrupt transaction execution and suspend the distributed transaction after a mainframe business function participating in a distributed transaction times out.
[0004] Because the solution design avoids involving host business functions in distributed transactions, all host functions involved in distributed transactions need to be moved down to the microservice system. This expands the scope of the move, increases the workload of each batch of move-down, prolongs the project timeline, and is not conducive to the overall planning of digital transformation. Furthermore, immediately suspending the distributed transaction when a service call times out will increase the number of inconsistent distributed transactions, reduce the overall success rate of distributed transaction consistency assurance, and affect the reliability of the distributed transaction device. Therefore, the existing distributed transaction processing methods have the technical problems of low processing efficiency and low reliability. Summary of the Invention
[0005] This application provides a method, apparatus, device, and storage medium for processing distributed transactions, in order to solve the problems of low processing efficiency and low reliability in existing distributed transaction processing methods.
[0006] Firstly, this application provides a method for processing a target distributed transaction, comprising:
[0007] Based on the target distributed transaction, determine the execution order of multiple branch transactions corresponding to the target distributed transaction; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution;
[0008] In response to a business request, initiate the target distributed transaction and create the target distributed transaction pipeline;
[0009] Based on the execution order, determine the branch transactions to be executed one by one, create the branch transaction pipeline of the branch transactions, execute the branch transactions, return the status code representing the execution result after the branch transaction is completed, and update the corresponding branch transaction pipeline based on the status code;
[0010] When a branch transaction succeeds, the next branch transaction is executed according to the execution order; when a branch transaction fails, the previous branch transaction is reversed according to the execution order to undo the operations performed when the previous branch transaction was executed.
[0011] When a branch transaction calls a service and times out, it is determined whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution status of the branch transaction is determined based on the query interface corresponding to the branch transaction; if so, and the current execution status is successful, the previous branch transaction is compensated in reverse order.
[0012] Optionally, when a branch transaction service call times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the method further includes:
[0013] When the current execution status is successful, the forward interface of the microservice corresponding to the branch transaction executes the next branch transaction according to the execution order.
[0014] Optionally, when a branch transaction service call times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the method further includes:
[0015] When the current execution status is execution failure, the reverse interface of the microservice corresponding to the branch transaction compensates the previous branch transaction in reverse order according to the execution order, so as to undo the operations performed when executing the previous branch transaction.
[0016] Optionally, upon successful execution of a branch transaction, the system returns an execution result indicating the success of the branch transaction and executes the next branch transaction based on the execution order, including:
[0017] When a branch transaction is executed successfully, the next branch transaction is determined based on the execution order.
[0018] The next branch transaction is executed based on the forward interface of the microservice corresponding to the next branch transaction.
[0019] Optionally, when a branch transaction fails, the previous branch transaction is compensated in reverse order based on the execution order, including:
[0020] When a branch transaction fails, the previous branch transaction is determined based on the execution order.
[0021] The reverse interface of the microservice corresponding to the previous branch transaction compensates for the previous branch transaction.
[0022] Optionally, it determines whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and determines the current execution state of the branch transaction based on the query interface corresponding to the branch transaction, including:
[0023] Determine whether the execution interface of the microservice corresponding to the branch transaction is a positive interface;
[0024] If so, the current execution status of the branch transaction is determined based on the forward query interface of the microservice corresponding to the branch transaction, and it is determined that the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order.
[0025] If not, the current execution status of the branch transaction is determined based on the reverse query interface of the service corresponding to the branch transaction, and the branch transaction is determined to be in the state of reverse compensation of the previous branch transaction based on the execution order.
[0026] Optionally, when the branch transaction call service times out, the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution state is execution failure, the method further includes: suspending the target distributed transaction.
[0027] Secondly, this application provides a distributed transaction processing apparatus, comprising:
[0028] The determination module is used to determine the execution order of multiple branch transactions corresponding to the target distributed transaction based on the target distributed transaction; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from executing multiple times are different from those from executing once;
[0029] The startup module is used to respond to business requests, start the target distributed transaction, and create the target distributed transaction pipeline.
[0030] The first processing module is used to determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, execute the branch transactions, return a status code representing the execution result after the branch transaction is executed, and update the corresponding branch transaction pipeline based on the status code.
[0031] The second processing module is used to execute the next branch transaction based on the execution order when the branch transaction is executed successfully; and to reverse the previous branch transaction based on the execution order when the branch transaction fails, so as to undo the operations performed when the previous branch transaction was executed.
[0032] The third processing module is used to determine whether the branch transaction is in the state of reverse compensation of the previous branch transaction based on the execution order when the branch transaction call service times out, and to determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; if so, and the current execution status is successful, the previous branch transaction is compensated in reverse order.
[0033] Optionally, when a branch transaction call to the service times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the device is further configured to:
[0034] When the current execution status is successful, the forward interface of the microservice corresponding to the branch transaction executes the next branch transaction according to the execution order.
[0035] Optionally, when a branch transaction call to the service times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the device is further configured to:
[0036] When the current execution status is execution failure, the reverse interface of the microservice corresponding to the branch transaction compensates the previous branch transaction in reverse order according to the execution order, so as to undo the operations performed when executing the previous branch transaction.
[0037] Optionally, the device is also used for:
[0038] When a branch transaction is executed successfully, the next branch transaction is determined based on the execution order.
[0039] The next branch transaction is executed based on the forward interface of the microservice corresponding to the next branch transaction.
[0040] Optionally, the device is also used for:
[0041] When a branch transaction fails, the previous branch transaction is determined based on the execution order.
[0042] The reverse interface of the microservice corresponding to the previous branch transaction compensates for the previous branch transaction.
[0043] Optionally, the device is also used for:
[0044] Determine whether the execution interface of the microservice corresponding to the branch transaction is a positive interface;
[0045] If so, the current execution status of the branch transaction is determined based on the forward query interface of the microservice corresponding to the branch transaction, and it is determined that the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order.
[0046] If not, the current execution status of the branch transaction is determined based on the reverse query interface of the service corresponding to the branch transaction, and the branch transaction is determined to be in the state of reverse compensation of the previous branch transaction based on the execution order.
[0047] Optionally, when a branch transaction call service times out, the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution state is execution failure, the device is also used to: suspend the target distributed transaction.
[0048] A third aspect of this application provides a distributed transaction processing device, comprising:
[0049] Processor and memory;
[0050] The memory stores instructions that the computer executes;
[0051] The processor executes computer execution instructions stored in memory, causing the distributed transaction processing device to perform the distributed transaction processing method of any one of the first aspects.
[0052] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement a distributed transaction processing method as described in any of the first aspects.
[0053] Fifthly, this application provides a program product comprising a computer program that is executed by a processor to implement the distributed transaction processing method as described in the first aspect.
[0054] The distributed transaction processing method, apparatus, device, and storage medium provided in this application determine whether a branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order when the corresponding branch transaction call service of the target distributed transaction times out through a query interface, and determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; when the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution status is successful, reverse compensation of the previous branch transaction is performed based on the execution order; when the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution status is failure, the means of suspending the target distributed transaction do not require involving distributed transactions. By offloading all mainframe functions of transactions to the microservice system, the processing efficiency of distributed transactions is improved. This allows for determining the current execution status of a branch transaction when it times out, and continuing execution if the branch transaction succeeds. Conversely, if a branch transaction fails, reverse compensation is triggered for branches executing in the forward order, suspending only those branches in the reverse compensation state that have failed. This reduces the probability of inconsistent distributed transactions, improves consistency and reliability, and increases the success rate of distributed transaction processing. Therefore, it achieves the technical effect of improving the efficiency and reliability of distributed transaction processing. Attached Figure Description
[0055] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0056] Figure 1 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 1 ;
[0057] Figure 2 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 2 ;
[0058] Figure 3 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 3 ;
[0059] Figure 4 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 4 ;
[0060] Figure 5 A schematic diagram of the structure of a distributed transaction processing device provided in an embodiment of this application;
[0061] Figure 6 This is a schematic diagram of the structure of a distributed transaction processing device provided in an embodiment of this application.
[0062] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0063] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0064] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with relevant laws, regulations and standards, and corresponding operation entry points are provided for users to choose to authorize or refuse.
[0065] It should be noted that the distributed transaction processing method and apparatus of this application can be used in the field of distributed technology, and can also be used in any field other than the financial field. The application field of the distributed transaction processing method and apparatus of this application is not limited.
[0066] First, let me explain the terms used in this application:
[0067] Distributed transactions refer to transactions in a distributed microservice architecture where participants, servers supporting transactions, resource servers, and transaction managers are distributed across different machine nodes and typically belong to different applications. Distributed transactions must ensure that all operations on different machine nodes either succeed or fail.
[0068] SAGA refers to an open-source distributed transaction solution for handling long-lived distributed transactions. SAGA decomposes a distributed transaction into multiple branch transactions. Each branch transaction in the distributed transaction returns an execution result indicating whether the branch transaction was successful or failed. When a branch transaction of the distributed transaction fails, the successfully executed branch transactions are compensated in turn.
[0069] Transaction coordinator: This refers to the program module that coordinates and controls the rollback and state transition of distributed transactions. It is used to coordinate the execution process of transactions in distributed transactions, including the opening and commit of distributed transactions, the opening and commit of branch transactions, and compensation for the previous branch transaction when the branch transaction returns an execution result indicating execution failure. In the entire process of coordinating transaction processing, the transaction coordinator calls the transaction manager to create and update the pipeline of distributed transactions and corresponding branch transactions.
[0070] The timeout control transaction compensation trigger module refers to the process of calling the query interface of the timed-out call when a branch transaction call in a distributed transaction times out, and judging the execution result of the branch transaction based on the query result, and coordinating whether the branch transaction is executed in the forward direction or enters the compensation phase.
[0071] Aspect-Oriented Programming (AOP) is a technique that uses pre-compilation and runtime dynamic proxies to achieve unified maintenance of program functionality. Based on Spring annotations, it defines distributed transaction annotations @GlobalTransaction and branch transaction annotations @BranchTransaction, and uses AOP to integrate the logic for starting, committing, and rolling back distributed transactions into the business execution flow.
[0072] Annotations, also known as metadata, are code-level descriptions that can be declared before packages, classes, fields, methods, local variables, method parameters, etc., to describe and comment on these elements. Java provides some native annotations, such as @Override and @Deprecated.
[0073] Custom annotations: These are annotations defined by users using Java annotations to meet their own needs.
[0074] In existing technologies, SAGA is used to split distributed transactions into multiple branch transactions. Each branch transaction has a corresponding forward interface and a reverse interface. When any branch transaction fails, the successfully executed branch transaction is reorganized by calling the reverse interface. In order to avoid the problem of repeated execution of business due to retrying after the service call corresponding to the branch transaction times out, the microservice of distributed transactions needs to implement business idempotency. During the phased migration of mainframe applications to microservice systems, banking institutions face the coexistence of mainframe applications and microservice systems. This leads to instances where microservice business processes call mainframe business functions, and since mainframe functions do not guarantee idempotency, they cannot be repeatedly invoked if they participate in distributed transactions. To address the issue of not being able to retry after a timeout in an idempotent branch transaction service call, existing technologies employ a design approach that avoids mainframe application functions from participating in distributed transactions or immediately interrupts the process and suspends the distributed transaction upon its inception. However, this design may require migrating all mainframe functions involved in distributed transactions to the microservice system, expanding the migration scope, increasing the workload per batch, extending the project timeline, and hindering the overall digital transformation plan. Furthermore, immediately suspending distributed transactions upon service call timeouts increases the number of inconsistent distributed transactions, reduces the overall success rate of distributed transaction consistency assurance, and affects the reliability of the distributed transaction mechanism. Therefore, existing distributed transaction processing methods using SAGA suffer from low processing efficiency and low reliability.
[0075] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will be described below with reference to the accompanying drawings.
[0076] Figure 1 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 1 .like Figure 1 As shown in the figure, the distributed transaction processing method of this application embodiment includes:
[0077] S101. Based on the target distributed transaction, determine the execution order of multiple branch transactions corresponding to the target distributed transaction; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution.
[0078] In this embodiment, non-idempotent distributed transactions are a type of distributed transaction, which refers to distributed transactions that include non-idempotent branch transactions whose effects differ from those of executing them once. Application scenarios of distributed transactions include: cross-database distributed transactions, cross-service distributed transactions, and hybrid distributed transactions.
[0079] In this embodiment, the distributed transaction processing method provided by this application can be based on SAGA, which divides the target distributed transaction into multiple branch transactions for management; in a specific example, the target distributed transaction includes branch transaction A, branch transaction B, branch transaction C and branch transaction D, wherein branch transaction C is the key branch transaction.
[0080] S102. In response to the business request, start the target distributed transaction and create the target distributed transaction flow;
[0081] S103. Determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, and execute the branch transactions. After the branch transactions are executed, return the status code representing the execution result and update the corresponding branch transaction pipeline based on the status code.
[0082] In this embodiment, before executing a branch transaction, a branch transaction pipeline is created. After executing a branch transaction, the transaction status of the corresponding branch transaction pipeline is updated based on the status code. The transaction status can be execution success, execution failure, or timeout when calling the corresponding microservice of the branch transaction.
[0083] In this embodiment, the branch transaction returns a status code that characterizes the execution result of the branch transaction. A status code of "00" indicates that the branch transaction was executed successfully, while a status code of "01" indicates that the branch transaction failed.
[0084] S104. When a branch transaction is executed successfully, the next branch transaction is executed according to the execution order; when a branch transaction fails, the previous branch transaction is reversed according to the execution order to undo the operations performed when the previous branch transaction was executed.
[0085] In this embodiment, the execution interface of the service corresponding to each branch transaction includes a forward interface and a reverse interface. When the microservice corresponding to the branch transaction returns "00", the next branch transaction is executed one by one according to the execution order based on the forward interface of the corresponding service, and the transaction status of the corresponding branch transaction pipeline is updated to success. When the microservice corresponding to the branch transaction returns "01", the previous branch transaction is compensated in reverse according to the execution order based on the reverse interface of the corresponding service, and the transaction status of the corresponding branch transaction pipeline is updated to failure.
[0086] S105. When the branch transaction call service times out, determine whether the branch transaction is in the state of reverse compensation of the previous branch transaction based on the execution order, and determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; if so, and the current execution status is successful, reverse compensation of the previous branch transaction based on the execution order.
[0087] In this embodiment, the current execution status of a branch transaction is determined through the query interface of the corresponding service of the branch transaction. The query interface includes a forward query interface and a reverse query interface. If the forward query interface determines that the branch transaction has been successfully executed, the next branch transaction continues to be executed. If the forward query interface determines that the branch transaction has failed, the reverse compensation logic is triggered, and the previous branch transaction is compensated in reverse based on the execution order according to the reverse interface. If the reverse query interface determines that the branch transaction has been successfully executed, the previous branch transaction continues to be compensated in reverse based on the execution order. If the reverse query interface determines that the branch transaction has failed, the execution process of the branch transaction is interrupted and the target distributed transaction is suspended.
[0088] The distributed transaction processing method, apparatus, device, and storage medium provided in this application, through a query interface, determine whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order when the corresponding branch transaction call service of the target distributed transaction times out, and determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; when the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution status is successful, reverse compensation of the previous branch transaction is performed based on the execution order; when the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution status is failure, the means of suspending the target distributed transaction does not require involving the branch transaction. By offloading all host functions of distributed transactions to the microservice system, the processing efficiency of distributed transactions is improved. This allows for determining the current execution status of a branch transaction when it times out, and continuing execution if the branch transaction succeeds. Conversely, if a branch transaction fails, reverse compensation is triggered for branches executing in the forward order, suspending only those in the reverse compensation state. This reduces the probability of inconsistent distributed transactions, improves consistency and reliability, and increases the success rate of distributed transaction processing. Therefore, it achieves the technical effect of improving the processing efficiency and reliability of distributed transactions.
[0089] Figure 2 This is a flowchart illustrating the distributed transaction processing method according to an embodiment of this application. Figure 2 .like Figure 2 As shown in the figure, the distributed transaction processing method of this application embodiment includes:
[0090] S201. Based on the target distributed transaction, determine the execution order of multiple branch transactions corresponding to the target distributed transaction; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution.
[0091] S202. In response to the business request, start the target distributed transaction and create the target distributed transaction flow;
[0092] S203. Determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, and execute the branch transactions. After the branch transactions are executed, return the status code representing the execution result and update the corresponding branch transaction pipeline based on the status code.
[0093] S204. When a branch transaction is executed successfully, the next branch transaction is determined based on the execution order, and the next branch transaction is executed based on the forward interface of the microservice corresponding to the next branch transaction.
[0094] S205. When a branch transaction fails to execute, the previous branch transaction is determined based on the execution order, and the previous branch transaction is compensated in reverse based on the reverse interface of the microservice corresponding to the previous branch transaction, so as to undo the operations performed when executing the previous branch transaction.
[0095] S206. When a branch transaction calls a service and times out, determine whether the execution interface of the microservice corresponding to the branch transaction is a forward interface. If yes, determine the current execution status of the branch transaction based on the forward query interface of the microservice corresponding to the branch transaction, and determine that the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order. If no, determine the current execution status of the branch transaction based on the reverse query interface of the service corresponding to the branch transaction, and determine that the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order.
[0096] In this embodiment, corresponding to the forward and reverse interfaces of the microservice execution interface, the branch transaction call timeout includes the forward interface call timeout and the reverse interface call timeout; the query interface is called to determine the current execution status of the current branch transaction call timeout; the query interface includes the forward query interface corresponding to the forward interface and the reverse query interface corresponding to the reverse interface.
[0097] In this embodiment, when the forward query interface returns a status code indicating successful execution, it means that the current branch transaction is not in a reverse compensation state and belongs to the state of executing branch transactions based on execution order and successfully executing. In this case, the next branch transaction continues to be executed based on the execution order. When the forward query interface returns a status code indicating failed execution, it means that the current branch transaction is not in a reverse compensation state and belongs to the state of executing branch transactions based on execution order and failed executing. In this case, the previous branch transaction continues to be reverse compensated based on the execution order.
[0098] In this embodiment, when the reverse query interface returns a status code indicating successful execution, it means that the current branch transaction is in a reverse compensation state and belongs to the state of reverse compensation branch transactions based on execution order and successful execution. In this case, the reverse compensation of the previous branch transaction continues. When the reverse query interface returns a status code indicating failed execution, it means that the current branch transaction is in a reverse compensation state and belongs to the state of reverse compensation branch transactions based on execution order and failed execution. In this case, the execution flow of the target distributed transaction is interrupted and the target distributed transaction is suspended.
[0099] S207. When a branch transaction calls a service and the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution status is successful, reverse compensation of the previous branch transaction based on the execution order is performed.
[0100] S208. When a branch transaction call to a service times out, the branch transaction is not in the state of reverse compensation of the previous branch transaction based on the execution order, and the current execution status is successful, the next branch transaction is executed according to the execution order based on the forward interface of the microservice corresponding to the branch transaction.
[0101] S209. When a branch transaction call to a service times out, the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution state is execution failure, the reverse interface of the microservice corresponding to the branch transaction is used to reverse compensate the previous branch transaction according to the execution order, so as to undo the operations performed when executing the previous branch transaction.
[0102] S210. When a branch transaction calls a service and times out, the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution state is execution failure, the target distributed transaction is suspended.
[0103] Through the above steps S201 to S210, after a branch transaction call times out, the query interface of the corresponding service execution interface of the branch transaction can be called to determine the actual execution status of the branch transaction. Based on the actual execution status of the branch transaction, the branch transaction can continue to be executed or the target distributed transaction can be suspended, thereby reducing the number of inconsistent transactions caused by transaction suspension and improving the success rate of consistency assurance and the reliability of distributed devices.
[0104] Figure 3 A flowchart illustrating the distributed transaction processing method in this embodiment. Figure 3 , Figure 4 A flowchart illustrating the distributed transaction processing method in this embodiment. Figure 4 .like Figure 3 and 4As shown, the distributed transaction processing method provided in this application is based on the SAGA distributed transaction model. The branch transactions corresponding to the target global transaction include branch transaction A, branch transaction B, branch transaction C, and branch transaction D. After the branch transaction is executed, the execution result returned includes a status code "00" representing successful execution and a status code "01" representing failed execution. The execution interface of the service corresponding to each branch transaction includes a forward interface and a reverse interface. When a branch transaction is executed successfully, the next branch transaction is executed one by one according to the execution order based on the forward interface of the corresponding service. When a branch transaction fails, the previous branch transaction is compensated in reverse according to the execution order based on the reverse interface of the corresponding service. The branch transaction forward interface... The query interface is called after the reverse interface call times out. Therefore, there are two types of query interfaces: a forward query interface corresponding to the forward interface and a reverse query interface corresponding to the reverse interface. If the forward query interface returns "00", the next branch transaction continues to execute; if it returns "01", the reverse compensation logic is triggered. If the reverse query interface returns "00", the previous branch transaction continues to be compensated; if it returns "01", the compensation fails, the business process is interrupted, and the transaction is suspended. The method includes: starting a global transaction based on the transaction coordinator; creating and updating the global transaction flow and branch transaction flow based on the transaction manager; and determining whether a branch transaction call times out and controlling the compensation triggering of the branch transaction based on the branch transaction aspect.
[0105] S301. Based on the target distributed transaction, determine the execution order of multiple branch transactions corresponding to the target distributed transaction. The execution order is branch transaction A, branch transaction B, branch transaction C, and branch transaction D. Among them, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from single executions.
[0106] S302. In response to the business request, start the target distributed transaction and create the target distributed transaction flow;
[0107] In this embodiment, the implementation process of the target distributed transaction can be used as the main business process. Four branch transactions are defined in this embodiment, including branch transaction A, branch transaction B, branch transaction C, and branch transaction D. Global transaction annotations and branch transaction annotations are added to the methods of the main business process and the forward interfaces corresponding to the branch transactions, respectively. For example, the @GlobalTransaction annotation is used to annotate the service methods of the main business process, and the @BranchTransaction annotation is used to annotate the above four branch transactions. The retryCount attribute of the number of calls is set to define the number of times the corresponding query interface can be retried after the service call of the branch transaction times out.
[0108] S303. Determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, and execute the branch transactions. After the branch transactions are executed, return the status code representing the execution result and update the corresponding branch transaction pipeline based on the status code.
[0109] In this embodiment, when a branch transaction is executed successfully, a status code "00" indicating the success of the branch transaction is returned, and the next branch transaction is executed based on the execution order; when a branch transaction fails, a status code "01" indicating the failure of the branch transaction is returned, and the previous branch transaction is reversed based on the execution order to undo the operations performed when executing the previous branch transaction.
[0110] S304. When a branch transaction is executed successfully, return the status code "00" indicating that the branch transaction was executed successfully, and determine the next branch transaction based on the execution order, and execute the next branch transaction based on the forward interface of the service corresponding to the next branch transaction.
[0111] In this embodiment, if branch transaction B is executed successfully, then branch transaction C should continue to be executed according to the execution order. At this time, the corresponding service is executed based on the forward interface of the service corresponding to branch transaction C.
[0112] S305. When a branch transaction fails to execute, return the status code "01" indicating that the branch transaction failed. Determine the previous branch transaction based on the execution order. Compensate the previous branch transaction in reverse based on the reverse interface of the service corresponding to the previous branch transaction to undo the operations performed when executing the previous branch transaction.
[0113] In this embodiment, when branch transaction B fails to execute, it is necessary to determine the previous branch transaction of branch transaction B based on the execution order. After determining that the previous branch transaction is branch transaction A, reverse compensation is performed based on the reverse interface of the service corresponding to branch transaction A, and the operation performed by the service corresponding to branch transaction A is undone.
[0114] S306. When the service call of branch transaction C times out, determine whether the execution interface of the microservice corresponding to branch transaction C is a forward interface; if so, determine the current execution status of branch transaction C based on the forward query interface of the microservice corresponding to branch transaction C, and determine that branch transaction C is not in the state of reverse compensation based on execution order; if not, determine the current execution status of branch transaction C based on the reverse query interface of the service corresponding to branch transaction C, and determine that branch transaction C is in the state of reverse compensation based on execution order.
[0115] In this embodiment, if the timeout for calling branch transaction C is a forward interface call timeout, the current execution state of branch transaction C is determined based on the forward query interface of the branch transaction; if the timeout for calling branch transaction C is a reverse interface call timeout, the current execution state of branch transaction C is determined based on the reverse query interface of the branch transaction.
[0116] S307. When the service call of branch transaction C times out, and branch transaction C is in the state of reverse compensation based on the execution order and the current execution status is successful, reverse compensation is made to branch transaction B based on the execution order.
[0117] In this embodiment, when the reverse query interface of branch transaction C returns a status code indicating successful execution, it means that branch transaction C is in a reverse compensation state and belongs to the state of reverse compensation of branch transaction C based on execution order and successful execution. Then, reverse compensation of branch transaction B continues.
[0118] S308. When the branch transaction call service times out, the branch transaction is not in the state of reverse compensation of the previous branch transaction based on the execution order, and the current execution status is successful, the branch transaction D is executed according to the execution order based on the forward interface of the microservice corresponding to the branch transaction.
[0119] In this embodiment, when the forward query interface of branch transaction C returns a status code indicating successful execution, it means that branch transaction C is not in a reverse compensation state and belongs to the state of executing branch transaction C based on the execution order and successfully executing. Then, branch transaction D continues to be executed based on the execution order.
[0120] S309. When the service call of branch transaction C times out, branch transaction C is not in the state of reverse compensation based on execution order and the current execution state is execution failure, the reverse interface of the microservice corresponding to branch transaction C is used to reverse compensate branch transaction B according to the execution order, so as to undo the operation performed when executing branch transaction B.
[0121] In this embodiment, when the forward query interface of branch transaction C returns a status code indicating execution failure, it means that branch transaction C is not in a reverse compensation state and belongs to the state of executing branch transaction C based on the execution order and failing to execute. Then, reverse compensation of branch transaction B continues based on the execution order.
[0122] S310. When the service call of branch transaction C times out, and branch transaction C is in the state of reverse compensation based on execution order and the current execution state is execution failure, the target distributed transaction corresponding to branch transaction C is suspended.
[0123] In this embodiment, when the reverse query interface of branch transaction C returns a status code indicating execution failure, it indicates that branch transaction C is in a reverse compensation state and belongs to the state of reverse compensation of branch transaction C based on execution order and execution failure. Then, the execution flow of the target distributed transaction is interrupted and the target distributed transaction is suspended.
[0124] S311. When branch transactions A, B, C, and D all return the status code "00" indicating that the branch transactions have been successfully executed, the target distributed transaction is determined to have been successfully executed.
[0125] The distributed transaction processing method provided in this application determines whether a branch transaction is in a state of reverse compensation based on the execution order of the previous branch transaction when the corresponding branch transaction call service times out through a query interface, and determines the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; if the branch transaction is in a state of reverse compensation based on the execution order of the previous branch transaction and the current execution status is successful, then reverse compensation is performed on the previous branch transaction according to the execution order; if the branch transaction is in a state of reverse compensation based on the execution order of the previous branch transaction and the current execution status is failure, then the target distributed transaction is suspended. This method eliminates the need to move all host functions involving distributed transactions down to microservices. The system improves the efficiency of distributed transaction processing. It introduces a query interface when a branch transaction call times out. After a branch transaction call times out, the query interface determines the current actual execution status of the branch transaction. If the branch transaction actually executes successfully, it continues execution. If the branch transaction fails, it triggers reverse compensation for branches executing in the forward order, suspending only branches in the reverse compensation state. This reduces the probability of inconsistent distributed transactions, improves the consistency and reliability of distributed transactions, and increases the success rate of distributed transaction processing. Therefore, it achieves the technical effect of improving the efficiency and reliability of distributed transaction processing.
[0126] Figure 5 This is a schematic diagram of a distributed transaction processing device provided in an embodiment of this application. The device in this embodiment can be in the form of software and / or hardware. Figure 5 As shown in the embodiment of this application, a distributed transaction processing device 500 is provided. The device includes: a determining module 501, an initiating module 502, a first processing module 503, a second processing module 504, and a third processing module 505.
[0127] The determination module 501 is used to determine the execution order of multiple branch transactions corresponding to the target distributed transaction based on the target distributed transaction; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution;
[0128] The startup module 502 is used to respond to business requests, start the target distributed transaction, and create the target distributed transaction pipeline.
[0129] The first processing module 503 is used to determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, execute the branch transactions, return a status code representing the execution result after the branch transaction is executed, and update the corresponding branch transaction pipeline based on the status code.
[0130] The second processing module 504 is used to execute the next branch transaction based on the execution order when the branch transaction is executed successfully; and to reverse the previous branch transaction based on the execution order when the branch transaction fails, so as to undo the operations performed when the previous branch transaction was executed.
[0131] The third processing module 505 is used to determine whether the branch transaction is in the state of reverse compensation of the previous branch transaction based on the execution order when the branch transaction call service times out, and to determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; if so, and the current execution status is successful, the previous branch transaction is compensated in reverse order.
[0132] In one possible implementation, optionally, when a branch transaction call to the service times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the device is further configured to:
[0133] When the current execution status is successful, the forward interface of the microservice corresponding to the branch transaction executes the next branch transaction according to the execution order.
[0134] In one possible implementation, when a branch transaction call to the service times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the device is further configured to:
[0135] When the current execution status is execution failure, the reverse interface of the microservice corresponding to the branch transaction compensates the previous branch transaction in reverse order according to the execution order, so as to undo the operations performed when executing the previous branch transaction.
[0136] In one possible implementation, the device is also used for:
[0137] When a branch transaction is executed successfully, the next branch transaction is determined based on the execution order.
[0138] The next branch transaction is executed based on the forward interface of the microservice corresponding to the next branch transaction.
[0139] In one possible implementation, the device is also used for:
[0140] When a branch transaction fails, the previous branch transaction is determined based on the execution order.
[0141] The reverse interface of the microservice corresponding to the previous branch transaction compensates for the previous branch transaction.
[0142] In one possible implementation, the device is also used for:
[0143] Determine whether the execution interface of the microservice corresponding to the branch transaction is a positive interface;
[0144] If so, the current execution status of the branch transaction is determined based on the forward query interface of the microservice corresponding to the branch transaction, and it is determined that the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order.
[0145] If not, the current execution status of the branch transaction is determined based on the reverse query interface of the service corresponding to the branch transaction, and the branch transaction is determined to be in the state of reverse compensation of the previous branch transaction based on the execution order.
[0146] In one possible implementation, when the branch transaction call service times out, the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution state is execution failure, the device is also used to: suspend the target distributed transaction.
[0147] Figure 6 This application provides a schematic diagram of the structure of a distributed transaction processing device, which includes:
[0148] Processor 601 and memory 602;
[0149] The memory stores instructions that the computer executes;
[0150] The processor executes the computer execution instructions stored in memory 602, causing the distributed transaction processing device to perform the distributed service processing method described above.
[0151] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement a distributed transaction processing method.
[0152] This disclosure provides a computer program product, including a computer program that, when executed by a processor, implements the distributed transaction processing method described above.
[0153] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0154] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A method for processing distributed transactions, characterized in that, The method includes: Based on the target distributed transaction, the execution order of multiple branch transactions corresponding to the target distributed transaction is determined; wherein, the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution. In response to a business request, initiate the target distributed transaction and create the target distributed transaction pipeline; Based on the execution order, the branch transactions to be executed are determined one by one, a branch transaction pipeline of the branch transactions is created, and the branch transactions are executed. After the branch transactions are executed, a status code representing the execution result is returned, and the corresponding branch transaction pipeline is updated based on the status code. When a branch transaction is executed successfully, the next branch transaction is executed according to the execution order; when a branch transaction fails, the previous branch transaction is reversed and compensated according to the execution order to undo the operations performed when executing the previous branch transaction. When the branch transaction call service times out, it is determined whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and the current execution status of the branch transaction is determined based on the query interface corresponding to the branch transaction; if so, and the current execution status is successful, the previous branch transaction is compensated in reverse based on the execution order. Wherein, when the branch transaction service call times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the method further includes: When the current execution status is successful, the next branch transaction is executed according to the execution order based on the forward interface of the microservice corresponding to the branch transaction. When the current execution status is execution failure, the reverse interface of the microservice corresponding to the branch transaction is used to reverse the previous branch transaction according to the execution order, so as to undo the operations performed when executing the previous branch transaction; When a branch transaction call to the service times out, and the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution state is execution failure, the following applies: suspending the target distributed transaction.
2. The processing method according to claim 1, characterized in that, The step of executing the next branch transaction based on the execution order when the branch transaction is executed successfully includes: When the branch transaction is executed successfully, the next branch transaction is determined based on the execution order. The next branch transaction is executed based on the forward interface of the microservice corresponding to the next branch transaction.
3. The processing method according to claim 2, characterized in that, When the branch transaction fails, the step of reverse compensation of the previous branch transaction based on the execution order includes: When a branch transaction fails to execute, the previous branch transaction is determined based on the execution order. The reverse interface of the microservice corresponding to the previous branch transaction compensates for the previous branch transaction.
4. The processing method according to claim 3, characterized in that, The step of determining whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order, and determining the current execution state of the branch transaction based on the query interface corresponding to the branch transaction, includes: Determine whether the execution interface of the microservice corresponding to the branch transaction is a positive interface; If so, the current execution state of the branch transaction is determined based on the forward query interface of the microservice corresponding to the branch transaction, and it is determined that the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order. If not, the current execution status of the branch transaction is determined based on the reverse query interface of the service corresponding to the branch transaction, and the branch transaction is determined to be in a state of reverse compensation of the previous branch transaction based on the execution order.
5. A distributed transaction processing apparatus, comprising: A determination module is used to determine the execution order of multiple branch transactions corresponding to a target distributed transaction based on the target distributed transaction; wherein the target distributed transaction includes non-idempotent branch transactions whose effects from multiple executions are different from those from a single execution; The startup module is used to respond to business requests, start the target distributed transaction, and create the target distributed transaction pipeline. The first processing module is used to determine the branch transactions to be executed one by one based on the execution order, create the branch transaction pipeline of the branch transactions, execute the branch transactions, return a status code representing the execution result after the branch transactions are executed, and update the corresponding branch transaction pipeline based on the status code. The second processing module is used to execute the next branch transaction based on the execution order when the branch transaction is executed successfully; and to reverse-compensate the previous branch transaction based on the execution order when the branch transaction fails, so as to undo the operations performed when executing the previous branch transaction. The third processing module is used to determine whether the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order when the branch transaction call service times out, and to determine the current execution status of the branch transaction based on the query interface corresponding to the branch transaction; if yes, and the current execution status is successful, then reverse compensation of the previous branch transaction based on the execution order. Wherein, when the branch transaction call service times out and the branch transaction is not in a state of reverse compensation of the previous branch transaction based on the execution order, the device is further configured to: When the current execution status is successful, the next branch transaction is executed according to the execution order based on the forward interface of the microservice corresponding to the branch transaction. When the current execution status is execution failure, the reverse interface of the microservice corresponding to the branch transaction is used to reverse the previous branch transaction according to the execution order, so as to undo the operations performed when executing the previous branch transaction; When a branch transaction call service times out, and the branch transaction is in a state of reverse compensation of the previous branch transaction based on the execution order and the current execution state is execution failure, the device is further configured to: suspend the target distributed transaction.
6. A distributed transaction processing device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the distributed transaction processing method as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the distributed transaction processing method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Distributed transaction processing method and device
CN111259083A
Distributed long transaction processing method and device
CN116126481A
