Spring 应用程序连接到内存中的 H2 数据库而不是基于文件
Spring app connects to in-memory H2 db instead of file based
我正在尝试在磁盘中创建一个本地数据库并连接到它。基本上我想要 jdbc.h2.file,而不是 jdbc.h2.mem。这就是我设置 application.properties.file
的方式
spring.jpa.hibernate.ddl-auto = none
spring.datasource.url:jdbc:h2:file:~/test
spring.datasource.driverClassName:org.h2.Driver
spring.datasource.username:sa
spring.datasource.password:
spring.jpa.show-sql=true
spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.flyway.url:jdbc:h2:file:~/test
spring.flyway.user:sa
spring.flyway.password:
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.flyway.locations=filesystem:./src/main/resources/db/migration
Spring 日志如下所示:
2022-05-22 13:21:34.094 INFO 46252 --- [ main] com.zhandos.SOLIDBankApp.Main : No active profile set, falling back to 1 default profile: "default"
2022-05-22 13:21:34.408 INFO 46252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-05-22 13:21:34.416 INFO 46252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2022-05-22 13:21:34.651 INFO 46252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-05-22 13:21:34.655 INFO 46252 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-05-22 13:21:34.655 INFO 46252 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.0.18]
2022-05-22 13:21:34.706 INFO 46252 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-05-22 13:21:34.708 INFO 46252 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 586 ms
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 8.5.11 by Redgate
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter : See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.11
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter :
2022-05-22 13:21:34.780 INFO 46252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-05-22 13:21:34.831 INFO 46252 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:d2716439-cb1c-4995-bb7a-1e672e3d7a42 user=SA
2022-05-22 13:21:34.832 INFO 46252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-05-22 13:21:34.838 INFO 46252 --- [ main] o.f.c.i.database.base.BaseDatabaseType : Database: jdbc:h2:mem:d2716439-cb1c-4995-bb7a-1e672e3d7a42 (H2 2.1)
2022-05-22 13:21:34.869 INFO 46252 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.007s)
2022-05-22 13:21:34.872 INFO 46252 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
2022-05-22 13:21:34.895 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
2022-05-22 13:21:34.898 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version "1.1 - Add transaction table"
2022-05-22 13:21:34.904 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version "1.2 - Add account table"
2022-05-22 13:21:34.908 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 2 migrations to schema "PUBLIC", now at version v1.2 (execution time 00:00.015s)
2022-05-22 13:21:34.969 INFO 46252 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-05-22 13:21:34.985 INFO 46252 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.7.Final
2022-05-22 13:21:35.031 INFO 46252 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-05-22 13:21:35.066 INFO 46252 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-05-22 13:21:35.126 INFO 46252 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-05-22 13:21:35.132 INFO 46252 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-05-22 13:21:35.169 WARN 46252 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-05-22 13:21:35.330 INFO 46252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-22 13:21:35.337 INFO 46252 --- [ main] com.zhandos.SOLIDBankApp.Main : Started Main in 1.434 seconds (JVM running for 1.771)
但是,我不希望数据库在内存中。我尝试了网络上针对类似问题的所有建议,尝试了 application.properties 中的不同组合,但这些努力都是徒劳的。似乎内存 (mem) 数据库设置为默认值且无法更改。
这样使用
spring.datasource.url:jdbc:h2:/home/ubuntu/test
您需要传递文件的绝对路径
这实际上是我犯的一个非常愚蠢的错误,将 :
替换为 =
后问题得到解决
spring.jpa.hibernate.ddl-auto = none
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.flyway.url=jdbc:h2:file:~/test
spring.flyway.user=sa
spring.flyway.password=
我正在尝试在磁盘中创建一个本地数据库并连接到它。基本上我想要 jdbc.h2.file,而不是 jdbc.h2.mem。这就是我设置 application.properties.file
的方式spring.jpa.hibernate.ddl-auto = none
spring.datasource.url:jdbc:h2:file:~/test
spring.datasource.driverClassName:org.h2.Driver
spring.datasource.username:sa
spring.datasource.password:
spring.jpa.show-sql=true
spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.flyway.url:jdbc:h2:file:~/test
spring.flyway.user:sa
spring.flyway.password:
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.flyway.locations=filesystem:./src/main/resources/db/migration
Spring 日志如下所示:
2022-05-22 13:21:34.094 INFO 46252 --- [ main] com.zhandos.SOLIDBankApp.Main : No active profile set, falling back to 1 default profile: "default"
2022-05-22 13:21:34.408 INFO 46252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-05-22 13:21:34.416 INFO 46252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2022-05-22 13:21:34.651 INFO 46252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-05-22 13:21:34.655 INFO 46252 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-05-22 13:21:34.655 INFO 46252 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.0.18]
2022-05-22 13:21:34.706 INFO 46252 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-05-22 13:21:34.708 INFO 46252 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 586 ms
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 8.5.11 by Redgate
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter : See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.11
2022-05-22 13:21:34.775 INFO 46252 --- [ main] o.f.c.internal.license.VersionPrinter :
2022-05-22 13:21:34.780 INFO 46252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-05-22 13:21:34.831 INFO 46252 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:d2716439-cb1c-4995-bb7a-1e672e3d7a42 user=SA
2022-05-22 13:21:34.832 INFO 46252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-05-22 13:21:34.838 INFO 46252 --- [ main] o.f.c.i.database.base.BaseDatabaseType : Database: jdbc:h2:mem:d2716439-cb1c-4995-bb7a-1e672e3d7a42 (H2 2.1)
2022-05-22 13:21:34.869 INFO 46252 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.007s)
2022-05-22 13:21:34.872 INFO 46252 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
2022-05-22 13:21:34.895 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
2022-05-22 13:21:34.898 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version "1.1 - Add transaction table"
2022-05-22 13:21:34.904 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version "1.2 - Add account table"
2022-05-22 13:21:34.908 INFO 46252 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 2 migrations to schema "PUBLIC", now at version v1.2 (execution time 00:00.015s)
2022-05-22 13:21:34.969 INFO 46252 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-05-22 13:21:34.985 INFO 46252 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.7.Final
2022-05-22 13:21:35.031 INFO 46252 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-05-22 13:21:35.066 INFO 46252 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-05-22 13:21:35.126 INFO 46252 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-05-22 13:21:35.132 INFO 46252 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-05-22 13:21:35.169 WARN 46252 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-05-22 13:21:35.330 INFO 46252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-22 13:21:35.337 INFO 46252 --- [ main] com.zhandos.SOLIDBankApp.Main : Started Main in 1.434 seconds (JVM running for 1.771)
但是,我不希望数据库在内存中。我尝试了网络上针对类似问题的所有建议,尝试了 application.properties 中的不同组合,但这些努力都是徒劳的。似乎内存 (mem) 数据库设置为默认值且无法更改。
这样使用
spring.datasource.url:jdbc:h2:/home/ubuntu/test
您需要传递文件的绝对路径
这实际上是我犯的一个非常愚蠢的错误,将 :
替换为 =
spring.jpa.hibernate.ddl-auto = none
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.flyway.url=jdbc:h2:file:~/test
spring.flyway.user=sa
spring.flyway.password=