Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Share an h2 in-memory database among several JVMs

DZone's Guide to

Share an h2 in-memory database among several JVMs

· Java Zone
Free Resource

Find your next Integration job at DZone Jobs. See jobs focused on integration, or create your profile and have the employers come to you!

I 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/

Find your next Integration job at DZone Jobs. See jobs focused on integration, or create your profile and have the employers come to you!

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}