如何在Web应用程序中以服务器模式启动并继续运行hsqldb? [英] How can I start and keep running hsqldb in server mode from within my web application?

查看:73
本文介绍了如何在Web应用程序中以服务器模式启动并继续运行hsqldb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在嵌入式模式下使用它,因为我可能允许其他外部应用程序也访问它.而且我想在Tomcat加载我的应用程序的同时(或者只是在tomcat运行时)执行服务器的启动.这样一来,我不必要求客户端使用命令或脚本手动运行hsqldb,然后他们才能将我的战争投入tomcat并运行它(以简化操作).

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).

我也许可以通过发送命令从主机中调用服务器从Java,但这将给我一个永无止境的线程,我不确定如何处理.有没有更简单的经过测试的方法可以做到这一点?

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?

推荐答案

根据HSQLDB文档,可以从Java代码启动数据库: http://hsqldb.org/doc/2.0/guide/listeners-chapt.html# listeners_appstart-sect .因此,当Web应用程序启动时,您可以使用servlet加载数据库.步骤应为以下步骤:

According to the HSQLDB Documentation is possible start the database from Java Code: http://hsqldb.org/doc/2.0/guide/listeners-chapt.html#listeners_appstart-sect. So you can use a servlet for load the database when the web application is starting. The steps should be the following:

  1. 创建一个Servlet"InitDatabase",并将用于启动数据库的代码放在init()方法上

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

@Override
public void init() throws ServletException {
    super.init();
    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.setProperties(p);
        server.setLogWriter(null); // can use custom writer
        server.setErrWriter(null); // can use custom writer
        server.start();
    } catch (AclFormatException afex) {
        throw new ServletException(afex);
    } catch (IOException ioex) {
        throw new ServletException(ioex);
    }
}

  • 在您的web.xml中添加启动时的属性负载,并将其设置为1.这用于在Web应用程序启动时调用方法init().

  • 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.

    <servlet>
        <servlet-name>InitDatabase</servlet-name>
        <servlet-class>bo.hsqltest.InitDatabase</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet> 
    

  • 完成此操作后,Web应用程序将在新线程中启动HSQLDB.为了在应用程序停止时关闭数据库,您可以重写InitServlet的destroy()方法.在destroy方法中,您必须像普通的sql查询一样(通过JDBC)执行命令"SHUTDOWN".

    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).

    这篇关于如何在Web应用程序中以服务器模式启动并继续运行hsqldb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆