1

This is related to these 2 posts:

Basically H2 keeps a lock on the database, even when all connections are closed, and so when stopping Tomcat it hangs waiting on a thread, the process is still running. The only way I managed to get H2 to not lock the database is by issueing the statement SHUTDOWN IMMEDIATELY command (the vanilla or compact did not release the lock).

This is performed in my ServletContextListener class in the contextDestroyed like this (I have omitted comments and log lines):

    ServletContext ctx = servletContextEvent.getServletContext();

    DataSource closeDS = databaseConnection.getDatasource();
    Connection closeConn = null;
    PreparedStatement closePS = null;
    try {
        closeConn = closeDS.getConnection();
        closePS = closeConn.prepareStatement("SHUTDOWN IMMEDIATELY");
        closePS.execute();
    } catch (Exception ex) {
    } finally {
        if (closePS != null) { 
          try { closePS.close(); } catch (SQLException ex) {} 
        }
        if (closeConn != null) { 
          try { closeConn.close(); } catch (SQLException ex) {}
        }
    }

    try {
        databaseConnection.close();
        databaseConnection = null;
        ctx.setAttribute("databaseConnection", null);
    } catch(Exception e) {
    }
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {
            DriverManager.deregisterDriver(driver);
        } catch (Exception e) {
        }
    }

Now the lock is released Tomcat stops (although I still get the severe memory leak messages in the logs) but now I receive also a number of error stacks in the logs thus:

INFO: Illegal access: this web application instance has been stopped already.  
  Could not load java.lang.ThreadGroup.  
  The eventual following stack trace is caused by an error thrown 
  for debugging purposes as well as to attempt to terminate 
  the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1531)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    at org.h2.engine.DatabaseCloser.reset(DatabaseCloser.java:43)
    at org.h2.engine.Database.close(Database.java:1155)
    at org.h2.engine.DatabaseCloser.run(DatabaseCloser.java:80)
10-sep-2013 13:31:41 org.apache.catalina.loader.WebappClassLoader loadClass

The question is: how can I shut down the database without causing illegal state exceptions. Is there something wrong in my code to call the shutdown command? Why is this such an issue with H2? I do not have this issue with JBoss or Websphere where the application also runs using datasources provided by the container.

Community
  • 1
  • 1
Gurnard
  • 1,773
  • 22
  • 43
  • You have a lot of "catch exception - ignore" in your code. This is very dangerous, as it will also swallow NPE or other problems. For example, I wouldn't be sure that `SHUTDOWN` is even executed, as maybe it is already closed or the field is `null`. I suggest to _not_ silently swallow exceptions. – Thomas Mueller Sep 11 '13 at 11:42
  • Could you try again with a recent version of H2? It looks like the code that throws an exception was removed in recent versions. That's the first thing I would do. What is your database URL exactly? – Thomas Mueller Sep 11 '13 at 11:54
  • Hello thanks for the comment. I have removed all of the logging from the code for compactness, none of the exceptions are ignored. I am currently using 1.2.1 will there be any issues upgrading to the latest version? Looking at the site there is a Datbase Upgrade Helper file, this makes me a little worried. If I upgrade will I be compatible with older versions that are already installed in the field? – Gurnard Sep 11 '13 at 12:52
  • sorry I forgot to add my DB url. It is auto server like this: jdbc:h2:c:/my/path/mydb;AUTO_SERVER=TRUE – Gurnard Sep 11 '13 at 13:19
  • There is actually no H2 version with the number 1.2.1... Could you try without the auto-server feature? I'm not aware of any issues with it, but who knows, maybe this is the problem. – Thomas Mueller Sep 11 '13 at 17:15

0 Answers0