Creating a shared HSQLDB database

冷暖自知 提交于 2019-12-30 06:52:30

问题


In-process HSQLDB database are not expected to be opened by others, even for file-based storage.

The documentation hints that this is possible: Server Modes, Advanced Topics, but I've not yet found a URL for how to activate this behaviour.

Did anyone do this so they can share how to?


回答1:


The following works for me:

  1. Start a server from your code, which is shown in the org.hsqldb.test.TestBase code in the HSQLDB source code. Something like:

    Server server = new Server();
    server.setDatabaseName(0, "test");
    server.setDatabasePath(0, "file:/path/to/db");
    server.start();
    
  2. In the same JVM, open a Connection to the same database the way you would in a normal (non-Server) program, like:

    conn = DriverManager.getConnection("jdbc:hsqldb:file:/path/to/db");
    

Then use conn to execute whatever SQL you want.

In my own experimentation this appears to work, and it works for in-memory and file database.

Other JVMs, of course, will need to connect to the server using TCP. Trying to open the database file directly in another JVM will result in the usual error about the database being locked.




回答2:


I've experience with starting HQLDB in server mode outside of application and connecting from several applications to the database.

As described in the doc the process is straight-forward.



来源:https://stackoverflow.com/questions/964540/creating-a-shared-hsqldb-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!