Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide different database configurations with Spring Boot?

As I currently see it I have 5 possible database profiles

  • CI testing -> h2 mem
  • developer environment (could be test or app run) -> h2 mem, or h2 file, or postgres
  • production -> postgres (ideally credentials not stored in the git/war)

currently I have postgres configured for running the application, and h2 configured for testing via having a different application.properties in java/resources vs test/resources

what's the simplest way to have the database connection information change for these scenarios?

like image 282
xenoterracide Avatar asked Jan 18 '15 06:01

xenoterracide


People also ask

How do I configure a different database in spring boot?

Multiple Databases in Spring Boot The interesting part is annotating the data source bean creation method with @ConfigurationProperties. We just need to specify the corresponding config prefix. Inside this method, we're using a DataSourceBuilder, and Spring Boot will automatically take care of the rest.

Can we configure multiple database in spring boot?

Hence, in this article we will be discussing about creating multiple database connections with JPA using spring boot through a single application. Actually, spring boot provides a very convenient way to use multiple datasources in a single application with properties file configurations.


2 Answers

As M. Deinum mentions in his comment, the simplest way to do this is to use profile specific configuration.

Spring Boot allows you to have one common configuration file (application.properties) and then multiple other files, each specific to a profile (application-${profile}.properties).

For instance:

  • application.properties - Common configuration
  • application-dev.properties - Configuration for dev profile
  • application-ci.properties - Configuration for ci profiles

If your application runs with "ci" profile for instance, the default configuration file as well as the ci configuration file (which would contain the datasource configuration properties for ci profile) will be loaded.

To switch profiles you can use one of the following options:

  • JVM property: -Dspring.profiles.active=ci
  • Command line switch: --spring.profiles.active=dev

For unit tests you can use @ActiveProfiles("test") annotation on your test classes to tell Spring that unit tests should be run with test profile.

Also if you don't want to store production database credentials along with your source code, you can specify external configuration file when you deploy your app in production:

  • Using command line switch: --spring.config.location=/srv/myapp/config.properties
  • Using a JVM property: -Dspring.config.location=/srv/myapp/config.properties
like image 159
Bohuslav Burghardt Avatar answered Sep 28 '22 09:09

Bohuslav Burghardt


Compact answer for the above scenario would be by creating a single application.yml file and creating different profiles based on the requirement, in your case -dev, -ci and -prod and providing the DB information accordingly.

Sample example is:

spring:   profiles.active: development  --- spring:   profiles: development datasource:   db-person:       url: jdbc:oracle:thin:@db_person_dev       username: username       password: pwd       driver-class-name: oracle.jdbc.OracleDriver       test-on-borrow: true       validation-query: SELECT 1 FROM dual   db-contract:       url: jdbc:oracle:thin:@db_contract_dev       username: username       password: pwd       driver-class-name: oracle.jdbc.OracleDriver       test-on-borrow: true       validation-query: SELECT 1 FROM dual      ---      spring:       profiles: test     datasource:       db-person:           url: jdbc:oracle:thin:@db_person_test           username: username           password: pwd           driver-class-name: oracle.jdbc.OracleDriver           test-on-borrow: true           validation-query: SELECT 1 FROM dual       db-contract:           url: jdbc:oracle:thin:@db_contract_test           username: username           password: pwd           driver-class-name: oracle.jdbc.OracleDriver           test-on-borrow: true           validation-query: SELECT 1 FROM dual      ---      spring:       profiles: production     datasource:       db-person:           url: jdbc:oracle:thin:@db_person_prod           username: username           password: pwd           driver-class-name: oracle.jdbc.OracleDriver           test-on-borrow: true           validation-query: SELECT 1 FROM dual       db-contract:           url: jdbc:oracle:thin:@db_contract_prod           username: username           password: pwd           driver-class-name: oracle.jdbc.OracleDriver           test-on-borrow: true           validation-query: SELECT 1 FROM dual      --- 

For further understanding and simple example you can refer this link.

like image 33
Santosh Anantharamaiah Avatar answered Sep 28 '22 09:09

Santosh Anantharamaiah