Deepboy
Deepboy

Reputation: 531

How to persist data in H2 database

My application has 3 components,

1) A user facing component that receives requests and stores into the DB 2) A backend component that reads data form the DB, processes it and sends it on to an external system. 3) A DB that stores user input

enter image description here I am currently testing using H2 in memory database (NOT file based) from eclipse. I want to test end to end, but the problem is I have stop the user facing component and start the backend component. So, the DB gets created from scratch each time I start the backend component.

How do I test so that the flow is like this: user enters data -> data persisted into DB -> Baackend connects to the same persisted data -> processes data + passes to external system?

NOTE: I tried using ddl-auto: update, but it doesn't work.

Upvotes: 1

Views: 8135

Answers (3)

Angad Singh
Angad Singh

Reputation: 1

A database which is in memory cannot persist data because each time the application is reloaded the memory is wiped out. Consider using disk storage (embedded mode) or cloud-based storage (server mode) of the database.

Upvotes: -1

Om Parkash
Om Parkash

Reputation: 3

This database supports multiple connection modes and connection settings. This is achieved using different database URLs. Please see the link below for official documentation.

Reference: https://www.h2database.com/html/features.html#database_url

Upvotes: 0

Sundararaj Govindasamy
Sundararaj Govindasamy

Reputation: 8495

In addition to spring.jpa.hibernate.ddl-auto=update, set auto_reconnect as true.

Example:

spring.datasource.url=jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.driver-class-name=org.h2.Driver

Upvotes: 5

Related Questions