Share an h2 in-memory database among several JVMs
Join the DZone community and get the full member experience.
Join For FreeI hate when things are written in the docs but not clearly enough to understand it on the first time.
I wish I were the only one not to have get this right at first, but if you look in google for terms like
“share h2 database among applications”
“common h2 database among jvms”
“accessing in-memory database from another jvm”
you’ll find plenty of confused people too.
SO here is the explanation
Because you are writing some test or POC, you want to use an In-Memory Database.
H2 is cool, you can look at the tutorial and cook-up a config pretty fast and it works very well.
Then let’s say you want to share your database amongst different processes/JVMs. That’s when the documentation becomes unclear (although everything is explained there).
the first process is going to create the DB, with the following URL :
"jdbc:h2:mem:db1"
and it’s going to need to start a tcp Server
final String[] args = new String[] {
"-tcpPort", "8092",
"-tcpAllowOthers","true" };
org.h2.tools.Server server = org.h2.tools.Server.createTcpServer(args).start();
Of course do not forget to stop the server when you exit the application
server.stop();
The other processes can then access your DB by using the following URL:
"jdbc:h2:tcp://localhost:8092/mem:db1"
From http://jsoftbiz.wordpress.com/2011/04/30/share-an-h2-in-memory-database-among-several-jvms/
Opinions expressed by DZone contributors are their own.
Comments