How to pass database name explicitly in Spring Boot Configuration? - spring

I've a Spring Boot application of mine, which connects to a Postgres database. I've specified in application.properties the datasource url as -
spring.datasource.url=jdbc:postgresql://< server ip here >:5432/mydb
The jdbc url (jdbc:postgresql://< server ip here >:5432/) is actually stored in a separate external location which my application is able to read. Therefore, I want to specify only the database name in my properties file.
I don't want to pass the database name as some environment variable since it's not going to change.
I'm stuck at this point for quite some time now, how can I achieve the same?

Add this in your application.properties file
spring.jpa.hibernate.ddl-auto=create\update\none
spring.datasource.url=jdbc:postgresql://host:port/db
spring.datasource.username=username
spring.datasource.password=password

Have you tried using ${var} syntax like:
spring.datasource.url=jdbc:postgresql://${server-ip}:5432/mydb
See:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-placeholders-in-properties

I finally implemented it this way.
Specified only database name in my application and created Datasource bean in a separate Spring Boot application (so that it can be reused across other projects as well).

Related

Log effective URL for Spring Boot Liquibase

I'm using Spring Boot 2.7. When I run a unit test, it insists on creating the Liquibase change log table either twice for what should be an H2 in memory database. I'd like to have Liquibase log the actual JDBC URL being used. I know what the properties say, but I have an application.properties, an application-h2.properties, and sometimes Spring wants to use an in memory database even though a different in memory database is used.
Is there some property like
spring.liquibase.show-effective-jdbc-url=true?
Bonus points for telling me how to log this for regular JPA access.
Thanks,
Woodsman
There is not a flag, but the effective URL is logged at FINE level. There should be a message like Connected to USER#URL where the value for url is returned from the driver itself, not just what you gave it.

I want to change some parameters of application.properties file in running application in spring boot

I am using application.properties file for some static properties like username and password but I want to change those parameters without stopping my application I was trying this with actuators but I failed.
I would also suggest to use Spring Cloud Config.
Here is a pretty good article about how this works:
https://nirajsonawane.github.io/2019/02/22/Update-Configs-Dynamically-Using-Spring-Cloud-Bus-and-Spring-Cloud-Config/

How can I make my flyway default DB pointing to different db and my actual Spring boot Application to some other DB

I am trying to integrate flyway to my existing Spring boot app. My requirement is, the default table created by flyway needs to be created on different DB schema and my applications actual table migration must happen on different schema. Reason being, no addition table must be created on the applications DB schema. Currently we are using Mysql
We had a Spring Boot app with the flyway configuration in application.properties file for different environments:
flyway.enabled=true
flyway.url=jdbc:postgresql://localhost:5432/finance
flyway.schemas=user
flyway.password=password
flyway.user=postgres
flyway.baseline-on-migrate=true
We had the application-dev.properties, application-local.properties and application-aws.properties with the configuration specific to the environment
Flyway provides a property flyway.schemas. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.
https://flywaydb.org/documentation/commandline/migrate.html
ex:flyway.schemas=schema1,schema2,schema3

application.properties configuration for distributed database pattern

I am trying to develop a microservice by using sprin and spring boot with postgresql database. I am here using distributted datbase. So for particular region I am using one DB, and for other region I am using different DB. Currently I only tried with one database. I added datasource name , username and password in application.properties.
Here my doubt is that, if I am using multiple distributed database, how cam mention different DB source URL in configuration (application.properties)? I am using following structure to use one database currently,
spring.datasource.url=jdbc:postgresql://localhost/milleTech_users
spring.datasource.username=postgres
spring.datasource.password=postgresql
spring.jpa.generate-ddl=true
Like above.
So if I am using multiple DB for multiple region How I can give configuration conditionally here? I am new to microservice world and distributed database design pattern.
Multiple Database details cannot be managed within a single application.properties.
Consider using Spring Cloud Config where in you can create multiple application.properties with different profile names for every application.
In your case, the profile names could reflect the region. When you deploy to a particular region, launch the app with that profile name so that the required config would be loaded and appropriate database connection would be used
Edit :
Also in your case, if you can set environment variables, you can explore on the following option mentioned in this thread

Having spring bean properties refreshed automatically from properties file

I'm using Spring 2.5.6. I have a bean whose properties are being assign from a property file via a PropertyPlaceholderConfigurer. I'm wondering whether its possible to have the property of the bean updated when the property file is modified. There would be for example some periodic process which checks the last modified date of the property file, and if it has changed, reload the bean.
I'm wondering if there is already something that satisfies my requirements. If not, what would be the best approach to solving this problem?
Thanks for your help.
Might also look into useing Spring's PropertyOverrideConfigurer. Could re-read the properties and re-apply it in some polling/schedular bean.
It does depend on how the actual configured beans use these properties. They might, for example, indirectly cache them somewhere themself.
If you want dynamic properties at runtime, perhaps another way to do it is JMX.
One way to do this is to embed a groovy console in your application. Here's some instructions. They were very simple to do, btw - took me very little time even though I'm not that familiar with groovy.
Once you do that you can simply go into the console and change values inside the live application on the fly.
You might try to use a custom scope for the bean that recreates beans on changes of the properties file. See my more extensive answer here.
Spring Cloud Config has facilities to change configuration properties at runtime via the Spring Cloud Bus and using a Cloud Config Server. The configuration or .properties or .yml files are "externalized" from the Spring app and instead retrieved from a Spring Cloud Config Server that the app connects to on startup. That Cloud Config Server retrieves the appropriate configuration .properties or .yml files from a GIT repo (there are other storage solutions, but GIT is the most common). You can then change configuration at runtime by changing the contents of the GIT repo's configuration files--The Cloud Config Server broadcasts the changes to any Client Spring applications via the Spring Cloud Bus, and those applications' configuration is updated without needing a restart of the app. You can find a working simple example here: https://github.com/ldojo/spring-cloud-config-examples

Resources