3.2.12 Transaction Task
The transaction task enables you to use MicroTx Distributed Transactions coordinator to manage XA transactions that span across multiple tasks in MicroTx Workflows.
The Transaction Task provides the capability of distributed transaction
management as a system task within MicroTx Workflows. This
task communicates with MicroTx Distributed Transactions
coordinator using HTTP
REST APIs and currently supports XA distributed transactions. This allows workflows to
automatically initiate, commit, or roll back transactions based on the value provided
for the action parameter. When a transaction starts, the transaction
context (txCtx) is maintained within the workflow input instance and it
is shared across the tasks that are called between the begin and commit actions. At
present, txCtx propagation is only implemented for HTTP tasks and SQL
tasks; when an HTTP task or SQL task is executed within this transaction context, the
link headers related to txCtx are propagated.
Prerequisites
Before you begin, ensure that you have installed and configured the MicroTx Distributed Transactions coordinator. See Access MicroTx Coordinator.
To add a Transaction Task
-
Navigate to the Task tab in a workflow and view all the tasks that you can add using the Workflow Builder. See Access the Task Tab in Workflow Builder.
- In the More Tasks dialog box, click Transaction to add it to the workflow.
-
Click the task that you have added in the left pane. The Task tab in the right pane displays details about the task, such as its name and parameters. Next, let's provide details for the task.
-
In the Task Details group, enter the following information.
- Task Name: Mandatory. Enter a unique name for the task. The name must be between 1 to 128 alphanumeric characters in length and cannot contain spaces or any special characters. Optionally, you can use underscore (_) and hyphen (-).
- Task Reference: Mandatory. Enter a value to refer to the task within a workflow definition. This value must be unique within a workflow. The task reference value must be between 1 to 128 alphanumeric characters in length and cannot contain spaces or any special characters. Optionally, you can use underscore (_) and hyphen (-).
- In the Transaction Parameters group, provide
the following information.
- Coordinator URL: Mandatory. Enter the
details required to create the external URL to access MicroTx from
outside the Kubernetes cluster where you have deployed the service. See
Access MicroTx
Coordinator. For example,
https://192.0.2.1:443/api/v1. - Transaction Type: Select
XAto specify the distributed transaction protocol used by the microservices. - Action: Select one of the following
values. Accordingly, the Transaction task begins, commits, or rolls back
a transaction.
BEGIN: starts the XA transaction.COMMIT: commits the XA transaction.ROLLBACK: rolls back the XA transaction.
When you begin, commit, or roll back a transaction, you must mention a value for Action.
- Transaction Timeout (ms): Specify the transaction
timeout, in milliseconds, only when the
actionis set toBEGIN. If you have also specified the transaction timeout for the transaction initiator application, that timeout overrides the value you specify here. - Add Header: Add headers that you want to pass in the header section of the outgoing request.
- Coordinator URL: Mandatory. Enter the
details required to create the external URL to access MicroTx from
outside the Kubernetes cluster where you have deployed the service. See
Access MicroTx
Coordinator. For example,
-
Click Save to save the changes to the workflow.
MicroTx Workflows displays the changes in JSON code.
-
Review all the changes, and then click Confirm Save to save the changes.
If you do not want to save the changes, click Cancel, and then click Reset to undo all the changes that you have made since the workflow was last saved.
Usage Notes
When you want to perform an XA transaction with a workflow, use transaction tasks within a workflow. In between these two Transaction tasks, you can add other tasks that are part of the XA transaction based on the business logic. You can add non-XA compliant tasks and XA-compliant tasks, such as HTTP task and SQL task. The following example describes how you can call SQL task and HTTP task between two Transaction tasks.
- Create a transaction task with
actionset toBEGIN. - Create a SQL task to query balance on account for verification.
- Create an HTTP task to withdraw money from one account and deposit it into another account.
- Create another transaction task with
actionset toCOMMITto commit the transaction. - Create a SQL task to query balance on accounts after withdraw and deposit.
Example
{
"name": "begin_tx",
"taskReferenceName": "begin_tx",
"inputParameters": {
"transaction_request": {
"coordinatorUrl": "http://192.0.2.1:9000/api/v1",
"transactionType": "XA",
"action": "BEGIN"
},
"type": "TRANSACTION"
}
}
Parent topic: Create System Tasks and Operator Tasks