As I currently see it I have 5 possible database profiles
currently I have postgres configured for running the application, and h2 configured for testing via having a different application.properties
in java/resource
s vs test/resources
what's the simplest way to have the database connection information change for these scenarios?
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.
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.
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 configurationapplication-dev.properties
- Configuration for dev profileapplication-ci.properties
- Configuration for ci profilesIf 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:
-Dspring.profiles.active=ci
--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:
--spring.config.location=/srv/myapp/config.properties
-Dspring.config.location=/srv/myapp/config.properties
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With