Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30464997
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-10-28 10:30:04

使用servlet方法配合hibernate启动会有异常,尤其用pool
hsqldb随tomcat启动
改进方法:使用listener
  1. package cn.qbq.database;

  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.util.logging.Logger;

  5. import javax.servlet.ServletContextEvent;
  6. import javax.servlet.ServletContextListener;

  7. import org.hsqldb.Server;

  8. public class StartHSQLDBListener implements ServletContextListener {

  9.     private Logger logger = Logger.getLogger(StartHSQLDBListener.class.getName());

  10.     private Server server;

  11.     private final static String HSQLDB_PATH_PARAM_NAME = "HSQLDBPath";
  12.     private final static String HSQLDB_NAME_PARAM_NAME = "HSQLDBName";
  13.     private final static String HSQLDB_PORT_PARAM_NAME = "HSQLDBPort";

  14.     private final static String DEFAULT_HSQLDB_PATH = "/WEB-INF/lib/hsqldb/";
  15.     private final static String DEFAULT_HSQLDB_NAME = "labdb";
  16.     private final static int DEFAULT_HSQLDB_PORT = 9001;

  17.     @Override
  18.     public void contextDestroyed(ServletContextEvent event) {
  19.         if (server != null) {
  20.             server.shutdown();
  21.         }
  22.     }

  23.     @Override
  24.     public void contextInitialized(ServletContextEvent event) {
  25.         String HSQLDBPath = event.getServletContext().getInitParameter(HSQLDB_PATH_PARAM_NAME);
  26.         String HSQLDBName = event.getServletContext().getInitParameter(HSQLDB_NAME_PARAM_NAME);
  27.         String paramPort = event.getServletContext().getInitParameter(HSQLDB_PORT_PARAM_NAME);
  28.         if (HSQLDBPath == null || HSQLDBPath.length() == 0) {
  29.             HSQLDBPath = DEFAULT_HSQLDB_PATH;
  30.         }
  31.         if (HSQLDBName == null || HSQLDBName.length() == 0) {
  32.             HSQLDBName = DEFAULT_HSQLDB_NAME;
  33.         }
  34.         int HSQLDBPort = DEFAULT_HSQLDB_PORT;
  35.         if (paramPort != null && paramPort.length() > 0) {
  36.             HSQLDBPort = Integer.valueOf(paramPort);
  37.         }
  38.         String path = event.getServletContext().getRealPath(HSQLDBPath) + "/" + HSQLDBName;

  39.         ServerSocket s = null;//因为有可能端口被占用,Server类内部会出异常,先在外部看下端口
  40.         try {
  41.             s = new ServerSocket(HSQLDBPort);
  42.         } catch (IOException e) {
  43.             logger.info("HSQLDB was not started caused by " + e.getMessage());
  44.             return;
  45.         } finally {
  46.             if (s != null) {
  47.                 try {
  48.                     s.close();
  49.                 } catch (IOException e) {
  50.                     logger.warning(e.getMessage());
  51.                     return;
  52.                 }
  53.             }
  54.         }

  55.         server = new Server();
  56.         server.setDatabaseName(0, HSQLDBName);
  57.         server.setDatabasePath(0, path);
  58.         server.setPort(HSQLDBPort);
  59.         server.setSilent(true);

  60.         if (server == null) {
  61.         }

  62.         Thread thread = new Thread(new Runnable() {
  63.             public void run() {
  64.                 server.start();
  65.             }
  66.         });
  67.         thread.start();
  68.     }

  69. }

web.xml加入如下:
  1. <!-- Start HSQLDB server -->
  2.     <context-param>
  3.         <param-name>HSQLDBPath</param-name>
  4.         <param-value>/WEB-INF/lib/hsqldb/</param-value>
  5.     </context-param>
  6.     
  7.     <context-param>
  8.         <param-name>HSQLDBName</param-name>
  9.         <param-value>labdb</param-value>
  10.     </context-param>

  11.     <context-param>
  12.         <param-name>HSQLDBPort</param-name>
  13.         <param-value>9001</param-value>
  14.     </context-param>

  15.     <listener>
  16.         <listener-class>cn.qbq.database.StartHSQLDBListener</listener-class>
  17.     </listener>

阅读(2208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~