无法连接到H2数据库

发布于 2025-02-11 03:02:34 字数 746 浏览 0 评论 0原文

通过使用以下连接字符串,我一直在努力连接H2数据库,如数据库URL概述部分:

spring.datasource.url=jdbc:h2:tcp://localhost:9092/~/test-db

我还尝试了TCP(服务器模式)连接的许多不同组合,但仍然获得错误,例如“ 连接” 已损坏:“ Java.net。 sockettimeoutexception:连接时间到了:LocalHost:9092“ 运行Spring Boot应用程序时。

@SpringBootApplication
public class Application {

    // code omitted
    
    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }
}

因此,如何解决此问题并通过服务器模式连接到H2数据库?

I have been getting struggle to connect H2 database from a Spring Boot app by using the following connection string as mentioned on Database URL Overview section:

spring.datasource.url=jdbc:h2:tcp://localhost:9092/~/test-db

I also tried many different combination for tcp (server mode) connection, but still get error e.g. "Connection is broken: "java.net.SocketTimeoutException: connect timed out: localhost:9092" when running Spring Boot app.

@SpringBootApplication
public class Application {

    // code omitted
    
    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }
}

So, how can I fix this problem and connect to H2 database via server mode?

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

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

发布评论

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

评论(1

凉月流沐 2025-02-18 03:02:34

您似乎有点困惑。

H2可以以两种不同的“模式”运行。

本地模式

本地模式表示H2'Just Works',您可以使用文件访问此模式:在JDBC Connect URL中的东西。 JDBC驱动程序本身可以完成所有数据库工作,就像打开文件,写入数据一样,它可以完成所有工作。 根本没有“数据库服务器” 。或者,如果您愿意,JDBC驱动程序是其自己的服务器,尽管它没有打开端口。

在这种情况下,服务器模式

您需要一个(单独的)JVM并在服务器模式下单独启动H2 ,然后您可以使用相同的库(仍然h2.jar)服务作为JDBC服务器。在此模式下,这两件事是完全独立的 - 如果需要,您可以在一台计算机上运行H2.JAR,将其作为服务器,并在完全不同的机器上运行相同的H2.jar连接到另一台H2机器。数据库服务器计算机完成了大部分工作,而“客户端” H2只是JDBC驱动程序。 H2与这种模式中的MySQL或Postgr没有什么不同:您有一个“应用程序” / jvm流程,该过程以数据库引擎的形式运行,允许多个不同的进程,即使您想,如果您想,如果您愿意,也来自世界各地的完全不同的机器连接到它。

您可以在JDBC字符串中使用tcp:事物访问此模式。

如果您真的想要,您可以运行此模式,并且仍然可以在一台机器上,甚至是一台JVM,但是为什么要这样做?无论您认为,这将解决“锁定错误”,这将无法通过在单个JVM上运行所有这些东西来解决。只有两个选择:

  • 您正在错误地解决这个问题。
  • 您确实确实有多个单独的JVM进程(一台具有2个java < / code>进程的计算机 / ps auxww < / code> output / task Manager或2+机器)都试图尝试连接到单个数据库,在这种情况下,您确实需要这个数据库,是的。

如何正确执行服务器模式,

您很可能需要一个以前启动并托管H2数据库的单独的JVM;它需要在“客户端” JVM(将连接到它的jvms)开始运行之前运行。 Catalina不是您要寻找的“服务器”,它是org.h2.tools.server,如果它说“找不到”,则需要修复Maven Imports。这需要是单独的JVM(您可以编写有关的代码:哦,嘿,没有使用H2服务器运行的单独的JVM为了永远留在空中,这很奇怪。

您没有解释自己在做什么。但是,假设您正在做的是这样的:

  • 我有一个CI脚本,可以启动多个单独的JVM,有些是并行的,它并行运行了一堆集成和单位测试。
  • 即使它们并行运行(或可能是故意的),大家都希望从一个DB中运行它。这通常是一个非常糟糕的主意(您希望测试被隔离;自行运行它们的行为继续相同。您不希望测试失败,只有在运行相同批次的情况下才能重现18使用相同的运行代码的18个单独测试,其中一个无关的测试以特定的方式失败,而这是星期二,满月和贝多芬在您的音乐播放器中播放,并且在房间里比24º温暖,影响了CPU的油门,cpu的节流,当然,如果您尝试在多个测试中重新利用资源,这正是发生的!
  • ...然后,编辑CI脚本首先启动托管H2服务器的JVM,一旦启动并运行,大概运行了一个过程,该进程填充该数据库中的测试数据,一旦完成,然后在并行运行所有测试,一旦所有这些都完成,请关闭JVM,然后删除数据库文件。

确切的方法是第三部分是一个单独的问题 - 如果您需要帮助,请提出一个新问题,并命名您用来运行此内容的相关工具,粘贴配置文件等。

You seem to be a little confused.

H2 can run in two different 'modes'.

Local mode

Local mode means H2 'just works', and you access this mode with the file: thing in the JDBC connect URL. The JDBC driver itself does all the database work, as in, it opens files, writes data, it does it all. There is no 'database server' at all. Or, if you prefer, the JDBC driver is its own server though it opens no ports.

Server mode

In this case you need a (separate) JVM and separately fire up H2 in server mode and then you can use the same library (still h2.jar) to serve as a JDBC server. In this mode, the two things are completely separate - if you want, you can run h2.jar on one machine to be the server, and run the same h2.jar on a completely different machine just to connect to the other H2 machine. The database server machine does the bulk of the work, with the 'client' H2 just being the JDBC driver. H2 is no different than e.g. mysql or postgres in such a mode: You have one 'app' / JVM process that runs as a database engine, allowing multiple different processes, even coming from completely different machines halfway around the world if you want to, to connect to it.

You access this mode with the tcp: thing in the JDBC string.

If you really want, you can run this mode and still have it all on a single machine, even a single JVM, but why would you want to? Whatever made you think this will 'solve lock errors' wouldn't be fixed by running all this stuff on a single JVM. There are only two options:

  • You're mis-analysing the problem.
  • You really do have multiple separate JVM processes (either one machine with 2 java processes in the activity monitor / ps auxww output / task manager, or 2+ machines) all trying to connect to a single database in which case you certainly do need this, yes.

How to do server mode right

You most likely want a separate JVM that starts before and that hosts the h2 database; it needs to run before the 'client' JVMs (the ones that will connect to it) start running. Catalina is not the 'server' you are looking for, it is org.h2.tools.Server, and if it says 'not found' you need to fix your maven imports. This needs be a separate JVM (you COULD write code that goes: Oh, hey, there isn't a separate JVM running with the h2 server so I'll start it in-process right here right now, but that means that process needs to stay in the air forever, which is just weird. Hence, you want a separate JVM process for this).

You haven't explained what you're doing. But, let's say what you're doing is this:

  • I have a CI script that fires up multiple separate JVMs, some in parallel even, which runs a bunch of integration and unit tests in parallel.
  • Even though they run in parallel (or perhaps intentionally so), you all want to run this off of a single DB. This is usually a really bad idea (you want tests to be isolated; that running them on their own continues to behave identically. You don't want a test to fail in a way that can only be reproduced if you run the same batch of 18 separate tests using the same run code, where one unrelated test fails in a specific fashion, whilst it's Tuesday, a full moon, and Beethoven is playing in your music player, and it's warmer than 24º in the room affecting the CPU's throttling, of course. Which is exactly what tends to happen if you try to re-use resources in multiple tests!) – still, you somehow really want this.
  • ... then, edit the CI script to first Launch a JVM that hosts a H2 server, and once that's up and running, presumably run a process that fills this database with test data, and once that's done, then run all tests in parallel, and once those are all done, shut down the JVM, and delete the DB file.

Exactly how to do the third part is a separate question - if you need help with that, ask a new question and name the relevant tool(s) you are using to run this stuff, paste the config files, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文