Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Database multiple connections

Tags:

h2

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?

like image 433
D-rk Avatar asked Sep 13 '12 08:09

D-rk


People also ask

How do I connect to my H2 memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .

Is H2 database thread safe?

H2 is thread-safe (as documented), but there are parts of the JDBC API that are problematic, for example it doesn't make sense to call PreparedStatement. setString + execute concurrently on the same object.

Is H2 better than SQLite?

SQLite is much more robust to corruption. Speed wise H2 was much faster in my case. With SQLite transactions are particularly costly, so you should prefer doing bulk operations within transactions or via batches.

Can I use H2 database in production?

It can be embedded in Java applications or run in the client-server mode. Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk. Because of embedded database it is not used for production development, but mostly used for development and testing.


2 Answers

I had the same issue and I've found the solution in the documentation. It can be found at; 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");
like image 118
Ahmet DAL Avatar answered Sep 17 '22 12:09

Ahmet DAL


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

like image 38
vincenzo iafelice Avatar answered Sep 18 '22 12:09

vincenzo iafelice