Home > Net >  how to make H2 in memory database work without having to populate the data.sql file in springboot
how to make H2 in memory database work without having to populate the data.sql file in springboot

Time:04-18

This might be a very simple question but I have tried to search or google how can I make it work with empty data.sql file and cant really find an answer, when you start your springboot application and you are using h2 database it always fails unless you populate the file, is there anyway to make it ignore the fact the file is empty and still be able to have an h2 database? The following is the error you get if you have it empty:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [URL ["pathTo"/data.sql]]; nested exception is java.lang.IllegalArgumentException: 'script' must not be null or empty

my application properties:

spring.datasource.url=jdbc:h2:mem:citizenDB
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

CodePudding user response:

Try to set property spring.sql.init.mode=never (application.yml or system env varible) in accordance with https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization.using-basic-sql-scripts

  • Related