如何在服务器模式下运行H2数据库?

2022-09-01 05:48:33

我需要从我的应用程序以服务器模式启动 H2 数据库。尝试了以下代码:

server = Server.createTcpServer().start();

下面是连接的属性:

javabase.jdbc.url = jdbc:h2:tcp://localhost:9092/nio:~/source/db/database/db;AUTO_SERVER=TRUE
javabase.jdbc.driver = org.h2.Driver
javabase.jdbc.username = sa
javabase.jdbc.password =

当我运行该程序时,我得到以下错误:

client.db.exception.DAOException: org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-164]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.store.FileLock.getExceptionAlreadyInUse(FileLock.java:439)
    at org.h2.store.FileLock.lockFile(FileLock.java:336)
    at org.h2.store.FileLock.lock(FileLock.java:128)
    at org.h2.engine.Database.open(Database.java:542)
    at org.h2.engine.Database.openDatabase(Database.java:222)
    at org.h2.engine.Database.<init>(Database.java:217)
    at org.h2.engine.Engine.openSession(Engine.java:56)
    at org.h2.engine.Engine.openSession(Engine.java:159)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)
    at org.h2.engine.Engine.createSession(Engine.java:121)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:133)
    at java.lang.Thread.run(Thread.java:680)

答案 1

正如异常消息所说,“数据库可能已在使用中”。您需要关闭所有其他连接,以确保数据库不会同时在另一个进程中打开。

顺便说一句,不要同时使用服务器模式。请参阅有关自动混合模式的文档。使用其中任何一个。AUTO_SERVER=TRUE

我想你对不同的连接模式有点困惑。我建议阅读有关连接模式的文档,以确保您理解它。


答案 2

从命令行,

java -jar h2-1.3.160.jar -webAllowOthers -tcpAllowOthers

这将在服务器模式下启动 h2 数据库:

Web Console server running at http://A.B.C.D:8082 (others can connect)
TCP server running at tcp://A.B.C.D:9092 (others can connect)
PG server running at pg://A.B.C.D:5435 (only local connections)

打开浏览器以具有管理 GUI


推荐