Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why H2 file database data get cleared every time

Tags:

spring-boot

h2

I use H2 database for a test application, using Spring boot. Every time, when I restart the spring boot app, the data in H2 gets cleared. I am using a file instead of memory. I set the spring.jpa.hibernate.ddl-auto=update in application.properties too. Here is my application.properties file

spring.datasource.url=jdbc:h2:file:./data/demo
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update

What am I doing wrong here. It saves data fine. But once I shut down the app, all data get cleared.

like image 923
Shehan Simen Avatar asked May 27 '26 03:05

Shehan Simen


1 Answers

I found the mistake I was doing. I had data.sql file in resources and every time Spring boot starts the app, it runs this script. In that script I was dropping and recreating all the tables. Once I removed those sql statements, it works perfect. Data is persistent to the file and won't be erased after the server restart.

like image 55
Shehan Simen Avatar answered May 30 '26 03:05

Shehan Simen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!