Sample application of Spring Data JDBC for ScalarDB with Microservice Transactions
This tutorial describes how to create a sample Spring Boot application for microservice transactions by using Spring Data JDBC for ScalarDB.
For details about these features, see Two-phase Commit Transactions and Guide of Spring Data JDBC for ScalarDB.
Prerequisites for this sample application​
- OpenJDK LTS version (8, 11, 17, or 21) from Eclipse Temurin
- Docker 20.10 or later with Docker Compose V2 or later
This sample application has been tested with OpenJDK from Eclipse Temurin. ScalarDB itself, however, has been tested with JDK distributions from various vendors. For details about the requirements for ScalarDB, including compatible JDK distributions, please see Requirements.
You need to have a license key (trial license or commercial license) to use ScalarDB Cluster. If you don't have a license key, please contact us.
Sample application​
Overview​
This tutorial illustrates the process of creating a sample e-commerce application, where items can be ordered and paid for with a line of credit through transactions with a two-phase commit interface in ScalarDB.
There are two microservices called the Customer Service and the Order Service based on the Database-per-service pattern in this sample application.
The Customer Service manages customers' information including credit card information like a credit limit and a credit total. The Order Service is responsible for order operations like placing an order and getting order histories. Each service has gRPC endpoints. Clients call the endpoints, and the services call the endpoints each other as well. The Customer Service and the Order Service use MySQL and Cassandra through ScalarDB, respectively.

Each service accesses the databases through its own dedicated ScalarDB Cluster.
Both ScalarDB Clusters access a small coordinator database used for the Consensus Commit protocol. In this sample application, for ease of setup and explanation, the coordinator database is co-located in the same Cassandra instance of the Order Service, but of course, the coordinator database can be managed as a separate database.
Also, application-specific error handling, authentication processing, etc. are omitted in the sample application since it focuses on explaining how to use ScalarDB. For details about handling exceptions in ScalarDB, see How to handle exceptions.
Service endpoints​
The endpoints defined in the services are as follows:
-
Customer Service
getCustomerInfopaymentpreparevalidatecommitrollbackrepayment
-
Order Service
placeOrdergetOrdergetOrders
What you can do in this sample application​
The sample application supports the following types of transactions:
- Get customer information through the
getCustomerInfoendpoint of the Customer Service. - Place an order by using a line of credit through the
placeOrderendpoint of the Order Service and thepayment,prepare,validate,commit, androllbackendpoints of the Customer Service.- Checks if the cost of the order is below the customer's credit limit.
- If the check passes, records the order history and updates the amount the customer has spent.
- Get order information by order ID through the
getOrderendpoint of the Order Service and thegetCustomerInfo,prepare,validate,commit, androllbackendpoints of the Customer Service. - Get order information by customer ID through the
getOrdersendpoint of the Order Service and thegetCustomerInfo,prepare,validate,commit, androllbackendpoints of the Customer Service. - Make a payment through the
repaymentendpoint of the Customer Service.- Reduces the amount the customer has spent.
The getCustomerInfo endpoint works as a participant service endpoint when receiving a transaction ID from the coordinator.
Configuration for ScalarDB Cluster​
The configuration for ScalarDB Cluster for the Customer Service is as follows:
scalar.db.storage=multi-storage
scalar.db.multi_storage.storages=cassandra,mysql
scalar.db.multi_storage.storages.cassandra.storage=cassandra
scalar.db.multi_storage.storages.cassandra.contact_points=cassandra-1
scalar.db.multi_storage.storages.cassandra.username=cassandra
scalar.db.multi_storage.storages.cassandra.password=cassandra
scalar.db.multi_storage.storages.mysql.storage=jdbc
scalar.db.multi_storage.storages.mysql.contact_points=jdbc:mysql://mysql-1:3306/
scalar.db.multi_storage.storages.mysql.username=root
scalar.db.multi_storage.storages.mysql.password=mysql
scalar.db.multi_storage.namespace_mapping=customer_service:mysql,coordinator:cassandra
scalar.db.multi_storage.default_storage=mysql
scalar.db.consensus_commit.isolation_level=SERIALIZABLE
scalar.db.cluster.node.standalone_mode.enabled=true
scalar.db.sql.enabled=true
# License key configurations
scalar.db.cluster.node.licensing.license_key=
scalar.db.cluster.node.licensing.license_check_cert_pem=
scalar.db.sql.connection_mode: This configuration decides how to connect to ScalarDB.scalar.db.storage: Specifyingmulti-storageis necessary to use Multi-storage Transactions in ScalarDB.scalar.db.multi_storage.storages: Your storage names must be defined here.scalar.db.multi_storage.storages.cassandra.*: These configurations are for thecassandrastorage, which is one of the storage names defined inscalar.db.multi_storage.storages. You can configure all thescalar.db.*properties for thecassandrastorage here.scalar.db.multi_storage.storages.mysql.*: These configurations are for themysqlstorage, which is one of the storage names defined inscalar.db.multi_storage.storages. You can configure all thescalar.db.*properties for themysqlstorage here.scalar.db.multi_storage.namespace_mapping: This configuration maps the namespaces to the storage. In this sample application, operations forcustomer_servicenamespace tables are mapped to themysqlstorage and operations fororder_servicenamespace tables are mapped to thecassandrastorage. You can also define which storage is mapped for thecoordinatornamespace that is used in Consensus Commit transactions.scalar.db.multi_storage.default_storage: This configuration sets the default storage that is used for operations on unmapped namespace tables.scalar.db.sql.default_transaction_mode: Specifyingtwo_phase_commit_transactionis necessary to use Two-Phase Commit Transactions mode in ScalarDB.scalar.db.consensus_commit.isolation_level: This configuration decides the isolation level used for ConsensusCommit.
For details, see Multi-Storage Transactions.
The configuration for ScalarDB Cluster for the Order Service is as follows:
scalar.db.storage=cassandra
scalar.db.contact_points=cassandra-1
scalar.db.username=cassandra
scalar.db.password=cassandra
scalar.db.consensus_commit.isolation_level=SERIALIZABLE
scalar.db.cluster.node.standalone_mode.enabled=true
scalar.db.sql.enabled=true
# License key configurations
scalar.db.cluster.node.licensing.license_key=
scalar.db.cluster.node.licensing.license_check_cert_pem=
scalar.db.storage:cassandrais specified since this servcise uses only Cassandra as an underlying database.scalar.db.contact_points: This configuration specifies the contact points (e.g., host) for connecting to Cassandra.scalar.db.username: This configuration specifies the username for connecting to Cassandra.scalar.db.password: This configuration specifies the password for connecting to Cassandra.scalar.db.sql.default_namespace_name: This configuration sets the default namespace toorder_service, eliminating the need for the application to specify namespaces.scalar.db.sql.default_transaction_mode: Specifyingtwo_phase_commit_transactionis necessary to use Two-Phase Commit Transactions mode in ScalarDB.scalar.db.consensus_commit.isolation_level: This configuration decides the isolation level used for ConsensusCommit.
In this sample application, the ScalarDB Clusters are running in standalone mode (scalar.db.cluster.node.standalone_mode.enabled=true).
Also, you need to set the license key (trial license or commercial license) for the ScalarDB Clusters in the configuration file. For details, see How to Configure a Product License Key.