1
votes

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

1
are you talking about production? - Amer Qarabsa
@AmerQarabsa I am sorry , I can't understand what you means - tanghuailong
why you want to delay the creation of database? - Amer Qarabsa
@AmerQarabsa beacause we need a mini project . The database require to add at runtime. - tanghuailong
@AmerQarabsa The database not ready when The mini project start up - tanghuailong

1 Answers

1
votes

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.