Spring boot: configure hsqldb datasource to persist data - java

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

Related

How is H2 database different compared to Postgresql? Does H2 create tables on its own when used in a microservice

I'm new to programming and I'm trying to read a json and store in a database using springboot, when i use H2 i dont have to create table structure it creates on its own but when i use postgres i get errors that unable to find table. is there anyway to create tables automatically in postgres ?
How do they work and postgres is not able create tables on its own, Am i doing anything wrong?`
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/MyDB
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
There's a property you need to set for PostgreSQL to automatically create the tables on startup:
spring.jpa.hibernate.ddl-auto=update
Or if you want to start from a clean database everytime:
spring.jpa.hibernate.ddl-auto=create
For embedded databases like H2, Spring Boot will set spring.jpa.hibernate.ddl-auto=create automatically
See also this 12-year old question about why you should not do that in production:
Hibernate: hbm2ddl.auto=update in production?
In production, you should manage the database tables outside of your application, or use something like Liquibase or Flyway
If the database is embedded the default value of spring.jpa.hibernate.ddl-auto is create-drop. If not, default value is none.
Check this out - https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html

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

Connect Java spring project with cloud database

I have created a spring boot project in Java, it works perfectly fine with my local database but I am not able to connect it to my GCP postgres cloud SQL instance.
I have followed the below steps: https://cloud.spring.io/spring-cloud-gcp/multi/multi__spring_jdbc.html
I also did the required changes in the pom.xml.
Is there anything I need to change in the application.properties file?
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.schema=classpath:/schema.sql
spring.datasource.continue-on-error=true
I want my project to working properly, pointing to cloud database.
Depend on which cloud you are using.
There is End Point for you created database.
That Endpoint link you have to put instead of Localhost.spring.datasource.url=jdbc:postgresql://(put your end point here instead of localhost):5432/postgres
Make sure that your database name should be same as in the link.
If you are using Spring there so you should first create database development environment.
Other than in Pom.xml you need to add Dependency of cloud.
If you want to access that DB then you have to go for JackDB platform.
Thanks & Regards,
JerryRank

HSQL DB lost when spring-boot app restarts

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.

Spring data and oracle slow data load

I'm developing an Spring-boot app with spring-data and oracle database. I have a couple of pretty simple tables but load of the data is so slow.
For example, loading 2000 rows from table that only have 3 columns (texts) lasts almost 25 seconds. I was using the spring data before but not with spring boot and it was not that slow. I have only one resource file application.properties and here it is.
spring.datasource.url = jdbc:oracle:thin:#1.2.3.4:1521:abcd
spring.datasource.username = user
spring.datasource.password = ____
spring.datasource.driverClassName = oracle.jdbc.driver.OracleDriver
spring.datasource.maxActive=15
spring.datasource.initialSize=5
spring.datasource.validationQuery=select 1
spring.datasource.removeAbandoned=false
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=true
spring.jpa.database-platform = org.hibernate.dialect.Oracle10gDialect
I've been trying with default repository implementation like findAll() as well as with custom #Query and it is the same.
Do I need some additional configuration? Any ideas?
In the configuration properties, you should use spring.jpa.properties.hibernate.jdbc.fetch_size

Categories

Resources