DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Share an h2 in-memory database among several JVMs

Share an h2 in-memory database among several JVMs

Aurelien Broszniowski user avatar by
Aurelien Broszniowski
·
Aug. 26, 11 · Interview
Like (0)
Save
Tweet
Share
13.33K Views

Join the DZone community and get the full member experience.

Join For Free

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/

In-memory database Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Common Firewall Misconfigurations and How to Address Them
  • Microservices 101: Transactional Outbox and Inbox
  • DevOps for Developers — Introduction and Version Control
  • A Deep Dive Into AIOps and MLOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: