H2 database on GlassFish

Previously I’d written about running the H2 database within Tomcat.

Here’s a recipe for running under V3 of the Glassfish application server. I’ll create a datasource for the same “employee” database I used previously. I’m creating an in-memory embedded database, but you can connect to any other H2 configuration. Just change the URL format.

Installing the driver

H2 comes with a single JAR file containing both the database and the driver. Before starting up GlassFish, copy this h2.*.jar file into your domains/domain1/lib/ directory.

Now, start up the server: asadmin start-domain

Open up the admin console at http://localhost:4848

It’s possible to configure everything by editing your domain.xml file, or through the admin console GUI. For now, I’ll explain how to get things going through the GUI.

Create a connection pool

On the left side of the page there is a tree of tasks. Find Resources / JDBC / Connection Pools. Hit the “New…” button.

Set the pool name to be “empPool”, the resource type to be “javax.sql.DataSource”, and then fill in vendor name to be “H2”. Submit with the “Next” button.

On the next page, fill in the datasource class name as “org.h2.jdbcx.JdbcDataSource”. At the bottom of the page, edit the additional properties to have only the following, then hit the “Finished” button.

  • user = sa
  • password = ()
  • url = jdbc:h2:mem:employee

Note that the password is set to just left and right parentheses. The admin GUI will interpret this as a blank string. If you tried to enter a blank password, the GUI would disregard your password field.

Now that the pool has been created, you’ll see “empPool” listed under the task tree. Click into this pool to review your setup. There is a “Ping” button on this page that will verify that you can connect to the database.

Set up a JNDI datasource

In the task tree, click into Resources / JDBC / JDBC Resources. Click “New…” to create a JNDI resource to the new connection pool.

The JNDI name should be “jdbc/empDS” and the pool name will be “empPool”. Submit with the “OK” button.

The GlassFish admin console can take care of everything. Just for fun, you can take a look at the “domain.xml” file to see how things are set up. Some environments work better by just deploying XML files.

Use your new datasource

At this point, you can retrieve the datasource "jdbc/empDS" from the InitialContext, and then get connections from the pool:

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/empDS");

 ...

 Connection conn = null;
 try {
     conn = ds.getConnection();
     ...
 } finally {
     if (conn!=null) { conn.close(); }
 }
Advertisements

No comments yet

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: