I set up Spring-boot to work with H2 in-memory database
application.properties
file is in the /config
directory
and it looks like, this file is processed
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/myconsole
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=data.sql
This file is processed and the console appears at /myconsole
But the schema.sql
and data.sql
are not processed and db is empty.
I placed schema.sql
and data.sql
files both under /config
and /src/main/resources
.
SQL language instructions are correct and I can populate the table using console input.
Another strange thing is even though i name db as
spring.datasource.url=jdbc:h2:mem:mydb
the spring console loads another database testdb
o.s.j.d.e.EmbeddedDatabaseFactory --- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
How to load the H2 database correctly?
Syntax. Following is the basic syntax of INSERT INTO statement. INSERT INTO tableName { [ ( columnName [,...] ) ] { VALUES { ( { DEFAULT | expression } [,...] ) }
You can simply create a import. sql file in src/main/resources and Hibernate will execute it when the schema is created.
You could run a script, or just a statement or two: String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST" String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;" + "SET SCHEMA TEST"; String url = "jdbc:h2:mem;" + "INIT=RUNSCRIPT FROM '~/create.
H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode. H2 supports a sub set of the SQL standard. H2 also provides a web console to maintain the database. You need very little configuration to connect Spring Boot application with H2.
For embedded databases Spring Boot loads schema.sql and data.sql automatically from classpath. For Relational Databases, have to set spring.datasource.initialization-mode=always to load schema.sql and data.sql
This property was introduced in Spring Boot 2.5.0; we need to use spring.datasource.initialization-mode if we are using previous versions of Spring Boot. 6. @Sql Spring also provides the @Sql annotation — a declarative way to initialize and populate our test schema.
By default, Spring Boot automatically creates the schema of an embedded DataSource. If we need to control or customize this behavior, we can use the property spring.sql.init.mode.
Resolved the issue.
The spring boot app requires its ownd jdbc dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
NON-boot dependency, that I had, is NOT enough alone:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Without the "spring-boot-starter-jdbc" dependency "spring.datasource.url" settings in file "application.properties" are not processed. That file is actually processed, but not the jdbc settings. Spring boot will create its own testdb in the memory, which destroys the data after closing the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With