在Web项目中初次尝试HSQLDB

最近在一个showcase的项目里面使用内存数据库HSQLDB,下面将自己的使用方式做以简单的介绍。

本人是在Web项目中使用HSQLDB的,所以希望数据库能随之Web程序一起启动。由于此Web使用了Spring Framework,因此本人编写了一个小组建用于启动数据库,其代码如下。

 

package com.wanmei.web.dev.showcase.hsqldb.support;

import java.io.File;
import java.util.Set;

import org.hsqldb.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author philip
 *
 */
public class LocalHsqldbServerBean {

 private static final Logger logger = LoggerFactory.getLogger(LocalHsqldbServerBean.class);
 
 public static final int DEFAULT_PORT = 9001;
 public static final String DEFAULT_ADDRESS = "127.0.0.1";
 
 private Set<String> databaseNames;
 
 // database server properties
 private String address;
 private int port;
 private boolean silent;
 private boolean trace;
 
 private Server hsqldbServer;
 
 public LocalHsqldbServerBean() {
  super();
  address = DEFAULT_ADDRESS;
  port = DEFAULT_PORT;
  silent = true;
  trace = false;
 }
 
 public void initialize() {
  
  logger.debug("starts to initializing hsqldb server");
  
  final String databaseScriptRootPath = getFileDirectory();
  logger.debug("hsqldb script root path " + databaseScriptRootPath);
  
  hsqldbServer = new Server();
  
  hsqldbServer.setPort(port);
  hsqldbServer.setAddress(address);
  hsqldbServer.setSilent(silent);
  hsqldbServer.setTrace(trace);
  
  setsetDatabaseNames(databaseNames, hsqldbServer, databaseScriptRootPath);
  
  try {
   hsqldbServer.start();
   logger.debug("hsqldb server started, hsqldb server state is " + hsqldbServer.getStateDescriptor());
  } catch (Exception e) {
   logger.error("can not start hsqldb server", e);
   throw new RuntimeException("can not start hsqldb server", e);
  }
  
 }
 
 private void setsetDatabaseNames(Set<String> names, Server server, String scriptPath) {
  
  if (names == null || names.isEmpty()) {
   throw new IllegalArgumentException("can not get database name");
  }
  
  int count = 0;
  
  for (String name : names) {
   logger.debug("setting [" + count++ + "] which database name is '" + name+ "'");
   hsqldbServer.setDatabaseName(count, name);
   hsqldbServer.setDatabasePath(count, scriptPath + name);
  }
 }
 
 private String getFileDirectory() {
  File classDirectory = new File(this.getClass().getResource("/").getPath());
  return classDirectory.getParent() + "/hsqldb/";
 }
 
 public void destory() {
  try {
   hsqldbServer.stop();
   logger.debug("hsqldb server stoped");
  } catch (Exception e) {
   logger.error("can not stop hsqldb server", e);
  }
  
 }

 public Set<String> getDatabaseNames() {
  return databaseNames;
 }

 public void setDatabaseNames(Set<String> databaseNames) {
  this.databaseNames = databaseNames;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }

 public boolean isSilent() {
  return silent;
 }

 public void setSilent(boolean silent) {
  this.silent = silent;
 }

 public boolean isTrace() {
  return trace;
 }

 public void setTrace(boolean trace) {
  this.trace = trace;
 }
}

 


在Spring的配置信息中,首先需要初始化HSQLDB,然后才初始化DataSource,Spring配置片段如下。

 

  <bean id="localHsqldbServerBean" class="com.wanmei.web.dev.showcase.hsqldb.support.LocalHsqldbServerBean" lazy-init="false" init-method="initialize" destroy-method="destory">
    <property name="databaseNames">
      <set>
        <value>devshowcase</value>
        <value>student</value>
        <value>bookstore</value>
      </set>
    </property>
    <property name="silent" value="true"/>
    <property name="trace" value="false"/>
  </bean>

  <!-- hsqldb datasource based on c3p0 -->
  <bean id="hsqldb-c3p0-dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" depends-on="localHsqldbServerBean">

    <property name="user" value="SA"/>
    <property name="password" value=""/>
    <property name="driverClass" value="org.hsqldb.jdbcDriver"/>
    <property name="jdbcUrl" value="jdbc:hsqldb:hsql://localhost:9001/devshowcase"/>

    <property name="acquireIncrement" value="${mysql.c3p0.acquireIncrement}"/>
    <property name="initialPoolSize" value="${mysql.c3p0.initialPoolSize}"/>
    <property name="minPoolSize" value="${mysql.c3p0.minPoolSize}"/>
    <property name="maxPoolSize" value="${mysql.c3p0.maxPoolSize}"/>
    <property name="checkoutTimeout" value="${mysql.c3p0.checkoutTimeout}"/>
    <property name="maxIdleTime" value="${mysql.c3p0.maxIdleTime}"/>
    <property name="maxIdleTimeExcessConnections" value="${mysql.c3p0.maxIdleTimeExcessConnections}"/>
  </bean>

 
PS:经过测试此DataSource可以用于Jdbc的操作,也可以用户Hibernate的操作。

当启动了Web项目后,就可以通过HSQLDB自带的Client工具访问HSQLDB数据库了。具体参考方法如下:
1、首先本人将hsqldb.jar文件拷贝到了D:/hsqldb/下
2、登录Windows Command Prompt,并将当前目录切换至D:/hsqldb/下
3、执行java -cp hsqldb.jar org.hsqldb.util.DatabaseManager,此时就可以通过HSQLDB的mini客户端对HSQLDB进行操作了。
4、使用客户端工具创建好需要的表之后,通过HSQLDB客户端UI工具中执行'SHOWDOWN'命令关闭了内存数据库,然后关闭了Web程序。此时在Tomcat目录中的webapp/devshowcase/WEB-INF/HSQLDB/文件夹中可以看到HSQLDB的script文件和properties文件。


方法可能不是很高明但是可以使用,各位有什么好的建议请留言。谢谢各位。

你可能感兴趣的:(spring,Web,mysql,bean,HSQLDB)