Transactions with a Two-Phase Commit Interface
ScalarDB supports executing transactions with a two-phase commit interface. With the two-phase commit interface, you can execute a transaction that spans multiple processes or applications, like in a microservice architecture.
This page explains how transactions with a two-phase commit interface work in ScalarDB and how to configure and execute them in ScalarDB.
How transactions with a two-phase commit interface work in ScalarDB
ScalarDB normally executes transactions in a single transaction manager instance with a one-phase commit interface. In transactions with a one-phase commit interface, you begin a transaction, execute CRUD operations, and commit the transaction in the same transaction manager instance.
In ScalarDB, you can execute transactions with a two-phase commit interface that span multiple transaction manager instances. The transaction manager instances can be in the same process or application, or the instances can be in different processes or applications. For example, if you have transaction manager instances in multiple microservices, you can execute a transaction that spans multiple microservices.
In transactions with a two-phase commit interface, there are two roles—Coordinator and a participant—that collaboratively execute a single transaction.
The Coordinator process and the participant processes all have different transaction manager instances. The Coordinator process first begins or starts a transaction, and the participant processes join the transaction. After executing CRUD operations, the Coordinator process and the participant processes commit the transaction by using the two-phase interface.
How to execute transactions with a two-phase commit interface
To execute a two-phase commit transaction, you must get the transaction manager instance. Then, the Coordinator process can begin or start the transaction, and the participant can process the transaction.
Get a TwoPhaseCommitTransactionManager
instance
You first need to get a TwoPhaseCommitTransactionManager
instance to execute transactions with a two-phase commit interface.
To get a TwoPhaseCommitTransactionManager
instance, you can use TransactionFactory
as follows:
TransactionFactory factory = TransactionFactory.create("<CONFIGURATION_FILE_PATH>");
TwoPhaseCommitTransactionManager manager = factory.getTwoPhaseCommitTransactionManager();
Begin or start a transaction (for Coordinator)
For the process or application that begins the transaction to act as Coordinator, you should use the following begin
method:
// Begin a transaction.
TwoPhaseCommitTransaction tx = manager.begin();
Or, for the process or application that starts the transaction to act as Coordinator, you should use the following start
method:
// Start a transaction.
TwoPhaseCommitTransaction tx = manager.start();
Alternatively, you can use the begin
method for a transaction by specifying a transaction ID as follows:
// Begin a transaction by specifying a transaction ID.
TwoPhaseCommitTransaction tx = manager.begin("<TRANSACTION_ID>");
Or, you can use the start
method for a transaction by specifying a transaction ID as follows:
// Start a transaction by specifying a transaction ID.
TwoPhaseCommitTransaction tx = manager.start("<TRANSACTION_ID>");