How to close all connections in HSQLDB (also prevents a locking defect)

By Angsuman Chakraborty, Gaea News Network
Saturday, March 5, 2005

I have noticed that despite closing all connections and exiting a standalone HSQLDB database, at least one connection still remains open.

The defect is manifested in HSQLDB 1.7.3 and HSQLDB 1.8.0 RC 8.

If you compile and run the sample code below, it will run fine for the first time. Second time (if it is run immediately after the first time say within 2-3 seconds after completion of first execution) it fails everytime citing a database locked exception!

Filed under: How To, Java Software, RDBMS
Discussion
February 9, 2009: 8:30 pm

Nice comments…

Thank your working…

April 1, 2006: 8:32 am

The file may get corrupted. At worst you have to manually truncate it.


kemalcakmak
April 1, 2006: 12:49 am

But what should I do when I debug with my IDE and terminate abruptly my application? This will lead to an not terminated database connection.

Is there any way to get over this effect?


jakkireddy
January 28, 2006: 11:57 pm

Nice site and ur information is nice.
i appriciate this.

Jakkireddy

April 6, 2005: 2:09 pm

@Stefan

stmt.executeUpdate(”SHUTDOWN”);

where stmt is the java.sql.Statement you have created earlier from HSQLDB java.sql.Connection.


Stefan C
April 6, 2005: 1:39 am

Hello,

i have the same problem.

You’ve wrote:
***********************
you must explicitly issue a command as an admin user. (”SHUTDOWN …”).
***********************

Can you please give me a small code example of that shutdown procedure?

Thank you
Stefan

April 4, 2005: 3:30 pm

@Campbell

Thanks for your detailed insight into this issue.

As you can see from my code above that I am normally closing my connection to the database. I had opened the database as a single user from my application (in other words not as a server). So the rules of server requiring a shutdown shouldn’t really be applicable here.

I am with you wrt. your locking logic in a server scenario. However as a single user closing the connection should be sufficient cue to release any lock to the database.

I think most use cases to HSQLDB is in single user (non-server) mode. Often we use it to load test cases for junit tests etc. In these cases ability to rapidly restart is a great benefit.

What do you think?


Campbell
March 11, 2005: 10:40 am

No. This is not a defect.

I did an extensive analysis and wrote the current hsqldb lock file and priority timer queue implementation: I disagree with your analysis.

HSQLDB is now like Oracle or MySQL, et al: to correctly shut down the database, you must explicitly issue a command as an admin user. (”SHUTDOWN …”).

The main difference is that, unlike Oracle/MySQL, etc., which are fairly rigidly “single installation per machine/release version/data file set” products, it has been observed much more likely that maybe serveral HSQLDB installations/running JVM instances on a machine and each may attempt to access a specific database file set.

That is, we have the problem msaccess had (has?), when the database files were (are?) on a network drive and accessed by several different client machines simultaneously.

But our problem is even more likely to occur, because Java does not provide a shared-mem/ipc ODBC connection manager that allows multiple JVM processes to acccess the same local database file (unless you use the JDBC/ODBC driver, which is not available to type 4 implementations, without writing JNI layer)

Anyway, if you abort the JVM (ctrl-c), kill, etc., or for any other reason the JVM dies abruptly, does a halt() (or even exit() on older JDKS where deleteOnExit is not available), then sure… the .lck file does not get deleted.

In the next release or so, we will auto-detect availability of shutdownHook and add handler there, but this still will not help of true JVM abends.

But back to the point: if the .lck file does not get deleted, then there’s a 10 second buffer zone because the lock file is touched at 10 second intervals.

I chose this number so that in cases where there is really heavy CPU and disk usage going on, there’s a fairly safe buffer zone: Java and most operating systems do not do really accurate timing, especially under high load. Just think about the case of a really high res, high frame-rate video with high bit-rate audio on a file) does not constitute an OS-enforced cross-process lock on the file.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :