简体   繁体   中英

How to use H2 DB for multiple instances of same application

We have a simple Java application developed using Spring Batch framework to load the data that we receive in form of files into the database. We have used H2 database to store the statistics of how many records read/loaded/failed. We get situations where we are required to load multiple files into different tables at same time, that means, running multiple instances of the same Jar file. The problem is, H2 database doesn't provide more than one connection at a time and it repeatedly throws the File is locked exception when running the second instance.

Platform: AIX

Java Version: 1.8

H2 DB Version: 1.4.193

H2 Config in application.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./h2/DataLoaderApp
spring.datasource.username=sa
spring.datasource.password=

I have tried different solutions but nothing seems to work at all.

Try 1: This did not work

Try 2: Appended FILE_LOCK=NO, it still throws File Locked exception though I do not see the *lock.db file being created.

Please help me out, Thanks a lot for your time.

Take a look on H2 manual for server-mode connection .
Change your connection url to jdbc:h2:tcp://localhost/~/DataLoaderApp and before make a connection, H2db instance must be running. You can execute a standalone H2 instance before run your app or start H2 in main method:

public static void main(String[] args) {
        Server.createTcpServer().start();
        SpringApplication.run(YourApplication.class, args);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM