我在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 */
}
这些是中的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
谢谢你的帮助