- Replies:
- 0
- Words:
- 6535

pom.xml
<dependencies>
<!-- Struts framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- PostgreSQL driver (compatible with CockroachDB) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<!-- Kubernetes client (optional for production) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
This setup gives your Struts app JDBC connectivity with CockroachDB and the ability to support Kubernetes deployment.
version: '3'
services:
cockroach:
image: cockroachdb/cockroach:v23.2.6
command: start-single-node --insecure --listen-addr=0.0.0.0
ports:
- "26257:26257"
- "8080:8080" # Web UI
Then run
docker-compose up -d
CockroachDB will be ready at localhost:26257
, and its admin UI at http://localhost:8080
.
cockroach-dev.properties
src/main/resources
:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:26257/defaultdb?sslmode=disable
db.user=root
db.password=
db.validationQuery=SELECT 1
Load it in Struts helper or DAO:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/cockroach-dev.properties"));
Class.forName(props.getProperty("db.driver"));
Connection conn = DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password")
);
conn.createStatement().executeQuery(props.getProperty("db.validationQuery"));
You're now connected and ready to develop.
kubectl create configmap cockroach-config \
--from-literal=DB_URL=jdbc:postgresql://cockroach-service:26257/proddb?sslmode=disable \
--from-literal=DB_DRIVER=org.postgresql.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic cockroach-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodPass123
These store connection settings outside of your application code.
cockroach-prod.properties
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
At runtime, Struts helper or DAO will load environment variables.
Ensure your application Container spec includes:
env:
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: cockroach-config
key: DB_DRIVER
- name: DB_URL
valueFrom:
configMapKeyRef:
name: cockroach-config
key: DB_URL
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: cockroach-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: cockroach-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: cockroach-secret
key: DB_PASSWORD
Add the cockroach-prod.properties
to your deployment JAR. The Struts app then reads settings at runtime.
public class DbHealthAction extends ActionSupport {
public String execute() {
try (Connection conn = DriverManager.getConnection(
System.getenv("DB_URL"),
System.getenv("DB_USER"),
System.getenv("DB_PASSWORD"));
Statement stmt = conn.createStatement()) {
stmt.executeQuery(System.getenv("DB_VALIDATION_QUERY"));
addActionMessage("Connected to CockroachDB successfully!");
return SUCCESS;
} catch (Exception e) {
addActionError("Connection failed: " + e.getMessage());
return ERROR;
}
}
}
No code changes are needed for dev vs production—just switch .properties
.
cockroach-dev.properties
vs cockroach-prod.properties
.
image quote pre code