Log effective URL for Spring Boot Liquibase - spring-boot

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.

Related

Spring Batch with unknown datasource

I have a working Spring Boot application which embeds a Spring Batch Job. The job is not run on a schedule, instead we kick it with an endpoint. It is working as it should. The basics of the batch are
Kick the endpoint to start the job
Reader reads from input file
Processor reads from oracle database using jpa repository and simple spring datasource config
Writer writes to output file
However there are new requirements:
The schema of the repository database is from here on unknown on application startup. The tables are the same, it is just an unknown schema. This fact is out of our control and you might think it is stupid but there are reasons for it and this cant be changed. This means that with current functionality we need to reconfigure the datasource when we know the new schema name, and restart the application. This is a job that we will run for a number of times when migrating from one system to another, so it has a limited lifecycle and we just need a "quick fix" to be able to use it without rewriting the whole app. So what I would like to do is:
Send the schema name as a query param to the application, put it in job parameters and then - get a new datasource when the processor reads from the repository. Would this be doable at all using Spring Batch? Any help appreciated!

spring-boot actuator refresh and in memory db

Is there a way to somehow configure spring-boot (2.1.1) actuator refresh or hikari/datasource so it reexecutes logic responsible for inmemory database (h2 for instance) creation?
At the moment after the /actuator/refresh I do not have previously created tables (hbm2ddl: create-drop or hbm2ddl: create) anymore.
There is no sample code of your implementation.
But Your problem seems to be not a problem as this is how the In-Memory Dbs are supposed to work.
These Dbs(H2,Hsql) are used for testing and not prefered for production but if you need for some small data than these are absolutely fine.
Also,
(hbm2ddl: create-drop or hbm2ddl: create)
Here, with this it will always recreate the db after the service is restarted.
change it to
(hbm2ddl: update) The syntax might not be correct.
Also taking into consideration you are using hibernate
I think actuator is meant for different things, to get an understanding of what happens in a microservice instance in a runtime mainly.
/actuator/refresh
indeed manages beans with Refresh Scope, it just recreates them without recreating the whole application context (which can be an expensive operation) if the configurations have changed (like in spring boot cloud config service).
So it has nothing to do with the lifecycle of H2 DataSource which is indeed used mainly for tests as our colleague Shubham has kindly stated, and and this makes the question even more confusing :)

Change the Hibernate database schema at run time in spring boot application

I have A requirement where the current schema to be used is stored in db table;(schema_a or schema_b).
The application is loaded with default schema
spring.jpa.properties.hibernate.default_schema=schema_a
Now when the data in table is updated to B I want to consider all jpa query to use schema_b without any down time at server.
Your question is not clear enough to provide some code to your question, but you can catch inspiration from the following articles:
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Spring Boot - Change connection dynamically

How to pass database name explicitly in Spring Boot Configuration?

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).

H2 databse to load data only once, but on app ending not to drop data

I gave a spring boot application that uses an embedded H2. What i need is on the first start of the application, to load the data form the data.sql. Every time the application ends, or breaks, the data should be maintained. The reason for this, is that i deploy my application on heroku, which for free use, sleeps after 30 minutes.
You have to use the database in embedded mode. See here.
So your datasource url have to be something like this:
spring.datasource.url=jdbc:h2:~/myDbFile;DB_CLOSE_ON_EXIT=FALSE
You can make your data.sql load conditionally and use a file H2 database see https://www.javatips.net/blog/h2-file-database-example

Resources