Setting up OracleDB in a Spring Boot application doesn’t have to be complicated. By using Docker for development and Kubernetes for production, you can ensure your setup is consistent and secure. And with Spring Boot's support for profile-based configurations via
application.properties
, you can easily separate dev and prod settings to keep things clean and manageable.
In this guide, we’ll walk through configuring OracleDB using Docker locally and setting up environment-specific files (
application-dev.properties
and
application-prod.properties
). We’ll also explore how to pull production configuration from Kubernetes ConfigMaps and Secrets.
1. Add the Required Dependencies
First, make sure your
pom.xml
includes the Oracle JDBC driver (note: Oracle requires you to manually install it due to licensing restrictions) and optionally dependencies for Kubernetes config handling.
<dependencies>
<!-- Oracle JDBC driver (manually install to your local Maven repository) -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<!-- Spring Boot JPA support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- (Optional) Spring Cloud Kubernetes for auto-config support -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-fabric8</artifactId>
</dependency>
</dependencies>
2. Run OracleDB with Docker for Local Development
Use the
gvenzl/oracle-free
image to run OracleDB locally with Docker:
docker run -d --name oracledb \
-p 1521:1521 \
-e ORACLE_PASSWORD=devpass \
-e APP_USER=devuser \
-e APP_USER_PASSWORD=devpass \
gvenzl/oracle-free:latest
This will launch OracleDB with user
devuser
and password
devpass
, listening on the default port
1521
.
3. Configure application-dev.properties
Now create a
src/main/resources/application-dev.properties
file for your development settings:
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/ORCLCDB
spring.datasource.username=devuser
spring.datasource.password=devpass
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
To activate this configuration locally, run your app with:
SPRING_PROFILES_ACTIVE=dev
Or pass it via Maven:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
4. Prepare OracleDB for Kubernetes Production
In production, it’s best practice not to hardcode any configuration. Instead, store values in Kubernetes ConfigMaps and Secrets.
4.1: Create a ConfigMap for General Oracle Settings
apiVersion: v1
kind: ConfigMap
metadata:
name: oracle-config
data:
spring.datasource.url: jdbc:oracle:thin:@oracle-service:1521/ORCLCDB
spring.datasource.driver-class-name: oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto: validate
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.Oracle12cDialect
4.2: Create a Secret for Credentials
apiVersion: v1
kind: Secret
metadata:
name: oracle-secret
type: Opaque
data:
spring.datasource.username: cHJvZHVzZXI= # base64 for 'produser'
spring.datasource.password: cHJvZHBhc3M= # base64 for 'prodpass'
5. Configure application-prod.properties
This configuration will read values from the environment, which will be provided by Kubernetes:
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.datasource.driver-class-name=${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO}
spring.jpa.properties.hibernate.dialect=${SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT}
Then, in your Kubernetes Deployment YAML, mount the environment variables from the ConfigMap and Secret:
env:
- name: SPRING_PROFILES_ACTIVE
value: prod
- name: SPRING_DATASOURCE_URL
valueFrom:
configMapKeyRef:
name: oracle-config
key: spring.datasource.url
- name: SPRING_DATASOURCE_DRIVER_CLASS_NAME
valueFrom:
configMapKeyRef:
name: oracle-config
key: spring.datasource.driver-class-name
- name: SPRING_JPA_HIBERNATE_DDL_AUTO
valueFrom:
configMapKeyRef:
name: oracle-config
key: spring.jpa.hibernate.ddl-auto
- name: SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT
valueFrom:
configMapKeyRef:
name: oracle-config
key: spring.jpa.properties.hibernate.dialect
- name: SPRING_DATASOURCE_USERNAME
valueFrom:
secretKeyRef:
name: oracle-secret
key: spring.datasource.username
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: oracle-secret
key: spring.datasource.password
6. Verifying Your Oracle Connection
To verify that your app is properly connected to OracleDB, try injecting a repository and querying a simple entity:
@RestController
public class HealthCheckController {
@Autowired
private YourEntityRepository repo;
@GetMapping("/ping")
public ResponseEntity<String> ping() {
return ResponseEntity.ok("Connected: " + repo.count());
}
}
If everything is wired correctly, you should get a valid response from
/ping
.
image quote pre code