Presentation and Use of H2 Database Engine
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.
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.
As you can see, the performance of H2 is very good. It’s the main reason I’ve used H2 instead of HSQLDB.
H2 can work in three modes. Embedded, Server or Mixed modes. In each mode, you can use memory or persistent tables.
In this mode, the database is only accessible from the current virtual machine. It’s the fastest connection 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.
In this mode, the first application creates the database in embedded mode but also starts a server to provide access to the database.
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 :
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();
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.