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
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Presentation and Use of H2 Database Engine

Presentation and Use of H2 Database Engine

Baptiste Wicht user avatar by
Baptiste Wicht
·
Aug. 11, 10 · Interview
Like (0)
Save
Tweet
Share
26.72K Views

Join the DZone community and get the full member experience.

Join For Free

It's a long time since I started to using the H2 Database Engine as an embedded database in JTheque and other projects. This post is a presentation of this database engine and some information about its utilization.

H2 is a pure Java database. It can work as an embedded database or in server mode. The developers of the database have ensured there is a very small footprint for this database; the jar file is just around 1MB.

Performance

Another feature the developers find important in this database is the performance. Here are two comparison graphs for H2, Derby and HSQLDB, generated directly from the benchmark data from the official H2 site.

Performances of H2 in Embedded Mode

Performances of H2 in Embedded Mode

Performances of H2 in Server Mode

Performances of H2 in Server Mode

As you can see, the performance of H2 is very good. It’s the main reason I’ve used H2 instead of HSQLDB.

Modes

H2 can work in three modes. Embedded, Server or Mixed modes. In each mode, you can use memory or persistent tables.

H2 Embedded Mode

In this mode, the database is only accessible from the current virtual machine. It’s the fastest connection mode.

H2 Server Mode

In this mode, the database runs in a server, so several applications can access the database engines. Of course, this mode is slower than in embedded mode, because all data is transfered using TCP/IP.

H2 Mixed Mode

H2 Mixed Mode

In this mode, the first application creates the database in embedded mode but also starts a server to provide access to the database.

Use

The use of this database is as simple as the use of any other database. You just have to create the database using the JDBC Driver and the database will be automatically created, or the server launched, if needed.

Here is an example to create an database in embedded mode, you just have to do the following :

Class.forName("org.hsqldb.jdbcDriver").newInstance();
Connection connexion = DriverManager.getConnection("jdbc:h2:test", "sa", "");

test is the name of the database. If the database already exists, the data will be loaded.

You can also create a database in memory. But when you close the database, there is no data persisted. It’s useful for benchmarking by example. It’s also the mode with the best performance. Here is an example showing how to create a memory database (named test) :

Connection connexion = DriverManager.getConnection("jdbc:h2:mem:test", "sa",  "");

To start the server, you have two choices. You can launch it from the command line :

java -cp h2*.jar org.h2.tools.Server

Or programmatically :

Server server = Server.createTcpServer(args).start();
...
server.stop();

And to connect to a remote server, just change the JDBC URL :

Connection connexion = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test", "sa",  "");

The database is closed when the last connection to the database is closed.

The SQL used is strandard SQL with some adds to support the features of H2. The syntax is described on the oficial site : SQL Grammar. By example, here are the SQL requests for creating the different types of database of H2 :

CREATE CACHED TABLE cachedTable ...
CREATE MEMORY TABLE memTable ...
CREATE MEMORY TEMPORARY TABLE memTempTable ...
CREATE CACHED TEMPORARY TABLE memTempTable ...
  • A cached table is the default type of table. The data is persistent and not limited by the memory.
  • A memory table is also persistent but the index of data is kept in the memory, so a memory table cannot be too large.
  • A temporary table is deleted when closing the database.

So, I think we covered here the main things we must know to start using H2 Database Engine.

For more information, consult the official site.

From http://www.baptiste-wicht.com/2010/08/presentation-usage-h2-database-engine/

Database engine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Create a REST API in C# Using ChatGPT

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: