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

发布于 2025-01-06 13:09:07 字数 1501 浏览 1 评论 0原文

我需要从我的应用程序以服务器模式启动 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)

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧话新听 2025-01-13 13:09:07

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

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

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

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.

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.

剩一世无双 2025-01-13 13:09:07

从命令行,

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

From command line,

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

this will launch an h2 database in server mode:

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)

open a browser to have an admin GUI

眼藏柔 2025-01-13 13:09:07

您可以使用以下代码在服务器模式下运行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>

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

  1. 创建新连接。
  2. 在驱动程序下拉菜单中选择 H2
  3. 将 url 设置为项目目标文件夹 h2 文件夹 (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
  4. 输入用户名 ("sa")
  5. 输入密码(“”)

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

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

  1. Create new connection.
  2. Select H2 in the driver dropdown menu
  3. Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
  4. Enter user name ("sa")
  5. Enter password ("")
狼亦尘 2025-01-13 13:09:07

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

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)

第七度阳光i 2025-01-13 13:09:07

我在尝试启动 H2 时收到此错误。
另请参阅 http://h2database.com/javadoc/org/h2/tools/服务器.html

线程“main”org.h2.jdbc.JdbcSQLException中出现异常:功能不
支持:“-~webAllowOthers”[50100-197]

所以我按照以下步骤操作:

  1. make dir mkdir h2db 该目录将包含您的数据库文件。
  2. 点击此命令:java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0
    此命令将启动 h2
  3. 如果您想在后端运行 h2,请打开 vi h2.sh 并将此命令粘贴到以下位置:
    nohup java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0/ &
  4. 现在运行./bin.h2.sh。

I was getting this error when trying to start H2.
See also http://h2database.com/javadoc/org/h2/tools/Server.html

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Feature not
supported: "-~webAllowOthers" [50100-197]

So I followed these steps:

  1. make dir mkdir h2db this directory will have your db files.
  2. Hit this command : java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0
    this command will start h2
  3. If you want to run h2 in backend then open vi h2.sh and paste this command in this:
    nohup java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0/ &
  4. Now run ./bin.h2.sh.
望喜 2025-01-13 13:09:07

还有一种方法。您可以定义@Configuration bean,例如:

@Configuration
public class H2Configuration {

    @Bean
    public void startTCPServer(){
        try {
            Server h2Server = Server.createTcpServer().start();
            if (h2Server.isRunning(true)) {
                System.out.println(h2Server.getStatus());
            } else {
                throw new RuntimeException("Could not start H2 server.");
            }
        } catch (SQLException e) {
            throw new RuntimeException("Failed to start H2 server: ", e);
        }
    }
}

There is one more way. You may define @Configuration bean, e.g.:

@Configuration
public class H2Configuration {

    @Bean
    public void startTCPServer(){
        try {
            Server h2Server = Server.createTcpServer().start();
            if (h2Server.isRunning(true)) {
                System.out.println(h2Server.getStatus());
            } else {
                throw new RuntimeException("Could not start H2 server.");
            }
        } catch (SQLException e) {
            throw new RuntimeException("Failed to start H2 server: ", e);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文