Up

The H2 database was written by the same person who was the original developer for the Hypersonic SQL database (Thomas Mueller). The URL for the H2 website is http://www.h2database.com.

Starting the Server

java -cp h2.jar org.h2.tools.Server

Arguments

By default, -tcp, -web, -browser and -odbc are started
-? show usage
-tcp (start the TCP Server)
-tcpPort <port>
-tcpSSL [true|false]
-tcpAllowOthers [true|false]
-tcpPassword {password} (the password for shutting down a TCP Server)
-tcpShutdown {url} (shutdown the TCP Server, URL example: tcp://localhost:9094)
-tcpShutdownForce [true|false] (don't wait for other connections to close)
-web (start the Web Server)
-webPort <port>  (default is 8082)
-webSSL [true|false}
-webAllowOthers [true|false}
-browser (start a browser)
-odbc (start the ODBC Server)
-odbcPort <port>
-odbcAllowOthers [true|false]
-log [true|false]
-baseDir <directory>
-ifExists [true|false] (only existing databases may be opened)

Adding H2 dialect to Hibernate

  • Copy the file h2/src/tools/org/h2/tools/hibernate/H2Dialect.txt to hibernate-3.1/src/org/hibernate/dialect/H2Dialect.java
  • Rebuild Hibernate
  • In your project's hibernate.properties file, set hibernate.dialect to org.hibernate.dialect.H2Dialect.

Fixing driver class not found error

I was trying to test H2 with a GWT/Hibernate project that I had started that used PostgreSQL as the database. I editted the extension/hibernate.properties file to use an embedded H2 database as follows:

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url=jdbc:h2:file:/tmp/gwt_test_2
hibernate.show_sql=true
hibernate.dbcp.maxActive=100
hibernate.dbcp.whenExhaustedAction=1
hibernate.dbcp.maxWait=120000
hibernate.dbcp.maxIdle=10
hibernate.dbcp.ps.maxActive=100
hibernate.dbcp.ps.whenExhaustedAction=1
hibernate.dbcp.ps.maxWait=120000
hibernate.dbcp.ps.maxIdle=100

I added the h2.jar file into the list of libraries for my NB project. When I tried to build I was getting errors from Hibernate saying that the org.h2.Driver class was not found. The exact error message was: org.hibernate.HibernateException: JDBC Driver class not found: org.h2.Driver

After some experimentation, I found that I had to do the following:

  • Open the project/properties dialog and click on the Libraries category.
  • Click the Add Library button, then click Manage Libraries
  • Find the hib-jdbc-driver.jar entry on the left side and click on it.
  • Click Add JAR/Folder and add the h2.jar file. For me this was located in ~/projects/h2/bin.
  • Then I had to open the extension/build.xml file and comment out the contents of the extension-library-variants target as follows:
  <target name="extension-library-variants">
    <!--
    <property name="libs.hib-jdbc-driver.jar.classpath"
          value="/home/gordy/.netbeans/5.0/config/libraries/postgresql/postgresql-7.4.1-jdbc3.jar" />
    -->
  </target>
You cannot just comment out the whole target, because it is referred to by another target. But removing or commenting-out the contents of that target keeps it from overriding the hib-jdbc-driver.jar entry that you set-up earlier. In my situation, the hib-jdb-driver.jar entry now contains entries for both the PostgreSQL driver and the H2 driver, so I can switch back and forth between the two.

Some other notes...

With my test NB project where I'm switching betwee PostgreSQL and H2, I first tried renaming the original hibernate.properties file, which was configured by the Hibernate module to use PostgreSQL when I first created the project, to postgresql.properties. Then I made a second properties file called h2.properties. I wanted to edit my HibernateUtil.java file to use one or the other properties file, based upon some startup parameter. I think this should work. But it was breaking something in the NB build process. One of the ant targets was looking specifically for the hibernate.properties file. In particular, this target:

    <!-- merge hibernate properties -->
    <loadproperties srcFile="extension/hibernate.properties">
      <filterchain>
        <striplinecomments>
          <comment value="#" />
        </striplinecomments>
        <prefixlines prefix="hibernate_prefX." />
      </filterchain>
    </loadproperties>

I'll need to do a bit more experimentation to find the most efficient way to manage multiple Hibernate dialect property files. I'm just posting these notes to track things that I have tested.


Page last modified on August 16, 2006, at 12:09 PM