1

Is it possible to use essentially two H2 databases within the same application: in-memory for some types of data and file storage for other data?

My application uses spring boot/batch. Spring automatically stores some metadata in H2. I have no control over this and generally don't care about persisting it - which is why I want it stored in memory.

However, I also have application specific data that I wanted persisted - which I want I want to store it in a file.

Is this possible?

  • Define two data sources: one in-memory and one backed by a file, then configure Spring Batch to use the in-memory one: https://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch – Mahmoud Ben Hassine Jul 06 '20 at 08:24

1 Answers1

0

You want to define multiple datasources & assign one of them as primary.

Connect Spring Boot application with H2 : In most situations, just adding the H2 runtime jar into dependencies should be sufficient.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

A suggested approach to make the configuration easy would be to create separate configuration classes, thus separating the repository packages etc.

Minimal configuration required to run H2 as a persisted database with Spring Boot :

Update you application.properties as :

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

# Datasource
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update

Please got through the docs for better understanding : Configure Two Data Sources

Amit kumar
  • 2,169
  • 10
  • 25
  • 36
  • This makes sense. However, I want the Spring batch metadata stored in memory. I do not control those transactions, which makes this two data source approach difficult. How can I essentially make Spring batch store everything in memory while having my application data stored in a file? – another_user10293412 Jul 06 '20 at 00:11