HowTo Use Apache Derby with Jena2

What is HSQLDB?

Apache Derby is a relational database implemented entirely in Java. Used with Jena, it provides a convenient way to give persistence to RDF data by using it as an embedded database engine, within the application JVM and storing data in the local file system. No database server need be run. Derby can also be run as a shared server.

Download and Installation of Apache Derby

Download Apache Derby, install and put derby.jar file on the application classpath. This includes the JDBC driver and the database engine.

Connecting Your Jena Program to Apache Derby

JDBC URLs

JDBC URLs for Derby look like jdbc:derby:datbasenameThe use of parameter "create=true" causes a database to be created if it does not already exist -- jdbc:derby:datbasename;create=true. For embedded use, the user name and password can both be "".

The database JDBC driver controls whether the embedded or server mode is in operation (unlike HSQLDB where the URl changes but the driver is the same). The driver is class org.apache.derby.jdbc.EmbeddedDriver for embedded use (no separate server) or org.apache.derby.jdbc.ClientDriver.

The Jena driver name is "Derby".

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.apache.derby.jdbc.EmbeddedDriver";   // path of driver class
Class.forName (className);                                   // Load the Driver
String DB_URL =    "jdbc:derby:database";                    // URL of database 
String DB_USER =   "";                                       // database user id
String DB_PASSWD = "";                                       // database password
String DB =        "Derby";                                  // 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();

Apache Derby Notes

When used embedded, only one application can attach to the database at a time.

The default transaction level is TRANSACTION_READ_COMMITTED, means that overlapping transactions do not read uncomitted updates but do see comitted updates as soon as one transaction commits. Derby supports other modes - see the Derby documentation and the javadoc for java.sql.Connection.

The application can use locking within the application, such as MRSW locking, or provide it's own locking, to ensure that updates and reads do not overlap.