How can I start and keep running hsqldb in server mode from within my web application?

2010-09-08 java hsqldb

I don't want to use it in embedded mode as I may allow other external applications to access it as well. And I want to execute the startup of the server at the same time as Tomcat loads my application (or just when tomcat runs for that matter). This is so that I don't have to ask clients to manually run hsqldb with a command or script before they can put my war into tomcat and run it (to keep things simple).

I can perhaps call Server from main by sending command from Java, but this will give me a unending thread, I am not sure how to deal with that. Is there an easier tested way to do this?


You can use HSQLDB on any web/app server in embedded mode and allow external apps to access it by running the org.hsqldb.server.Servlet on your web/app server. The external apps will then connect to the servlet using the jdbc:hsqldb:http: type url's.

If you want to run org.hsqldb.server.Server (the version 2.0 class), or org.hsqldb.Server, as a separate process as Tomcat, you can use the class org.hsqldb.util.MainInvoker to call the main method for multiple classes (e.g. Tomcat and HSQLDB Server) with Server called first.

According to the HSQLDB Documentation is possible start the database from Java Code: So you can use a servlet for load the database when the web application is starting. The steps should be the following:

  1. Create a Servlet "InitDatabase" and put the code for start the database on the method init()

    public void init() throws ServletException {
        try {
            System.out.println("Starting Database");
            HsqlProperties p = new HsqlProperties();
            p.setProperty("server.database.0", "file:/opt/db/crm");
            p.setProperty("server.dbname.0", "mydb");
            p.setProperty("server.port", "9001");
            Server server = new Server();
            server.setLogWriter(null); // can use custom writer
            server.setErrWriter(null); // can use custom writer
        } catch (AclFormatException afex) {
            throw new ServletException(afex);
        } catch (IOException ioex) {
            throw new ServletException(ioex);
  2. In your web.xml add the property load on start up and set it to 1. This for call to method init() when the Web Application is starting.


After do this the Web Application will start HSQLDB in a new Thread. For shutdown the database when the application stops you can override the method destroy() of InitServlet. In the method destroy you must execute the command "SHUTDOWN" as normal sql query (through JDBC).

This question is quite dated, but I recently ran into a similar situation. I tried to access the embedded HSQLDB database running in a TomEE server. @fredt's answer gave me the decisive hint, but I was still struggling to find out all the details. Here is my solution:

Add a servlet mapping for org.hsqldb.server.Servlet to either a web.xml or web-fragment.xml descriptor file:


Using the parameter hsqldb.server.database you can specify the location of your database. Because Tomcat/Tomcat distinguishes between CATALINA_HOME and CATALINA_BASE, simply specifying a relative path like "hsqldb/db" is not enough. But you can use system properties provided by Tomcat/TomEE, like ${catalina.base} to make your path absolute.

After starting your server the HSQLDB database can be accessed via the URL