我正在尝试在Spring启动应用程序中使用H2数据库。下面是application.properties文件。
# Local db filestore: in your home folder
spring.datasource.url=jdbc:h2:file:~/db/rd_light.db;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
# h2 defaults:
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
# Use "create" or "create-drop" when you wish to recreate database on restart; use
# "update" or "validate" when data is to be kept.
spring.jpa.hibernate.ddl-auto = update
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.flyway.enabled=true
spring.flyway.url=jdbc:h2:file:~/db/rd_light.db
spring.flyway.user=sa
spring.flyway.password=
spring.flyway.locations=classpath:db/migration
当我运行应用程序时,出现以下错误。
2018-06-11 12:19:53.160 ERROR 19455 --- [JavaFX-Launcher] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.h2.jdbc.JdbcSQLException: Database "/home/administrator/db/rd_light.db" not found [90013-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357) ~[h2-1.4.197.jar:1.4.197]
at org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.197.jar:1.4.197]
我尝试手动创建文件,但仍然遇到同样的错误。 即使我尝试在内存数据库中使用而不是基于文件,我也会得到相同的错误。
答案 0 :(得分:1)
在spring.datasource.url
中删除标记IFEXISTS=TRUE;
。
如果该标志设置为true,则仅在数据库存在的情况下建立连接。有关该标志的参考,请参见http://www.h2database.com/html/features.html#database_only_if_exists