NullPointerException when connecting to a database which is shutting down

Lucas Ventura Carro <[email protected]> Fri, 1 Sep 2017 11:13:03 +0200
Newsgroups gmane.comp.java.hsqldb.user
Message-ID <CAJvqtSr+dyXCVaoBrsitw-=8nM9MWHZ15tw4iPhy1SpYCkJUjg@mail.gmail.com>
Hello,

I use HSQLDB 2.2.9 in a testing environment, where I execute too many times
restarts of it.
Lastly I have added threads which try to write to database, even when is
shutdown, so I've faced NullPointerException when the database is shutting
down, which yeld the database unusable, so is needed to kill it and start
again.
The log I got is:

    [Server@9a5a48]: [Thread[main,5,main]]: checkRunning(false) entered
    [Server@9a5a48]: [Thread[main,5,main]]: checkRunning(false) exited
    [Server@9a5a48]: Startup sequence initiated from main() method
    [Server@9a5a48]: Loaded properties from
[/usr/local/hsqldb/trustedx/server.properties]
    [Server@9a5a48]: Initiating startup sequence...
    [Server@9a5a48]: Server socket opened successfully in 24 ms.
    [Server@9a5a48]: Database [index=0, id=0, db=file:localdb, alias=]
opened sucessfully in 6481 ms.
    [Server@9a5a48]: Startup sequence completed in 6507 ms.
    [Server@9a5a48]: 2017-08-29 11:56:28.573 HSQLDB server 2.2.9 is online
on port 1702
    [Server@9a5a48]: To close normally, connect and execute SHUTDOWN SQL
    [Server@9a5a48]: From command line, use [Ctrl]+[C] to abort abruptly
    Exception in thread "HSQLDB Connection @3600ab"
java.lang.NullPointerException
    at org.hsqldb.Session.close(Unknown Source)
    at org.hsqldb.server.ServerConnection.close(Unknown Source)
    at org.hsqldb.server.ServerConnection.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
    SEVERE SQL Error at '--sql' line 1:
    "SHUTDOWN"
    java.lang.NullPointerException java.lang.NullPointerException
    org.hsqldb.cmdline.SqlTool$SqlToolException

And each new try to connect to database (even to execute the shutdown)
writes:

    [Server@9a5a48]: [Thread[HSQLDB Connection @1025dc7,5,HSQLDB
Connections @9a5a48]]: database alias= does not exist

In my google searchs I got the already resolved bug #855 (
https://sourceforge.net/p/hsqldb/bugs/855/) and the proposed patch #221 (
https://sourceforge.net/p/hsqldb/patches/221/) which didn't applied to me
(I start the database without this property).

After update to 2.3.4, I got the same error.

So I dig into the code - as this was blocking my automatic tests. When I
recompiled HSQLDB issued classes with full debug information, the stack was:

    Exception in thread "HSQLDB Connection @13dfba4"
java.lang.NullPointerException
    at org.hsqldb.Session.close(Session.java:225)
    at org.hsqldb.server.ServerConnection.close(ServerConnection.java:228)
    at org.hsqldb.server.ServerConnection.run(ServerConnection.java:1557)
    at java.lang.Thread.run(Thread.java:745)

With the affected line being:

    database.sessionManager.removeSession(this);

The guilty null was the 'sessionManager' instance, which is nulled when
database is shutting down.
After adding some syso's to code, I've realized that the problem was that
'SessionManager' was creating new sessions **while '.closeAllSessions()'
was being executed** - classic race condition.
So adding 'synchronized' to '.closeAllSessions()' (
https://sourceforge.net/p/hsqldb/svn/HEAD/tree/base/tags/2.2.9/src/org/hsqldb/SessionManager.java#l196)
method didn't caused the issue again.

Seems an issue with shutdown, right?

Thanks,
--
Lucas

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Hsqldb-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hsqldb-user