In order to configure HSQLDB in web application before accesing it, you can extends HttpServlet
and initialize it within web.xml file so HSQLDB will be available inside the application

hypersql


package hsqldb;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;

public class InitHSQLDB extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private Server server = null;
	@Override
	public void init() throws ServletException {
		super.init();
		server = new Server();
		try {
			System.out.println("Start Database");
	        HsqlProperties properties = new HsqlProperties();
	        properties.setProperty("server.database.0", "jdbc:hsqldb:mem://localhost");	        
	        properties.setProperty("server.port", "9001");
	        Server server = new Server();
	        server.setProperties(properties);
	        server.start();
	        System.out.println("Database is started");
		} catch (Exception e) {
			throw new ServletException(e);
		}		
	}
	
	@Override
	public void destroy() {		
		   super.destroy();
		   server.stop();
		   server.shutdown();
	}
}

web.xml configuration

 <servlet>
    <servlet-name>InitDatabase</servlet-name>
    <servlet-class>hsqldb.InitHSQLDB</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

maven dependency

  <dependency>
     <groupId>org.hsqldb</groupId>
     <artifactId>hsqldb</artifactId>
     <version>2.2.9</version>
  </dependency>

log

INFO: Initializing Mojarra 2.1.13 ( 20120907-1514) for context ‘/jsf’
Jun 16, 2015 4:12:46 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present. ManagedBeans methods marked with these annotations will have said annotations processed.
Jun 16, 2015 4:12:47 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 5.2
Start Database
[Server@185a8812]: [Thread[localhost-startStop-1,5,main]]: checkRunning(false) entered
[Server@185a8812]: [Thread[localhost-startStop-1,5,main]]: checkRunning(false) exited
[Server@185a8812]: Initiating startup sequence…
[Server@185a8812]: Server socket opened successfully in 3 ms.
[Server@185a8812]: Initiating shutdown sequence…
[Server@185a8812]: [Thread[HSQLDB Server @185a8812,5,main]]: Shutting down because there are no open databases
[Server@185a8812]: Shutdown sequence completed in 4 ms.
[Server@185a8812]: 2015-06-16 16:12:47.939 SHUTDOWN : System.exit() was not called
Database is started!

Jun 16, 2015 4:12:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [“http-nio-8080”]
Jun 16, 2015 4:12:48 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [“ajp-nio-8009”]
Jun 16, 2015 4:12:48 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5455 ms

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s