springspring-bootpersistenceh2

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.


Solution

  • 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