无法连接到H2数据库
通过使用以下连接字符串,我一直在努力连接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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎有点困惑。
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上运行所有这些东西来解决。只有两个选择:
java < / code>进程的计算机 /
ps auxww < / code> output / task Manager或2+机器)都试图尝试连接到单个数据库,在这种情况下,您确实需要这个数据库,是的。
如何正确执行服务器模式,
您很可能需要一个以前启动并托管H2数据库的单独的JVM;它需要在“客户端” JVM(将连接到它的jvms)开始运行之前运行。 Catalina不是您要寻找的“服务器”,它是
org.h2.tools.server
,如果它说“找不到”,则需要修复Maven Imports。这需要是单独的JVM(您可以编写有关的代码:哦,嘿,没有使用H2服务器运行的单独的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:
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:
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.