Two-Phase Commit Transactions
ScalarDB also supports two-phase commit style transactions called Two-phase Commit Transactions. With Two-phase Commit Transactions, you can execute a transaction that spans multiple processes/applications (e.g., Microservices).
This document briefly explains how to execute Two-phase Commit Transactions in ScalarDB.
Configuration
The configuration for Two-phase Commit Transactions is the same as the one for the transaction API.
For example, you can set the following configuration when you use Cassandra:
# Comma separated contact points
scalar.db.contact_points=cassandra
# Port number for all the contact points. Default port number for each database is used if empty.
scalar.db.contact_port=9042
# Credential information to access the database
scalar.db.username=cassandra
scalar.db.password=cassandra
# Storage implementation. Either cassandra or cosmos or dynamo or jdbc can be set. Default storage is cassandra.
scalar.db.storage=cassandra
Please see Getting Started for configurations of other databases/storages.
ScalarDB Server
You can also execute Two-phase Commit Transactions through the ScalarDB Server. You don't need a special configuration for Two-phase Commit Transactions, so you can follow the ScalarDB Server document to use it.
How to execute Two-phase Commit Transactions
This section explains how to execute Two-phase Commit Transactions.
Like a well-known two-phase commit protocol, there are two roles, a coordinator and a participant, that collaboratively execute a single transaction. The coordinator process first starts a transaction, and the participant processes join the transaction after that.
Get a TwoPhaseCommitTransactionManager instance
First, you need to get a TwoPhaseCommitTransactionManager
instance to execute Two-phase Commit Transactions.
You can use TransactionFactory
to get a TwoPhaseCommitTransactionManager
instance as follows:
TransactionFactory factory = new TransactionFactory(new DatabaseConfig(new File("<configuration file path>")));
TwoPhaseCommitTransactionManager manager = factory.getTwoPhaseCommitTransactionManager();
Start a transaction (coordinator only)
You can start a transaction as follows:
TwoPhaseCommitTransaction tx = manager.start();
The process/application that starts the transaction acts as a coordinator, as mentioned.
You can also start a transaction by specifying a transaction ID as follows:
TwoPhaseCommitTransaction tx = manager.start("<transaction ID>");
And, you can get the transaction ID with getId()
as follows:
tx.getId();