It makes a long time now that I started to use the H2 Database Engine as embedded database in JTheque and other projects. This post is a presentation of this database engine and some informations about its utilization.
H2 is a pure Java database. It can work as embedded database or in server mode. The developers of the database have made all to have a very small footprint for this database, it takes around 1MB jar file size.
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 performances of H2 are 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 datas are transfered using TCP/IP.
In this mode, the first application create the application in embedded mode but also start 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 to the database using the JDBC Driver and the database will be automatically created if needed or the server launched if needed.
By example, here it’s an example to create an database in embedded mode, you just have to do that :
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. There is no persisting data. 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 performances. Here is an example to create a memory database (named test) :
Connection connexion = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
To start the server, you have two choice. 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 remove 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 are persistent and not limited by the memory.
- A memory table is also persistent but the index of data is kept in the memory, so 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 informations, consult the official site.