D-rk
D-rk

Reputation: 5919

H2 Database multiple connections

I have the following issue: Two instances of an application on two different systems should share a small database. The main problem is that both systems can only exchange data through a network-folder. I don't have the possibilty to setup a database-server somewhere.

Is it possible to place a H2 database on the network-folder and let both instances connect to the database (also concurrently)?

I could connect with both instances to the db using the embedded mode if I disable the file-locking, right? The instances can perfom either READ or INSERT operations on the db. Do I risk data corruptions using multiple concurrent embedded connections?

Upvotes: 22

Views: 37318

Answers (2)

Ahmet DAL
Ahmet DAL

Reputation: 4683

As the documentation says; ( http://h2database.com/html/features.html#auto_mixed_mode )

Multiple processes can access the same database without having to start the server manually. To do that, append ;AUTO_SERVER=TRUE to the database URL. You can use the same database URL independent of whether the database is already open or not. This feature doesn't work with in-memory databases.

// Application 1:
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE");

// Application 2:
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE");

Upvotes: 61

vincenzo iafelice
vincenzo iafelice

Reputation: 270

From H2 documentation:

It is also possible to open the database without file locking; in this case it is up to the application to protect the database files. Failing to do so will result in a corrupted database.

I think that if your application use always the same configuration (shared file database on network folder), you need to create an application layer that manages concurrency

Upvotes: 2

Related Questions