java 如何从我的 Web 应用程序中以服务器模式启动并继续运行 hsqldb?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3660502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I start and keep running hsqldb in server mode from within my web application?
提问by Ravish Bhagdev
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).
我不想在嵌入式模式下使用它,因为我也可能允许其他外部应用程序访问它。我想在 Tomcat 加载我的应用程序的同时(或者就在 tomcat 运行时)执行服务器的启动。这样我就不必要求客户手动运行带有命令或脚本的 hsqldb,然后他们才能将我的War放入 tomcat 并运行它(以保持简单)。
I can perhaps call Serverfrom 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?
我也许可以通过从 Java 发送命令来从 main调用服务器,但这会给我一个无休止的线程,我不知道如何处理。有没有更简单的测试方法来做到这一点?
采纳答案by Ernesto Campohermoso
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:
根据 HSQLDB 文档,可以从 Java 代码启动数据库:http: //hsqldb.org/doc/2.0/guide/listeners-chapt.html#listeners_appstart-sect。因此,您可以在 Web 应用程序启动时使用 servlet 加载数据库。步骤应如下:
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); } }
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>
创建一个Servlet“InitDatabase”并将启动数据库的代码放在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()。
<servlet> <servlet-name>InitDatabase</servlet-name> <servlet-class>bo.hsqltest.InitDatabase</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
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。要在应用程序停止时关闭数据库,您可以覆盖 InitServlet 的方法 destroy()。在 destroy 方法中,您必须像普通的 sql 查询一样(通过 JDBC)执行命令“SHUTDOWN”。
回答by fredt
You can use HSQLDB on any web/app server in embedded mode and allow external apps to access it by running the org.hsqldb.server.Servlet on your web/app server. The external apps will then connect to the servlet using the jdbc:hsqldb:http: type url's.
你可以在任何嵌入模式的 web/app 服务器上使用 HSQLDB,并允许外部应用程序通过在你的 web/app 服务器上运行 org.hsqldb.server.Servlet 来访问它。然后外部应用程序将使用 jdbc:hsqldb:http: 类型 url 连接到 servlet。
If you want to run org.hsqldb.server.Server (the version 2.0 class), or org.hsqldb.Server, as a separate process as Tomcat, you can use the class org.hsqldb.util.MainInvoker to call the main method for multiple classes (e.g. Tomcat and HSQLDB Server) with Server called first.
如果你想运行 org.hsqldb.server.Server(2.0 版本的类),或者 org.hsqldb.Server,作为 Tomcat 的一个单独的进程,你可以使用类 org.hsqldb.util.MainInvoker 来调用 main 方法对于首先调用服务器的多个类(例如 Tomcat 和 HSQLDB 服务器)。
回答by rtaugerbeck
This question is quite dated, but I recently ran into a similar situation. I tried to access the embedded HSQLDB database running in a TomEE server. @fredt's answer gave me the decisive hint, but I was still struggling to find out all the details. Here is my solution:
这个问题已经过时了,但我最近遇到了类似的情况。我试图访问在 TomEE 服务器中运行的嵌入式 HSQLDB 数据库。@fredt 的回答给了我决定性的提示,但我仍在努力找出所有细节。这是我的解决方案:
Add a servlet mapping for org.hsqldb.server.Servletto either a web.xmlor web-fragment.xmldescriptor file:
将org.hsqldb.server.Servlet的 servlet 映射添加到web.xml或web-fragment.xml描述符文件:
<servlet>
<servlet-name>hsqldb</servlet-name>
<servlet-class>org.hsqldb.server.Servlet</servlet-class>
<init-param>
<param-name>hsqldb.server.database</param-name>
<param-value>${catalina.base}/hsqldb/db</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hsqldb</servlet-name>
<url-pattern>/hsqldb</url-pattern>
</servlet-mapping>
Using the parameter hsqldb.server.databaseyou can specify the location of your database. Because Tomcat/Tomcat distinguishes between CATALINA_HOMEand CATALINA_BASE, simply specifying a relative path like "hsqldb/db" is not enough. But you can use system properties provided by Tomcat/TomEE, like ${catalina.base}to make your path absolute.
使用参数hsqldb.server.database可以指定数据库的位置。因为 Tomcat/Tomcat 区分CATALINA_HOME和CATALINA_BASE,所以仅仅指定像“hsqldb/db”这样的相对路径是不够的。但是您可以使用 Tomcat/TomEE 提供的系统属性,例如${catalina.base}使您的路径成为绝对路径。
After starting your server the HSQLDB database can be accessed via the URL
启动服务器后,可以通过 URL 访问 HSQLDB 数据库
jdbc:hsqldb:http://localhost:8080/<context-path>/hsqldb