Struts is a lightweight Java web framework, perfect for building traditional apps—and now, with TiDB, you can easily layer in a distributed SQL database that's both scalable and MySQL-compatible. In this guide, we’ll focus only on setting up TiDB in Struts: using Docker for development and Kubernetes for production, keeping dependencies simple, configurations clean, and your environments separated.
1. Add Dependencies in pom.xml
Make sure you include the necessary libraries:
<dependencies>
<!-- Struts framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- MySQL-compatible JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
TiDB speaks the MySQL protocol, so you use the MySQL driver. With Kubernetes support, you can easily deploy safely in production.
2. Run TiDB Locally with Docker
Use Docker to spin up a local TiDB cluster:
version: '3'
services:
pd:
image: pingcap/pd:v6.6.0
ports:
- "2379:2379"
tikv:
image: pingcap/tikv:v6.6.0
tidb:
image: pingcap/tidb:v6.6.0
ports:
- "4000:4000"
depends_on:
- pd
- tikv
Start it:
docker-compose up -d
Once up, TiDB listens on
localhost:4000
, ready for development.
3. Create Dev Config: tidb-dev.properties
Add this file under
src/main/resources
:
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:4000/testdb?useSSL=false&serverTimezone=UTC
db.user=root
db.password=
db.validationQuery=SELECT 1
Load these values into your Struts helper:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/tidb-dev.properties"));
Class.forName(props.getProperty("db.driver"));
try (Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password"));
Statement stmt = conn.createStatement()) {
stmt.executeQuery(props.getProperty("db.validationQuery"));
}
You're now connected to your local TiDB instance.
4. Prepare Kubernetes Config for Production
Keep sensitive values out of code by creating ConfigMaps and Secrets:
kubectl create configmap tidb-config \
--from-literal=DB_URL=jdbc:mysql://tidb-service:4000/proddb?useSSL=false&serverTimezone=UTC \
--from-literal=DB_DRIVER=com.mysql.cj.jdbc.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic tidb-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodpass
5. Create Production Config: tidb-prod.properties
Add this to your application resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
At runtime, your application automatically uses production values via environment variables.
6. Kubernetes Deployment Snippet
Set environment variables in your container spec:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: tidb-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: tidb-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: tidb-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: tidb-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: tidb-secret
key: DB_PASSWORD
Package
tidb-prod.properties
and deploy your Struts app: it picks up the right config automatically.
7. Example Struts Action for DB Check
You can test connection health like this:
public class DbHealthAction extends ActionSupport {
public String execute() {
String url = System.getenv("DB_URL");
String user = System.getenv("DB_USER");
String pass = System.getenv("DB_PASSWORD");
String validation = System.getenv("DB_VALIDATION_QUERY");
try (Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement()) {
stmt.executeQuery(validation);
addActionMessage("Connected to TiDB successfully!");
return SUCCESS;
} catch (SQLException e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
No code change is needed between local and production environments—just switch
.properties
.
Why This Setup Works
- Fast local iteration: Docker and dev configs give quick results
- Secure production: Kubernetes Secrets keep credentials safe
- Single codebase: No code duplication — Struts logic works everywhere
- Separation of configuration: Distinct files reduce misconfiguration risk
With this setup, your Struts application works seamlessly with TiDB—using Docker for easy local dev and Kubernetes for secure production. You get a modern, distributed SQL backend without sacrificing simplicity or reliability.
image quote pre code