Home » Java » Enterprise Java » Embedding HSQLDB server instance in Spring

About Allen Chee

Allen Chee
Allen is a software developer working in the banking domain. Apart from hacking code and tinkering with technology, he reads a lot about history, so that mistakes of the past need not be repeated if they are remembered.

Embedding HSQLDB server instance in Spring

I was using XAMPP happily for development until I had to host it somewhere accessible via the Internet for the client to test and use. I have a VPS that only has 384 RAM, and needing to find a way fast, I decided to install XAMPP into the VPS. Because of the low RAM, when MySQL was running, Tomcat failed to start, even though the initial Java heap size was set to 64m. I managed to host the site temporarily in Jelastic, before moving to OpenShift.

I toyed at the idea of combining the database and application server instances in 1 JVM, to reduce RAM usage (compared to running MySQL + Tomcat). After searching the Internet, I came across several articles on running HSQL server instances together with Tomcat. No doubt I have to update my site to be compatible with HSQL first, but as a POC (proof-of-concept) attempt, I decided to explore the feasibility of running the HSQL server instance in a Spring container.

There are several reasons to run the HSQL server just like a bean in Spring:

1. All-in-one configuration. Everything that is needed to be configured is done in Spring. There are examples in the Net to run the HSQL instance alongside Tomcat, but this requires adding stuff to Tomcat (see links below).

2. Application server independence. ‘Theoretically’ (in quotes as I successfully tested this in Tomcat only), since everything is done in Spring, there’s no or little that needs to be configured in the appserver.

The HSQL server ‘bean’ is also meant to launch an instance in network mode (not in-process e.g. mem or file). Some reasons for this:

1. ‘mem’ in-process access is the fastest, but is not persistent. There are other means to initiate a ‘mem’ data source using Spring’s spring-jdbc tags, which is a better approach.

2. ‘file’ in-process access is persistent, but like ‘mem’, it can only be accessed within the Java process.

3. Network mode (hsql) is both persistent and accessible using external JDBC client tools. This is useful for troubleshooting and verification.

After reading HSQLDB’s documentation, here’s the code that does the HSQL server instance bean lifecycle management:

package org.gizmo.hsql.spring;

import java.io.IOException;
import java.util.Properties;

import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;
import org.hsqldb.server.ServerAcl.AclFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.SmartLifecycle;

public class HyperSqlDbServer implements SmartLifecycle
 private final Logger logger = LoggerFactory.getLogger(HyperSqlDbServer.class);
 private HsqlProperties properties;
 private Server server;
 private boolean running = false;

 public HyperSqlDbServer(Properties props)
  properties = new HsqlProperties(props);

 public boolean isRunning()
  if(server != null)
  return running;

 public void start()
  if(server == null)
   logger.info('Starting HSQL server...');
   server = new Server();
    running = true;
   catch(AclFormatException afe)
    logger.error('Error starting HSQL server.', afe);
   catch (IOException e)
    logger.error('Error starting HSQL server.', e);

 public void stop()
  logger.info('Stopping HSQL server...');
  if(server != null)
   running = false;

 public int getPhase()
  return 0;

 public boolean isAutoStartup()
  return true;

 public void stop(Runnable runnable)

The abridged Spring configuration:


Sample output when starting Spring in Tomcat:

[[email protected]]: [Thread[pool-2-thread-1,5,main]]: checkRunning(false) entered
[[email protected]]: [Thread[pool-2-thread-1,5,main]]: checkRunning(false) exited
[[email protected]]: Initiating startup sequence...
[[email protected]]: Server socket opened successfully in 7 ms.
Sep 27, 2012 9:26:23 AM org.hsqldb.persist.Logger logInfoEvent
INFO: checkpointClose start
Sep 27, 2012 9:26:23 AM org.hsqldb.persist.Logger logInfoEvent
INFO: checkpointClose end
[[email protected]]: Database [index=0, id=0, db=file:d:/hsqldb/demo
base, alias=demobase] opened sucessfully in 442 ms.
[[email protected]]: Startup sequence completed in 451 ms.
[[email protected]]: 2012-09-27 09:26:23.395 HSQLDB server 2.2.8 is online on port
[[email protected]]: To close normally, connect and execute SHUTDOWN SQL
[[email protected]]: From command line, use [Ctrl]+[C] to abort abruptly

Related Links:

Reference: Embedding HSQLDB server instance in Spring from our JCG partner Allen Julia at the YK’s Workshop blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

2 Comments on "Embedding HSQLDB server instance in Spring"

Notify of
Sort by:   newest | oldest | most voted
Marcelo Sales

did you post de source code? the spring configuration.

Shalima sidhik


In which file should i provide the jdbc-embedded code ? Do I give it in applicationContext of Spring where I mention all the beans or a different plain xml file ?