java - Spring Boot - Web 应用程序无法停止 MVStore 编写器线程

标签 java spring hibernate spring-data-jpa h2

我在 Spring Boot 应用程序中使用嵌入式 H2 数据库,但每次关闭时,数据库都没有按照我在 application.properties 中配置的那样正确关闭/删除。 。这是我关闭应用程序时出现的错误:

2018-02-05 13:00:37.360 WARN  [localhost-startStop-2] [WebappClassLoaderBase] 
     The web application [ROOT] appears to have started a 
     thread named [MVStore background writer nio:C:/Users/user/testdb.mv.db] but has failed to stop it. 
     This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 org.h2.mvstore.MVStore$BackgroundWriterThread.run(MVStore.java:2715)

效果是文件 testdb.mv 保留在我的用户目录中,并且在后续启动时我得到

org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.USERS(ID)"; SQL statement:
INSERT INTO Users (id,first_name,last_name) VALUES (1,'Vincent', 'Vega') [23505-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)

这是data.sql启动时运行的脚本

INSERT INTO Users (id,first_name,last_name) VALUES (1,'Vincent', 'Vega');

这是User定义 @Table("Users") 的实体

@Entity
@Table(name="Users")
public class User 
{
    @Id
    @GeneratedValue
    private long id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;
    /* getters and setters */
}

这些是 application.properties 中与 h2 相关的配置

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none 
spring.jpa.database=H2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:~/testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
spring.datasource.name=testdb
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

感谢您的帮助

最佳答案

您正在组合 DB_CLOSE_DELAY=-1(告诉数据库不要自动关闭)和 DB_CLOSE_ON_EXIT=TRUE。如果您想使用 H2 拥有持久数据库,请将其更改为 DB_CLOSE_ON_EXIT=FALSE

关于java - Spring Boot - Web 应用程序无法停止 MVStore 编写器线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623375/

相关文章:

java - 如何使 java 6 兼容 Kafka?

java - 验证组继承

spring - grails 3 约束异常

java - 使用 Spring 在 View 中保留 `MultipartFile` 时出现问题

hibernate - grails/groovy 域类中 TimeDuration 的 Hibernate 类型是否正确?

java - Hibernate 返回设置为 EAGER 的对象的代理

java - 如何在java servlet中获取客户端MAC地址?

java - 在 JBoss 7.1.1 下使用应用程序的 Log4J 配置

java - Spring Boot 测试 - @WithMockUser 发布的内容为空

java - 使用java + spring实现时间段(Window)