Home / Java Patterns and Pitfalls     frequal.com

H2: The Best Database You've Never Heard Of

Summary

H2 is a mature, stable, fast, production-ready database. It just happens to be tiny, easy to use, and 100% Pure Java. You can use it for everything from in-memory transient unit tests, to live production databases. Read on to find out more about H2 and how to get started with it.

What It Is

H2 is the second Pure Java database from its author. It comes in a single, small JAR file that is ready to include directly or as a dependency using your favorite build tool. It is a full-featured SQL database:
  • Full SQL syntax
  • High performance
  • JDBC, Hibernate, and JPA support
  • Can run as a server
  • Can run in memory (for tests)
  • Has a full web interface for running queries and editing tables

How To Use It

The easiest way is to include it as a dependency in your maven POM file.

You can also include it in your project directly. H2 makes this easy since it is packaged in one JAR.

The H2 cheat sheet has full details on including it in your project, including maven dependency XML and JDBC connect strings.

Where To Use It

Almost everywhere!

First, use it in your integration tests. Anywhere your code connects to your production DB, use embedded H2 instead. Use hibernate hbm2ddl to create your tables and seed them with sample data.

Second, use it in your test servers. With its small footprint, easy deploy (single zip), and zero cost, you can deploy on as many servers as you like, on site or in the cloud.

Finally, deploy it in production. In my experience it is rock solid, consumes few resources, and handles anything I can throw at it. The only thing I haven't tried is clustering (high availability).


Last modified on 11 Jan 2018 by AO

Copyright © 2018 Andrew Oliver