HSQL DB lost when spring-boot app restarts - java

I'm using liquibase to initialize my db in my springboot app and it works fine until I restart - the db is re-initialized wiping all the data.
Here are my application properties
# Liquibase
liquibase.change-log=classpath:/db/changelog/iot-db.xml
liquibase.check-change-log-location=true
# Hibernate
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=
entitymanager.packagesToScan=com.whatever
Are there properties which will allow me to create a persistent db instead of an in memory db?

in application.properties, set this property:
spring.jpa.hibernate.ddl-auto = validate
source and more info.

Related

Spring Boot: how to connect to external h2 database

I have created one Spring boot application and I am not using in-memory H2 database, instead I have installed exe for H2 database and using it externally. Now I want to connect my Spring boot app with this external H2 database. I have added the dependencies, I have added all the required properties in application.properties file (you can see below). Also I have created one Entity class having #Entity annotation. But when I am trying to connect with the database it is connecting even with different URL and username and I cannot see my Entity class table over there. So where am I doing a mistake and what are the things which I have missed, please address me on this.
For your information, my Spring boot app is running on port - 8080 and H2 database is running on 8082
spring.h2.driverClassName = org.h2.Driver
spring.h2.url = jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE; AUTO_RECONNECT=TRUE
spring.h2.username = QW
spring.h2.password = root
spring.h2.console.enabled = true
spring.datasource.platform = h2
You can try this
spring.datasource.url=jdbc:h2:tcp://localhost:8082/~/<DB-NAME>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=org.h2.Driver
Give in properties file
# H2
spring.datasource.url=<your value
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Check this tutorial below link
https://howtodoinjava.com/spring-boot2/h2-database-example/
https://www.baeldung.com/spring-boot-h2-database

What is the default name of embedded H2 database in Spring Boot?

As I've read the default name of the embedded H2 database in Spring Boot should be testdb, but if I try to connect to with the H2 Console, I get the following error:
Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments)
It works only if I set the name explicitly in the application.properties with the following parameter:
spring.datasource.url=jdbc:h2:mem:testdb
Since the application can connect to the embedded database without this configuration, there must be a different default name. But what is the default name of the automatically configured database?
Enable h2 console in application.properties as jurez said
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
and then when you run the application in the console you will see something like this:
H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc'
then you can use this JDBC URL to connect to the database from the h2-console
http://localhost:8080/h2-console
jdbc:h2:mem:2f5b7da9-0cd2-4fdd-98d2-5ef5f76391bc
you can specify your own name by adding the following property in the application.property file
spring.datasource.url=jdbc:h2:mem:testdb
remember since this is an in-memory database it will be dropped and recreated every time you run your application
h2 also has a persistent mode but it is not recommended, you can do that by adding the following configurations taken from the following tutorial Spring Boot and H2 in memory database - Why, What and How?
spring.datasource.name=yourdbname
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.initialize=false
spring.datasource.url=jdbc:h2:file:~/yourdbname;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
spring.jpa.hibernate.ddl-auto = update
By default, memory databases are only accessible to the process in which they are running.
If you want to access it from H2 Console, you need to enable it in application.properties:
spring.h2.console.enabled=true

How to get ddl auto generated script in Spring boot JPA/Hibernate?

Is there a way to quickly get initial SQL script for the database that is being generated by hibernating through ddl-auto property?
I would like to get an initial SQL script for the flyway as a quick start.
use property spring.jpa.show-sql = true and it will print all script on console in format.
use ddl-auto as you did and take backup of schema for your respective DB and use that as initial script
example - mysql , postgres
You can see the DDL script generated by Hibernate in a Spring boot application by setting the logging level of Hibernate to debug mode in the application.properties file of your project.
logging.level.org.hibernate.SQL=DEBUG

Spring-boot application, H2 embedded database - but tables disappear when the application closes

My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.

Spring boot: configure hsqldb datasource to persist data

I am trying to create a spring boot app. I want to persist data using hsql database.
first I did include hsqldb maven dependency and springboot auto-configure is working fine, but after app restart data is lost.
Then I did include following configuration in application.peoprties file
spring.datasource.url = jdbc:hsqldb:file:testdb.script
spring.datasource.username = SA
spring.datasource.password =
#JPA properties
spring.jpa.generate-ddl=true
But data is still not persisted.
What should be configuration for persisting data?
Thanks for helping.
The default for spring.jpa.hibernate.ddl-auto is create-drop if you use an embedded database. Update this in your properties to spring.jpa.hibernate.ddl-auto=validate or none

Categories

Resources