有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我可以使用inmemory模式将一些数据持久化到H2数据库中吗?

我想使用h2作为数据库将数据准确地保存在in-memory mode

所以我有application.properties文件,配置如下:

spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=...
spring.datasource.password=...
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.session.jdbc.initialize-schema=always

使用特定参数:

DB_CLOSE_DELAY=-1-保持数据库打开/在虚拟机处于活动状态时保持内存中数据库的内容
DB_CLOSE_ON_EXIT=FALSE-在退出时禁用数据库关闭

H2 database文件所述

同时,我使用Entity保存在数据库中:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "address_entity")
@Table(name = "addresses")
public class GeolocationAddress {

    public GeolocationAddress(GeolocationAddressDTO geolocationAddressDTO) {
        this.displayName = geolocationAddressDTO.getDisplayName();
        this.lat = geolocationAddressDTO.getLat();
        this.lon = geolocationAddressDTO.getLon();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "address_id")
    private Integer id;

    private String displayName;

    private String lat;

    private String lon;
}

发送请求后,我将从Hibernate获取控制台信息:

Hibernate: insert into addresses (address_id, display_name, lat, lon) values (null, ?, ?, ?)
Hibernate: insert into addresses (address_id, display_name, lat, lon) values (null, ?, ?, ?)

但是在使用Ctrl+F5组合或通过以下方式刷新Data Source中的H2数据库之后:

enter image description here

钥匙,我没有看到任何数据

为了研究这种行为,我检查了documentation中的公式:

"For certain use cases (for example: rapid prototyping, testing, high performance operations, read-only databases), it may not be required to persist data, or persist changes to the data. This database supports the in-memory mode, where the data is not persisted."

以及来自一些articles,例如:

"By design, the in-memory database is volatile, and data will be lost when we restart the application."

this一:

"H2 is an in memory database. Its not a persisted database. H2 is a great tool for learning because you need zero setup."

但我仍然不完全理解,在应用程序运行时,我可以在这种模式下保存数据吗

如果我切换到其他模式,例如:

spring.datasource.url=jdbc:h2:file:./data/demo;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE

如前所述{a7}和{a8}

然后,是的,我在刷新后看到数据库中的数据,数据将以如下格式保存:

enter image description here

enter image description here

但是我能不能对in-memory做同样的事情

如能就此作出澄清,我将不胜感激

UPD#1:

例如,指示here

"Most in-memory database systems offer persistence, at least as an option."

UPD#2:

H2-console我可以看到使用in-memory模式的数据,但从Intellij Idea看不到:

enter image description here

UPD#3: 正如前面提到的@Turing85,我已经测试了url:jdbc:h2:tcp://localhost/mem:db1通过TCP/IPTLS访问数据库,但是这个链接在Data Source中无效

经过一些调查,我发现useful information

":mem will not work for TCP connections. so remove :mem from connection url"

所以另一种方式是:jdbc:h2:./data/testdb;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=10990 同样有效

UPD#4: 也许,这也与我的问题有关


共 (1) 个答案

  1. # 1 楼答案

    正如前面提到的Turing85Elliott FrischM. Deinumin-memory数据库与正在运行的Spring Boot应用程序隔离,这就是为什么我需要使用其他模式,例如:

    spring.datasource.url=jdbc:h2:file:./data/testdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
    

    或者

    spring.datasource.url=jdbc:h2:./data/testdb;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=10990