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

时间:2022-09-11 14:45:19

How to start H2 database in server mode. I need to start it from my application.I tried the following code:

如何在服务器模式下启动H2数据库。我需要从我的申请开始。我尝试了以下代码:

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

Here is the properties for the connection:

以下是连接的属性:

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 =

When I run the program I got the following error:

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

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)

Thanks,

谢谢,

4 个解决方案

#1


24  

As the exception message says, "Database may be already in use". You need to close all other connection(s), to make sure the database is not open in another process concurrently.

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

By the way, don't use AUTO_SERVER=TRUE and the server mode at the same time. See the documentation for the automatic mixed mode. Use either one.

顺便说一下,不要在同一时间使用AUTO_SERVER=TRUE和服务器模式。请参阅有关自动混合模式的文档。使用一个。

I guess you are a bit confused about the different connection modes. I suggest to read the documentation about the connection modes, to make sure you understand it.

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

#2


15  

From command line,

从命令行,

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

this will launch an h2 database in server mode:

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

Web Console server running at http://A.B.C.D:8082 (others can connect) Failed to start a browser to open the URL http://A.B.C.D:8082: Browser detection failed and system property h2.browser not set 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)

运行于http://A.B.C的Web控制台服务器。D:8082(其他人可以连接)未能启动浏览器打开URL http://A.B.C。D:8082:浏览器检测失败,系统属性h2。浏览器没有设置TCP服务器在TCP:/ a.b.cD:9092(其他人可以连接)PG服务器运行在PG:// a.b.cD:5435(只有本地连接)

open a browser to have an admin GUI

打开一个具有管理GUI的浏览器

#3


11  

You can use the following code to run H2 in server mode.

您可以使用以下代码在服务器模式下运行H2。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:target/h2/ps;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

You can use SQuirrel SQL client (http://squirrel-sql.sourceforge.net/) to connect to you H2 database and look at the tables.

您可以使用SQuirrel SQL客户机(http://squirrel-sql.sourceforge.net/)连接到H2数据库并查看表。

  1. Create new connection.
  2. 创建新的连接。
  3. Select H2 in the driver dropdown menu
  4. 在驱动程序下拉菜单中选择H2
  5. Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
  6. 将url设置为项目目标文件夹h2 (jdbc:h2:C:\项目\工作空间\TestProject\target/h2/ps;AUTO_SERVER=true)
  7. Enter user name ("sa")
  8. 输入用户名(“公司”)
  9. Enter password ("")
  10. 输入密码(" ")

#4


2  

Close all the applications that using H2 (web console, etc) Then add the AUTO_SERVER=TRUE to the end of the location in h2 console and also in java program (which you already have done)

关闭所有使用H2 (web控制台等)的应用程序,然后将AUTO_SERVER=TRUE添加到H2控制台和java程序(您已经完成了)中的位置末尾

#1


24  

As the exception message says, "Database may be already in use". You need to close all other connection(s), to make sure the database is not open in another process concurrently.

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

By the way, don't use AUTO_SERVER=TRUE and the server mode at the same time. See the documentation for the automatic mixed mode. Use either one.

顺便说一下,不要在同一时间使用AUTO_SERVER=TRUE和服务器模式。请参阅有关自动混合模式的文档。使用一个。

I guess you are a bit confused about the different connection modes. I suggest to read the documentation about the connection modes, to make sure you understand it.

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

#2


15  

From command line,

从命令行,

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

this will launch an h2 database in server mode:

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

Web Console server running at http://A.B.C.D:8082 (others can connect) Failed to start a browser to open the URL http://A.B.C.D:8082: Browser detection failed and system property h2.browser not set 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)

运行于http://A.B.C的Web控制台服务器。D:8082(其他人可以连接)未能启动浏览器打开URL http://A.B.C。D:8082:浏览器检测失败,系统属性h2。浏览器没有设置TCP服务器在TCP:/ a.b.cD:9092(其他人可以连接)PG服务器运行在PG:// a.b.cD:5435(只有本地连接)

open a browser to have an admin GUI

打开一个具有管理GUI的浏览器

#3


11  

You can use the following code to run H2 in server mode.

您可以使用以下代码在服务器模式下运行H2。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:target/h2/ps;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

You can use SQuirrel SQL client (http://squirrel-sql.sourceforge.net/) to connect to you H2 database and look at the tables.

您可以使用SQuirrel SQL客户机(http://squirrel-sql.sourceforge.net/)连接到H2数据库并查看表。

  1. Create new connection.
  2. 创建新的连接。
  3. Select H2 in the driver dropdown menu
  4. 在驱动程序下拉菜单中选择H2
  5. Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
  6. 将url设置为项目目标文件夹h2 (jdbc:h2:C:\项目\工作空间\TestProject\target/h2/ps;AUTO_SERVER=true)
  7. Enter user name ("sa")
  8. 输入用户名(“公司”)
  9. Enter password ("")
  10. 输入密码(" ")

#4


2  

Close all the applications that using H2 (web console, etc) Then add the AUTO_SERVER=TRUE to the end of the location in h2 console and also in java program (which you already have done)

关闭所有使用H2 (web控制台等)的应用程序,然后将AUTO_SERVER=TRUE添加到H2控制台和java程序(您已经完成了)中的位置末尾