#1
Struts is a classic Java web framework, and pairing it with SAP HANA gives you a powerful in-memory analytics backend. Thanks to Docker, setting up a development instance is quick, and Kubernetes makes production deployments clean and maintainable. This guide will walk you through configuring Struts to connect to SAP HANA using Docker locally, and environment-specific configs in Kubernetes without cluttering your application code.

1. Add Dependencies in pom.xml

Begin by including Struts, the SAP HANA JDBC driver, and optional Kubernetes support:
<dependencies>
  <!-- Struts web framework -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts.version}</version>
  </dependency>

  <!-- SAP HANA JDBC driver -->
  <dependency>
    <groupId>com.sap.cloud.db.jdbc</groupId>
    <artifactId>ngdbc</artifactId>
    <version>${sap.hana.jdbc.version}</version>
  </dependency>

  <!-- Kubernetes client for production config -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>${fabric8.version}</version>
  </dependency>
</dependencies>
With these, your Struts app can support Docker-powered HANA development and read production settings from Kubernetes.

2. Run SAP HANA Express in Docker for Development

SAP Labs provides an official SAP HANA Express image suitable for Linux.
To start a local container:
docker pull saplabs/hanaexpress
docker run -d \
  --name hana-dev \
  -p 39013:39013 \
  -p 39041:39041 \
  saplabs/hanaexpress \
  --agree-to-sap-license \
  --passwords-url file:///hana/mounts/hxe-pass.json
Make sure to prepare a JSON file on your host with your SYSDBA password, and inject it into /hana/mounts/hxe-pass.json.
Ports:
  • 39013: SystemDB (SYSTEMDB)
  • 39041: Tenant DB (HXE)

3. Development Config: hana-dev.properties

Place this under src/main/resources:
db.driver=com.sap.db.jdbc.Driver
db.url=jdbc:sap://localhost:39041/?databaseName=HXE
db.user=SYSTEM
db.password=YourHanaPass
db.validationQuery=SELECT CURRENT_USER FROM DUMMY
Use this snippet in your Struts DAO:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/hana-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"));
}
This confirms your application can talk to HANA locally.

4. Set Up Kubernetes Config for Production

In production, manage your connection securely with ConfigMaps and Secrets:
kubectl create configmap hana-config \
  --from-literal=DB_DRIVER=com.sap.db.jdbc.Driver \
  --from-literal=DB_URL=jdbc:sap://hana-svc:39041/?databaseName=HXE \
  --from-literal=DB_VALIDATION_QUERY=SELECT CURRENT_USER FROM DUMMY

kubectl create secret generic hana-secret \
  --from-literal=DB_USER=SYSTEM \
  --from-literal=DB_PASSWORD=ProdHanaPassword
This ensures credentials are not baked into your application.

5. Production Config: hana-prod.properties

Include this as a resource file:
db.driver=${DB_DRIVER}
db.url=${DB_URL}
db.user=${DB_USER}
db.password=${DB_PASSWORD}
db.validationQuery=${DB_VALIDATION_QUERY}
When the application starts, these placeholders resolve against the environment variables provided by Kubernetes.

6. Kubernetes Deployment Snippet

Update your app’s Kubernetes Deployment spec to include:
env:
  - name: DB_DRIVER
    valueFrom:
      configMapKeyRef:
        name: hana-config
        key: DB_DRIVER
  - name: DB_URL
    valueFrom:
      configMapKeyRef:
        name: hana-config
        key: DB_URL
  - name: DB_VALIDATION_QUERY
    valueFrom:
      configMapKeyRef:
        name: hana-config
        key: DB_VALIDATION_QUERY
  - name: DB_USER
    valueFrom:
      secretKeyRef:
        name: hana-secret
        key: DB_USER
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: hana-secret
        key: DB_PASSWORD

7. Struts Health-Check Action

Here’s an example Struts action to test connectivity:
public class DbCheckAction extends ActionSupport {
  public String execute() {
    String file = System.getenv("DB_URL") != null
      ? "/hana-prod.properties"
      : "/hana-dev.properties";
    Properties props = new Properties();
    props.load(getClass().getResourceAsStream(configFile));
    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"));
      addActionMessage("Connected to SAP HANA successfully!");
      return SUCCESS;
    } catch (Exception e) {
      addActionError("Connection failed: " + e.getMessage());
      return ERROR;
    }
  }
}
This unified code works in both environments, switching configurations automatically.

Why This Works

  • Fast local prototyping: Docker image spins up SAP HANA Express easily.
  • Secure production settings: Kubernetes handles sensitive configs.
  • One codebase: No code branching—just different .properties files.
  • Smooth environment transition: Properties based on environment variables ensure clarity and avoid mistakes.
  • Clean separation: Dev vs. prod configs prevent cross-contamination.
With Docker for local development and Kubernetes for production deployment, your Struts application can robustly connect to SAP HANA across environments—without tangled configuration or code changes. Happy building with in-memory analytics!

image quote pre code