20060830 Wednesday August 30, 2006

The database is already in use by another process

I was writing an app that uses HSQLDB in Eclipse. The app would work fine the first time I ran it, but subsequent invocations within Eclipse resulted in the following error:

java.sql.SQLException: The database is already in use by another process

I got tired of restarting Eclipse each time, and decided to RTFM. Found this on the hsqldb website:

All databases running in different modes can be closed with the SHUTDOWN command, issued as an SQL query. From version 1.7.2, in-process databases are no longer closed when the last connection to the database is explicitly closed via JDBC, a SHUTDOWN is required. In 1.8.0, a connection property, shutdown=true, can be specified on the first connection to the database (the connection that opens the database) to force a shutdown when the last connection closes.

So turns out the fix was easy. I can either set the shutdown property, or issue the shutdown command via SQL. I chose the latter. Here is my code for closing the database:

Statement stmt = conn.createStatement();
stmt.execute("SHUTDOWN");
stmt.close();
conn.close();

Works fine now.

FYI, this was also causing a problem with Excelsior Jet, which seems to keep the JVM loaded between invocations of compilied apps, so while Eclipse was ok between restarts, I had to physically reboot the computer between invocations of a compiled Jet program, which was really inconvenient.

Posted by rickg ( Aug 30 2006, 10:41:18 PM PDT ) Permalink Comments [0]

Locating a Jar file

I wanted to find the exact location of a jar file (in this case hsqldb.jar) on the file system, no matter where it was located, as long as it was on the classpath. I knew that hsqldb.jar contained "org.hsqldb.jdbcDriver", so I figured I would search for that class using getResource, then trim off the other URL garbage to get a real file.

Here is what I ended up with (sorry if it looks a little Perlish):

URL jarUrl = this.getClass().getResource("/org/hsqldb/jdbcDriver.class");
String jarUrlString = jarUrl.toExternalForm().split("!")[0];
jarUrlString = jarUrlString.substring(jarUrlString.indexOf(":") + 1);
System.out.println(jarUrlString);
jarUrl = new URL(jarUrlString);
File jarFile = new File(jarUrl.toURI());
if (jarFile.exists()){
   System.out.println("Found it!");
  }
System.out.println(jarFile.getAbsolutePath());

Posted by rickg ( Aug 30 2006, 09:26:18 AM PDT ) Permalink Comments [0]