The simplest ActiveObjects database provider for the H2 database

For my current project i decided to use ActiveObjects as ORM layer. Additionally i looked for a free, java based database engine which offers support for

  • Embedded Mode
  • Full Text Search
  • Blobs and Clobs

Finally i decided to use the H2 database. Unfortunately i was lazy and didn’t check ActiveObjects list of supported databases.
ActiveObjects doesn’t support the H2 database out of the box. :-(

Using ActiveObjects HSQLDatabaseProvider (H2 is known to be highly compatible with HSQL) doesn’t work because in ActiveObjects a DataBaseProvider has to return the appropriate JDBC driver class.
To use the HSQLDatabaseProvider for H2 anyway you can just extend it and overwrite the getDriverClass() method:

package net.java.ao.db;

import java.sql.Driver;

/**
 * @author netseeker
 *
 */
public class H2DatabaseProvider extends HSQLDatabaseProvider {

    /**
     * @param uri
     * @param username
     * @param password
     */
    public H2DatabaseProvider(String uri, String username, String password) {
	super(uri, username, password);
    }

    /*
     * (non-Javadoc)
     *
     * @see net.java.ao.db.HSQLDatabaseProvider#getDriverClass()
     */
    @SuppressWarnings("unchecked")
    @Override
    public Class< ? extends Driver> getDriverClass()
	    throws ClassNotFoundException {
	return (Class< ? extends Driver>) Class.forName("org.h2.Driver");
    }
}

The new DataBaseProvider can then be used like this:

    EntityManager manager = new EntityManager(new H2DatabaseProvider(
	    						dbProperties.getProperty("db.uri"),
	    						dbProperties.getProperty("db.username"),
	    						dbProperties.getProperty("db.password")));
Bookmark and Share
3 Comments
Oktober 30, 2008 in Development

3 Responses

  1. You know, if H2 support is really that simple, I can move this functionality into the core distribution. That way you could still have auto-magical JDBC URI-based driver detection. :-)

    The one wrinkle is AO is into feature freeze for 1.0, which means that it still wouldn’t be “official” support for H2, but I don’t mind adding it to the list of supported databases and marking it as “experimental”.

  2. Hi Daniel,
    adding this basic H2-support to AO would be great – even if the support would be “experimental”. Thank you in advance. :-)
    I will try to contact Thomas Mueller who is the author of H2. I feel confident that he will help me to create a “real” DataBaseProvider for H2. Maybe that provider could then replace my simple hack in AO 1.x.

  3. I had a look at the HSQLDatabaseProvider. There may be a few things that can be improved, but only minor items (renderOnUpdate maybe). I can help writing and testing a complete H2DatabaseProvider, but your initial implementation looks very good already! It would be great if this implementation could be included in AO 1.0 (even experimental)!

    Regards,
    Thomas

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>