Spring Boot Application add datasource at runtime? - spring

I working on a project which uses Spring boot , Spring Data JPA and postgres .There is a problem that can't solve .
When my application start up ,The database not ready yet . It need to add to application at runtime . But I also want to initialize a database using JPA.
just like spring.jpa.hibernate.ddl-auto:create-drop,Unfortunately Initialize a database using JPA will happen at application startup.
My question is that how to delay spring data jpa DDL generation. now we can't add a datasource at application runtime.
I am searching for a long time on net. But no use. The AbstractRoutingDataSource may be not suit for us, because we don't have a datasource at begin .
Please help or try to give some ideas how to achieve this
Thanks in advance

AbstractRoutingDataSource is not useful as it requires pre-configured datasources.
just check this stackoverflow question, it shows how you can add/remove datasources at runtime. While it doesn't support hibernate's delayed ddl creation but you can create database tables in runtime datasources using schema.sql and inserts some constants using data.sql.

Related

what is the best way to create session from java spring boot using oracle Database?

I created user in oracle database and I am trying to create session but I find many ways in spring boot so what is the easy way if I want to create classe connections using the Username and Password ?
You can jdbc template, spring data JDBC or spring data JPA, well depending on your use case.
If your data model is quite complex, you should avoid using the JDBC template as you will need to write prepared statements which can be cumbersome. JPA will allow you to use object-oriented programming principles and also will help you map the entities to your database columns.
For example, if you are going to use spring data JPA, you need to set the application properties as follows:
spring.datasource.type=oracle.oracleucp.jdbc.UCPDataSource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual
spring.datasource.oracleucp.connection-pool-name=UcpPoolBooks
spring.datasource.oracleucp.initial-pool-size=5
spring.datasource.oracleucp.min-pool-size=5
spring.datasource.oracleucp.max-pool-size=10
This would behind the scene create an Oracle Datasource. In this example, we are using Oracle Universal Connection Pooling. You can also use HikariCP which is quite popular.
check this out
If you want to use UCP with above properties then you must have SpringBoot version higher than 2.4.0.
Check out the Spring Boot code sample on GitHub.

Can we use multiple datasources with jdbi in spring boot project

Can we use multiple datasources with jdbi.
Will the configuration will be same as what we have with JPA : https://www.baeldung.com/spring-data-jpa-multiple-databases
So, in first, you can set more than one database for your application with JDBI, exactely like JDBC. You just have to set them inside your application.properties.
Second, if you want to use JDBI, you'll use a kind of classical queries, instead of a dialect for JPA/hibernate inside repositories.
You can read this discussion to compare them : Benchmarking spring data vs JDBI in select from postgres Database

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

Spring Boot - Let Hibernate initialize all datasources of AbstractRoutingDataSource

I've implemented the AbstractRoutingDataSource in order to horizontally partition my data [1]. I rely on Hibernates functionality to create and update the database schema. This works fine for the first datasource, which is resolved by the determineCurrentLookupKey() declared in AbstractRoutingDataSource.
Is there a way to apply the schema generation/update to the other datasources as well?
I've found the classes SchemaExport and SchemaUpdate respectively, that are capable of the required functionality. However, both require an instance of org.hibernate.boot.MetaData, which I don't how to obtain.
(I'm using Spring Boot in version 1.4.2.RELEASE.)
[1] https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Thanks!
I got the same issue and found a solution using the SchemaExport class of hibernate.
For each DataSourceEnum you can manually initialize the datasource.
here is my detailed answer to my own issue discription

Spring Boot app testing with database vendor specific JPA annotation

I got an app with an existing database that has Oracle only field type (e.g. "binary-float") mapped on the entity class with #ColumnDefinition annotation.
Things runs fine when running the app normally by launching the Application class.
However I can't seem to find a way to write junit tests easily. In another Spring Boot app, I have been using different profile to define a normal datasource that points to Oracle and a junit test datasource that points to the h2 in-memory db. I stayed mostly within JPAQL and common sql standard when using direct sql. Problem is, this scheme doesn't work if the JPA mapping annotation is database specific.
Any suggestions?

Resources