1 About MicroTx
Oracle Transaction Manager for Microservices (MicroTx) enables enterprise users to adopt and increase use of microservices architecture for mission-critical applications by providing capabilities that make it easier to develop, deploy, and maintain such applications.
As organizations rush to adopt microservices architecture, they often run into problems associated with data consistency as each microservice typically has its own database. In monolithic applications, local transactions were enough as there were no other sources of data that needed to be consistent with the database. An application would start a local transaction, perform some updates, and then commit the local transaction to ensure the application moved from one consistent state to another. Once the application’s state is spread across multiple sources of data, some factors need to be considered. What happens if updates succeed in one microservice, but it fails in another microservice as part of the same request? One solution is to use a distributed transaction that spans the sources of data used by the microservices involved in a request. Oracle Transaction Manager for Microservices provides a transaction coordination microservice and libraries to maintain consistency in the state of microservices participating in a transaction.
- About the Distributed Transaction Protocols
MicroTx supports the following distributed transaction protocols: - Components of MicroTx
MicroTx contains two components: the transaction coordinator and the MicroTx library. - How MicroTx Works
- Use MicroTx Library with Application Code
1.1 About the Distributed Transaction Protocols
MicroTx supports the following distributed transaction protocols:
- XA protocol, which is based upon The Open Group’s XA specification. For details about the specification, see https://pubs.opengroup.org/onlinepubs/009680699/toc.pdf.
- Saga protocol, which is based on the Eclipse MicroProfile LRA specification. For details about the specification, see https://download.eclipse.org/microprofile/microprofile-lra-1.0-M1/microprofile-lra-spec.html.
- Try-Confirm/Cancel (TCC) protocol
Use XA when strong consistency is required, similar to consistency provided by the local database transactions, where all the ACID properties of a transaction are present. For example, financial applications. Use the Saga protocol for transactions that may take a long time to complete. You can use the Saga protocol to mitigate locking issues. The TCC protocol fits well for applications that use a reservation model, such as airline seats or hotel rooms. Both Saga and TCC support long running transactions. Saga is far more general, but requires application specific actions for both completing a successful Saga and compensating a failed Saga. Whereas, compensation in TCC is performed by deleting the reservation, and then returning whatever was reserved to the pool of available resources.
Parent topic: About MicroTx
1.2 Components of MicroTx
MicroTx contains two components: the transaction coordinator and the MicroTx library.
MicroTx, a containerized microservice, runs along with your application microservices. The following figure shows how the components of MicroTx interact with your application microservices.

Transaction Coordinator Server
The transaction coordinator manages transactions amongst the participant services.
MicroTx supports internal memory, Oracle Database, and etcd as a data store for persistence of transaction state.
MicroTx library
Application microservices provide the business logic and demarcate transaction boundaries. These services participate in a distributed transaction. They use MicroTx APIs to manage their distributed transactions.
- The development framework of the microservice, such as Helidon or Node.js.
- The selected transaction protocol, such as XA, Saga, or TCC.
- Whether the application initiates a transaction or participates in the transaction.
- Transaction initiator service - These applications start and end a transaction. In the preceding figure, Microservice 1 is the transaction initiator service and it sends a request to MicroTx to begin the transaction.
- Transaction participant service - These applications only join the transaction. They do not initiate the transaction. In the preceding figure, Microservice 2 and Microservice 3 are the transaction participant services that are involved in the transaction.
Parent topic: About MicroTx
1.3 How MicroTx Works
Here's a typical transaction workflow when you use MicroTx. The following figure shows how MicroTx communicates with your application microservices to handle transactions.

- Application developers use functions present in the MicroTx library with their application code.
- When a microservice or client initiates a transaction, it calls functions in the MicroTx library to start a distributed transaction.
- MicroTx library includes headers that enable the participant services to automatically enlist in the transaction.
- After all the tasks associated the original request made by the initiator service are complete, the initiator service requests the transaction coordinator to either commit or roll back all the changes.
- The transaction coordinator sends a call to each participant service to either commit or roll back the changes made by the participants as part of the distributed transaction.
Parent topic: About MicroTx
1.4 Use MicroTx Library with Application Code
To use MicroTx to manage the transactions in your application, you need to make a few changes to your existing application code to integrate the functionality provided by the MicroTx libraries.
Let's use a sample Java application to understand the changes that you, as an application developer need to make. The sample application is a banking teller application which transfers an amount from one department to another. The sample XA application code files are available in the xa
folder in the microtx-samples
GitHub repository. The MicroTx library is already integrated with the sample application code.
The MicroTx library for Java performs the following functions:
- Enlists the participant service with the Transaction Coordinator in the transaction.
- Injects an
XADataSource
object for the participant application code to use through dependency injection, and then callsstart()
on the associatedXAResource
. Participant microservices, those microservices called in the context of an XA transaction, must use an XA-compliant data source. In Java this means using anXADataSource
object.The MicroTx libraries automatically inject the configured data source into the participant services, so the application developer must add the
@Inject
or@Context
annotation to the application code. The application code runs the DML using this connection. - Calls the resource managers to perform operations.
This highlighted code lines, in bold, in the following code snippet of the sample XA application, in Java, show the changes or additions that are typically made.
package com.oracle.mtm.sample.data;
...
/**
* Include the Transaction Manager for Microservices library files.
*/
import com.oracle.mtm.sample.Configuration;
import com.oracle.mtm.sample.entity.Account;
import oracle.trm.jta.common.TrmSQLConnection;
/**
* Service that connects to the accounts database and provides methods to interact with the accounts
*/
@RequestScoped
public class AccountsService implements IAccountsService {
/**
* The database connection injected by the Transaction Manager for Microservices library.
* Use this connection object to execute SQLs (DMLs) within the application code.
*/
@Inject
@TrmSQLConnection
private Connection connection;
@Inject
private Configuration config;
...
@Override
public boolean withdraw(String accountId, double amount) throws SQLException {
String query = "UPDATE accounts SET amount=amount-? where account_id=?";
/**
* Use the connection object that the Transaction Manager for Microservices library
* injects in the application code.
*/
PreparedStatement statement = connection.prepareStatement(query);
statement.setDouble(1, amount);
statement.setString(2, accountId);
return statement.executeUpdate() > 0;
}
...
}
The sample code has been truncated with … to improve readability. To view the entire sample code, see the sample XA application code files that are available in the installation bundle in the GitHub repository.
Parent topic: About MicroTx