#1
If you're building a web app with Struts and need a reliable relational database, MariaDB is a fantastic choice. It's fast, compatible with MySQL, and easy to manage. In this guide, we'll walk through configuring MariaDB for both local development using Docker and production deployment on Kubernetes—while keeping your application configuration tidy and environment-specific.

1. Add Essential Dependencies

Include the necessary libraries in your pom.xml:
<dependencies>
  <!-- Struts core framework -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts.version}</version>
  </dependency>

  <!-- MariaDB JDBC driver -->
  <dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>${mariadb.version}</version>
  </dependency>

  <!-- Kubernetes client (optional) -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    lt;version>${fabric8.version}</version>
  </dependency>
</dependencies>
This setup equips your app with Struts, JDBC connectivity to MariaDB, and optional Kubernetes capability for production configuration.

2. Launch MariaDB Locally with Docker

Create this docker-compose.yml for your local environment:
version: '3'
services:
  mariadb:
    image: mariadb:10.11
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: devdb
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    ports:
      - "3306:3306"
Run the container:
docker-compose up -d
Your app can now connect locally to MariaDB at localhost:3306, with credentials ready.

3. Dev Configuration: mariadb-dev.properties

In your src/main/resources, create:
db.driver=org.mariadb.jdbc.Driver
db.url=jdbc:mariadb://localhost:3306/devdb
db.user=devuser
db.password=devpass
db.validationQuery=SELECT 1
Then use it in a Struts DAO or helper:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/mariadb-dev.properties"));

Class.forName(props.getProperty("db.driver"));
Connection conn = DriverManager.getConnection(
    props.getProperty("db.url"),
    props.getProperty("db.user"),
    props.getProperty("db.password")
);

// Optionally verify connection
conn.createStatement().executeQuery(props.getProperty("db.validationQuery"));
Great, you're now connected to your development database!

4. Prepare Kubernetes for Production

Avoid hardcoding credentials by storing them in Kubernetes:
kubectl create configmap mariadb-config \
  --from-literal=DB_URL=jdbc:mariadb://mariadb-svc:3306/proddb \
  --from-literal=DB_DRIVER=org.mariadb.jdbc.Driver \
  --from-literal=DB_VALIDATION_QUERY=SELECT 1

kubectl create secret generic mariadb-secret \
  --from-literal=DB_USER=produser \
  --from-literal=DB_PASSWORD=prodpass

5. Production Configuration: mariadb-prod.properties

Add this to your JAR resources:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
Your application will read these environment variables in production.

6. Kubernetes Deployment Snippet

Make sure your Pod spec includes:
env:
  - name: DB_DRIVER
    valueFrom:
      configMapKeyRef:
        name: mariadb-config
        key: DB_DRIVER
  - name: DB_URL
    valueFrom:
      configMapKeyRef:
        name: mariadb-config
        key: DB_URL
  - name: DB_VALIDATION_QUERY
    valueFrom:
      configMapKeyRef:
        name: mariadb-config
        key: DB_VALIDATION_QUERY
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: mariadb-secret
        key: DB_USER
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: mariadb-secret
        key: DB_PASSWORD
Add the .properties file to your WAR or JAR, and Struts will load it dynamically based on the environment.

7. Example in a Struts Action

Want to test the connection? Use this snippet in a Struts action:
public class DbAction 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 MariaDB successfully!");
      return SUCCESS;
    } catch (Exception e) {
      addActionError("Connection failed: " + e.getMessage());
      return ERROR;
    }
  }
}
This code works identically in both development and production—with only the config changing.

Why This Setup Works

  • Fast local development: Docker and .properties make it easy to get running.
  • Production safety: Credentials stay out of code and are managed via Kubernetes.
  • Seamless code reuse: Same code works in both environments.
  • Clear separation: Dev and prod configs are clearly organized.
By combining Docker, Kubernetes, and environment-driven properties, you get a Struts application that’s both developer-friendly and production-ready. Set up is straightforward, transitions are clean, and your database connectivity stays reliable and secure from local to cloud. Good luck building powerful, data-driven web apps with Struts and MariaDB!

image quote pre code