Skip to main content
Version: 3.12

Getting Started with ScalarDB Cluster SQL via Spring Data JDBC for ScalarDB

This tutorial describes how to create a sample application by using ScalarDB Cluster SQL via Spring Data JDBC for ScalarDB. You'll be using the same sample application as found in the Sample application of Spring Data JDBC for ScalarDB.



We recommend using the LTS versions mentioned above, but other non-LTS versions may work.

In addition, other JDKs should work with ScalarDB, but we haven't tested them.

Sample application

This tutorial illustrates the process of creating a sample e-commerce application, where items can be ordered and paid for with a credit card by using ScalarDB. For details about the sample application, see the sample application for Spring Data JDBC for ScalarDB.

The following diagram shows the system architecture of the sample application:

| [Kubernetes Cluster] |
| |
| [Pod] [Pod] [Pod] |
+------------------------+ | |
| SQL CLI | | +-------+ +-----------------------+ |
| (indirect client mode) | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
+------------------------+ | | | +-------+ | | +-----------------------+ | |
| | | | | | |
| | +---------+ | +-------+ | +--------------------+ | +-----------------------+ | +------------+ |
+--+-> | Service | ---+---> | Envoy | ---+---> | Service | ---+---> | ScalarDB Cluster Node | ---+---> | PostgreSQL | |
+------------------------+ | | | (Envoy) | | +-------+ | | (ScalarDB Cluster) | | +-----------------------+ | +------------+ |
| Sample application | | | +---------+ | | +--------------------+ | | |
| with Spring Data JDBC | | | | +-------+ | | +-----------------------+ | |
| for ScalarDB | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
| (indirect client mode) | | +-------+ +-----------------------+ |
+------------------------+ | |

Step 1. Clone the ScalarDB Samples repository

$ git clone
$ cd scalardb-samples/spring-data-sample

Step 2. Modify build.gradle

To use ScalarDB Cluster, you need to modify build.gradle:

$ vim build.gradle

Then, delete the existing dependency for com.scalar-labs:scalardb-sql-direct-mode from the dependencies section, and add the following dependency to the dependencies section:

dependencies {

implementation 'com.scalar-labs:scalardb-cluster-java-client-sdk:3.12.2'

Step 3. Modify

You need to modify to connect to ScalarDB Cluster as well. But before doing so, you need to get the EXTERNAL-IP address of the service resource of Envoy (scalardb-cluster-envoy) as follows:

$ kubectl get svc scalardb-cluster-envoy
scalardb-cluster-envoy LoadBalancer localhost 60053:30641/TCP 16h

In this case, the EXTERNAL-IP address is localhost.

Next, open

$ vim

Then, modify as follows:


To connect to ScalarDB Cluster, you need to specify cluster for the scalar.db.sql.connection_mode property. In addition, you will use the indirect client mode and connect to the service resource of Envoy in this tutorial. For details about the client modes, see Developer Guide for ScalarDB Cluster with the Java API.

Step 4. Load a schema

To load a schema via ScalarDB Cluster SQL, you need to use the dedicated SQL CLI for ScalarDB Cluster (SQL CLI for Cluster). Using the SQL CLI for Cluster is basically the same as using the ScalarDB SQL Command Line Interface except the name of the JAR file is different. You can download the SQL CLI for Cluster from Releases. After downloading the JAR file, you can run SQL CLI for Cluster with the following command:

$ java -jar scalardb-cluster-sql-cli-3.12.2-all.jar --config --file schema.sql

Step 5. Modify

Then, you need to modify to connect to ScalarDB Cluster as well:

$ vim src/main/resources/

Similar to, you need to specify cluster for the spring.datasource.driver-class-name property and use the indirect client mode. To do so, modify as follows:


Step 6. Load the initial data

Before running the sample application, you need to load the initial data by running the following command:

$ ./gradlew run --args="LoadInitialData"

After the initial data has loaded, the following records should be stored in the tables:

  • For the sample.customers table:
1Yamada Taro100000
2Yamada Hanako100000
3Suzuki Ichiro100000
  • For the sample.items table:

Step 7. Run the sample application

Let's start with getting information about the customer whose ID is 1:

$ ./gradlew run --args="GetCustomerInfo 1"
{"customer_id":1,"name":"Yamada Taro","credit_limit":10000,"credit_total":0}

Then, place an order for three apples and two oranges by using customer ID 1. Note that the order format is <Item ID>:<Count>,<Item ID>:<Count>,...:

$ ./gradlew run --args="PlaceOrder 1 1:3,2:2"

You can see that running this command shows the order ID.

Let's check the details of the order by using the order ID:

$ ./gradlew run --args="GetOrder 2358ab35-5819-4f8f-acb1-12e73d97d34e"
{"order_id":"2358ab35-5819-4f8f-acb1-12e73d97d34e","timestamp":1677478005400,"customer_id":1,"customer_name":"Yamada Taro","statements":[{"item_id":1,"item_name":"Apple","price":1000,"count":3,"total":3000},{"item_id":2,"item_name":"Orange","price":2000,"count":2,"total":4000}],"total":7000}

Then, let's place another order and get the order history of customer ID 1:

$ ./gradlew run --args="PlaceOrder 1 5:1"
$ ./gradlew run --args="GetOrders 1"
[{"order_id":"46062b16-b71b-46f9-a9ff-dc6b0991259b","timestamp":1677478201428,"customer_id":1,"customer_name":"Yamada Taro","statements":[{"item_id":5,"item_name":"Melon","price":3000,"count":1,"total":3000}],"total":3000},{"order_id":"2358ab35-5819-4f8f-acb1-12e73d97d34e","timestamp":1677478005400,"customer_id":1,"customer_name":"Yamada Taro","statements":[{"item_id":1,"item_name":"Apple","price":1000,"count":3,"total":3000},{"item_id":2,"item_name":"Orange","price":2000,"count":2,"total":4000}],"total":7000}]

This order history is shown in descending order by timestamp.

The customer's current credit_total is 10000. Since the customer has now reached their credit_limit, which was shown when retrieving their information, they cannot place anymore orders.

$ ./gradlew run --args="GetCustomerInfo 1"
{"id": 1, "name": "Yamada Taro", "credit_limit": 10000, "credit_total": 10000}
$ ./gradlew run --args="PlaceOrder 1 3:1,4:1"
java.lang.RuntimeException: Credit limit exceeded. limit:10000, total:17500
at sample.SampleService.placeOrder(
at sample.SampleService$$FastClassBySpringCGLIB$$1123c447.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(
at sample.SampleService$$EnhancerBySpringCGLIB$$a94e1d9.placeOrder(<generated>)
at picocli.CommandLine.executeUserObject(
at picocli.CommandLine.access$1500(
at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(
at picocli.CommandLine$RunLast.handle(
at picocli.CommandLine$RunLast.handle(
at picocli.CommandLine$AbstractParseResultHandler.execute(
at picocli.CommandLine$RunLast.execute(
at picocli.CommandLine.execute(
at org.springframework.boot.SpringApplication.callRunner(
at org.springframework.boot.SpringApplication.callRunners(
at sample.SampleApp.main(

After making a payment, the customer will be able to place orders again.

$ ./gradlew run --args="Repayment 1 8000"
$ ./gradlew run --args="GetCustomerInfo 1"
{"customer_id":1,"name":"Yamada Taro","credit_limit":10000,"credit_total":2000}
$ ./gradlew run --args="PlaceOrder 1 3:1,4:1"

Source code of the sample application

To learn more about Spring Data JDBC for ScalarDB, you can check the source code of the sample application.

Next steps

If you have not tried the other ScalarDB Cluster tutorials, we encourage you to read the following:

For details about developing applications that use ScalarDB Cluster with the Java API, refer to the following:

For details about the ScalarDB Cluster gRPC API, refer to the following: