简体   繁体   English

如何在多个 Spring Boot 应用程序之间共享 H2 内存数据库?

[英]How do I share H2 in-memory database across multiple Spring boot applications?

This is a different scenario than the other answered questions.这是与其他已回答问题不同的场景。 I have one Spring boot application (app #1) that uses embedded H2 in-memory database and exposes it as a server.我有一个 Spring Boot 应用程序(应用程序 #1),它使用嵌入式 H2 内存数据库并将其公开为服务器。 My second Spring boot application (app #2) connects to the H2 server from app #1 and that works correctly.我的第二个 Spring Boot 应用程序(应用程序 #2)从应用程序 #1 连接到 H2 服务器,并且工作正常。 I can store and retrieve data from H2.我可以从 H2 存储和检索数据。 Now here is the problem.现在问题来了。 I have a third Spring boot (app #3) and it connects to the H2 server from app #1.我有第三个 Spring Boot(应用程序 #3),它从应用程序 #1 连接到 H2 服务器。 The connection works but it (Issue #1) clears all the data that had been stored previously by app #2.连接有效,但它(问题 #1)会清除应用程序 #2 之前存储的所有数据。 After this, now both app #2 and #3 can store and retrieve its data and also each other's data.在此之后,现在应用程序 #2 和 #3 都可以存储和检索其数据以及彼此的数据。 Now I terminate either app #2 or app #3 and (Issue #2) the remaining app cannot continue using the H2 in-memory database.现在我终止应用程序 #2 或应用程序 #3 并且(问题 #2)剩余的应用程序无法继续使用 H2 内存数据库。 Are issues #1 and #2 normal behavior?问题 #1 和 #2 是正常行为吗? If not, how can I fix them?如果没有,我该如何修复它们? I want my H2 data to stay as long as app #1 (the H2 server) is running, even if other apps have been disconnected and I don't want my data cleared when new apps connect to the H2 server.我希望我的 H2 数据在应用程序 #1(H2 服务器)运行时一直保留,即使其他应用程序已断开连接,并且我不希望在新应用程序连接到 H2 服务器时清除我的数据。

Found this in the docs here: https://h2database.com/html/features.html在这里的文档中找到了这个: https : //h2database.com/html/features.html

By default, closing the last connection to a database closes the database.默认情况下,关闭与数据库的最后一个连接将关闭该数据库。 For an in-memory database, this means the content is lost.对于内存数据库,这意味着内容丢失。 To keep the database open, add ;DB_CLOSE_DELAY=-1 to the database URL.要保持数据库打开,请将 ;DB_CLOSE_DELAY=-1 添加到数据库 URL。 To keep the content of an in-memory database as long as the virtual machine is alive, use jdbc:h2:mem:test;DB_CLOSE_DELAY=-1.要在虚拟机处于活动状态时保留内存数据库的内容,请使用 jdbc:h2:mem:test;DB_CLOSE_DELAY=-1。

So I think your problem might be solved if you just set the db line in your application.properties file for app #1 to:因此,我认为如果您将 application.properties 文件中应用程序 #1 的 db 行设置为:

spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1

Please let me know if this fixes your problem, as I'm very curious.请让我知道这是否能解决您的问题,因为我很好奇。 Your description seems to suggest it's a bit more complicated than this...overlapping access from the two apps involved...but this sure sounds like the kind of parameter you're missing.您的描述似乎表明它比这更复杂...涉及两个应用程序的重叠访问...但这听起来确实像是您缺少的那种参数。

i did it this way , register multiple H2 servers in you spring configuration我是这样做的,在你的 spring 配置中注册多个 H2 服务器

@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server1() throws SQLException {
    return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9080");
}

// Second App
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server2() throws SQLException {
    return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9081");
}

then while connecting to H2 used below config然后在连接到配置下面使用的 H2 时

for file based H2基于文件的 H2

url=jdbc:h2:tcp://localhost:9081/<path to H2>
username=sa
password=sa

for in-memory H2 database用于内存 H2 数据库

url=jdbc:h2:tcp://localhost:9081/<H2 database name>
username=sa
password=sa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM