HowTo Use HSQLDB with Jena2

What is HSQLDB?

HSQLDB (also known as Hypersonic or HSQL), is a lightweight, 100% Java SQL Database Engine. Used with Jena, it provides a simple way to give persistence to RDF data by using HSQLDB within the application JVM, using the file system for persistence.  HSQLDB can also run as a database server, handling connections from different JVMs, and also as a purely in-memory engine.

Using it within one application JVM, and using file-based persistence, is the most useful mode with Jena. The application should manage internal concurrency with MRSW (multi-reader, single-writer) locking, or Java synchronized (which is single reader, single writer locking), because HSQLDB does not support full transaction isolation.

Download and Installation of HSQLDB

Download HSQLDB from SourceForge, unpack the zip file and put the hsqldb.jar file on the application classpath. This includes the JDBC driver and the database engine.

Connecting Your Jena Program to HSQLDB

JDBC URLs

JDBC URLs for HSQLDB look like the following:

By default, for file-persistence, the user name is "sa" and the password is "". See the HSQLDB documentation for further details.

The database JDBC driver is class org.hsqldb.jdbcDriver.  Compare this to Derby where the driver changes but the URL is the same for server or embedded use.

The Jena driver name is HSQL.

Persistent models are created in the same way for any database system:

  1. Load the JDBC driver. This enables the Jena program to communicate with the database instance.
  2. Create a database connection. This creates a Java object for a database connection.
  3. Create a ModelMaker for the database
  4. Create a Model for existing or new data.

These steps are illustrated in the following Java code.

String className = "org.hsqldb.jdbcDriver";       // path of driver class
Class.forName (className);                        // Load the Driver
String DB_URL =    "jdbc:hsqldb:file:filename";   // URL of database 
String DB_USER =   "sa";                          // database user id
String DB_PASSWD = "";                            // database password
String DB =        "HSQL";                        // database type

// Create database connection
IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );
ModelMaker maker = ModelFactory.createModelRDBMaker(conn) ;

// create or open the default model
Model model = maker.createDefaultModel();

// Close the database connection
conn.close();

HSQLDB Notes

HSQLDB does not support full ACID transactions; the application can read uncommitted updates from other transactions. The application should use locking within the application, such as MRSW locking or provide it's own locking, to ensure that updates and reads do not overlap.

Jena does not compact the database because it does not know when the database is no longer required by the application - the application must arrange to do this itself. This can be by using the database manager (org.hsqldb.util.DatabaseManager) to compact the database

Or the application can do it by either:

  1. Get the Jena driver , cast to Driver_HSQL and call .shutdown().
  2. Get the SQL connection and execute "SHUTDOWN COMPACT" as an SQL command.