gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:57:53 24 4
gpt4 key购买 nike

这是一个与其他回答的问题不同的场景。我有一个 Spring boot 应用程序(应用程序 #1),它使用嵌入式 H2 内存数据库并将其公开为服务器。我的第二个 Spring boot 应用程序(应用程序 #2)从应用程序 #1 连接到 H2 服务器并且工作正常。我可以从 H2 存储和检索数据。现在问题来了。我有第三个 Spring boot(应用程序 #3),它从应用程序 #1 连接到 H2 服务器。连接有效,但它(问题#1)清除了应用#2 之前存储的所有数据。此后,现在应用程序 #2 和 #3 都可以存储和检索其数据以及彼此的数据。现在,我终止应用程序 #2 或应用程序 #3,并且(问题 #2)剩余的应用程序无法继续使用 H2 内存数据库。问题 #1 和 #2 是正常行为吗?如果没有,我该如何修复它们?我希望只要应用程序 #1(H2 服务器)运行,我的 H2 数据就会保留下来,即使其他应用程序已断开连接,并且我不希望在新应用程序连接到 H2 服务器时清除我的数据。

最佳答案

在此处的文档中找到此内容: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. 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.

所以我认为如果您只需将应用程序 #1 的 application.properties 文件中的 db 行设置为:

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

请告诉我这是否可以解决您的问题,因为我很好奇。您的描述似乎表明它比这更复杂一点...涉及的两个应用程序的访问重叠...但这听起来确实像是您缺少的参数。

关于java - 如何在多个 Spring boot 应用程序之间共享 H2 内存数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55273325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com