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​
- One of the following Java Development Kits (JDKs):
- Oracle JDK: 8, 11, 17, or 21 (LTS versions)
- OpenJDK distribution (Eclipse Temurin, Amazon Corretto, or Microsoft Build of OpenJDK): 8, 11, 17, or 21 (LTS versions)
- Docker 20.10 or later with Docker Compose V2 or later
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.
Setup​
Clone the ScalarDB samples repository​
Open Terminal, then clone the ScalarDB samples repository by running the following command:
git clone https://github.com/scalar-labs/scalardb-samples
Then, go to the directory with this sample by running the following command:
cd scalardb-samples/spring-data-microservice-transaction-sample
Set the license key​
Set the license key (trial license or commercial license) for the ScalarDB Clusters in the configuration files scalardb-cluster-node-for-customer-service.properties and scalardb-cluster-node-for-order-service.properties. For details, see How to Configure a Product License Key.
Start Cassandra, MySQL, and ScalarDB Clusters​
To start Cassandra, MySQL, and ScalarDB Clusters, you need to run the following docker-compose command:
docker-compose up -d cassandra mysql scalardb-cluster-node-for-customer-service scalardb-cluster-node-for-order-service
You will need to wait more than one minute for the containers to be fully started.
Load schema​
The database schema (the method in which the data will be organized) for the sample application has already been defined in schema-for-customer-service.sql for the Customer Service and schema-for-order-service.sql for the Order Service.
To apply the schema, go to the ScalarDB Releases page and download the SQL CLI tool that matches the version of ScalarDB that you want to use.
MySQL​
To load the schema for schema-for-customer-service.sql into MySQL, run the following command, replacing <VERSION> with the version of the ScalarDB Schema Loader that you downloaded:
java -jar scalardb-cluster-sql-cli-<VERSION>-all.jar --config scalardb-cluster-node-for-customer-service-client.properties --file schema-for-customer-service.sql
Cassandra​
To load the schema for schema-for-order-service.sql into Cassandra, run the following command, replacing <VERSION> with the version of the ScalarDB Schema Loader that you downloaded:
java -jar scalardb-cluster-sql-cli-<VERSION>-all.jar --config scalardb-cluster-node-for-order-service-client.properties --file schema-for-order-service.sql
Schema details​
As shown in schema-for-customer-service.sql, all the tables for the Customer Service are created in the customer_service namespace.
customer_service.customers: a table that manages customers' informationcredit_limit: the maximum amount of money a lender will allow each customer to spend when using a line of creditcredit_total: the amount of money that each customer has already spent by using their line of credit
As shown in schema-for-order-service.sql, all the tables for the Order Service are created in the order_service namespace.
order_service.orders: a table that manages order informationorder_service.statements: a table that manages order statement informationorder_service.items: a table that manages information of items to be ordered
The Entity Relationship Diagram for the schema is as follows:
