如何配置spring-boot以使用基于文件的H2数据库 [英] How to configure spring-boot to use file based H2 database
问题描述
我已经成功创建了一个使用内存中的H2嵌入式数据库的Spring Boot应用程序.我现在想将其更改为将继续存在的基于文件的版本.
I have successfully created a spring boot application that uses the H2 embedded database in-memory. I would now like to change this to a file based version that will persist.
我尝试只更改我的application.properties
文件中的spring.datasource.*
属性,它们看起来像这样:
I have tried just changing the spring.datasource.*
properties in my application.properties
file and they look something like this:
spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=org.h2.Driver`
spring boot似乎忽略了这些设置,因为它的启动过程如下:
It seems like spring boot just ignores these settings because it just starts as follows:
o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
我的pom.xml
包含以下与这篇文章可能相关的依赖项:
My pom.xml
contains the following dependencies that may be relevant to this post:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
从文档和许多帖子中我的理解是,配置应该可以正常工作,但是对我来说没有任何运气.为了防止某些基本错误,我尝试并检查了以下内容:
My understanding from the documentation and a number of posts is that the configuration should just work but no luck for me. Just to prevent some of the basic errors I have tried and checked the following:
- 我的应用程序属性在类路径中:
- 我试图在注释
@EnableAutoConfiguration
中排除自动配置
- 我尝试注入带有注释
@Primary
,@ConfigurationProperties(prefix = "spring.datasource")
组合的dataSource
bean,并使用DataSourceBuilder
编程设置属性.这会导致其他与类型为null
的错误.
- My application properties is in the classspath:
- I have tried to exclude the auto configuration in annotation
@EnableAutoConfiguration
- I have tried to inject a
dataSource
bean with combinations of annotation@Primary
,@ConfigurationProperties(prefix = "spring.datasource")
and setting the properties programmatically withDataSourceBuilder
. This causes other errors related to the type beingnull
.
似乎我缺少一个关键概念或某些东西.谁能帮忙.
Seems like I am missing a key concept or something. Can anyone help.
更新1:从我的自动配置报告中提取:
UPDATE 1: Extract from my auto configuration report:
Positive matches:
-----------------
DataSourceAutoConfiguration matched
- @ConditionalOnClass classes found: javax.sql.DataSource,org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)
DataSourceAutoConfiguration.DataSourceInitializerConfiguration matched
- @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.EmbeddedConfiguration matched
- embedded database H2 detected (DataSourceAutoConfiguration.EmbeddedDataSourceCondition)
- @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration matched
- existing auto database detected (DataSourceAutoConfiguration.DataSourceAvailableCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#jdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration#namedParameterJdbcTemplate matched
- @ConditionalOnMissingBean (types: org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
DataSourceTransactionManagerAutoConfiguration matched
- @ConditionalOnClass classes found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfiguration matched
- @ConditionalOnMissingBean (types: org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; SearchStrategy: all) found no beans (OnBeanCondition)
H2ConsoleAutoConfiguration matched
- @ConditionalOnClass classes found: org.h2.server.web.WebServlet (OnClassCondition)
- found web application StandardServletEnvironment (OnWebApplicationCondition)
- matched (OnPropertyCondition)
HibernateJpaAutoConfiguration matched
- @ConditionalOnClass classes found: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean,org.springframework.transaction.annotation.EnableTransactionManagement,javax.persistence.EntityManager (OnClassCondition)
- found HibernateEntityManager class (HibernateJpaAutoConfiguration.HibernateEntityManagerCondition)
Negative matches:
-----------------
DataSourceAutoConfiguration.NonEmbeddedConfiguration did not match
- missing supported DataSource (DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition)
`
更新2:添加了执行器,并查看了端点/configprops
.这里有趣的是,我的配置已被使用并且数据库存在,但是当应用程序运行时,它不使用此dataSource
.
UPDATE 2: added actuator and looked at endpoint /configprops
. What is interesting here is that my config has been taken and the database exists but when the application runs it does not use this dataSource
.
"spring.datasource.CONFIGURATION_PROPERTIES":
{"prefix":"spring.datasource",
"properties":{
"schema":null,
"data":null,
"xa":{"dataSourceClassName":null,
"properties":{}
},
"type":null,
"separator":";",
"url":"jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE",
"platform":"all",
"continueOnError":false,
"jndiName":null,
"sqlScriptEncoding":null,
"password":"******",
"name":"testdb",
"driverClassName":"org.h2.Driver",
"initialize":true,
"username":"test"
}
}
推荐答案
请参考 http://www.h2database.com/html/cheatSheet.html
我想jdbc.url可能有问题,请像这样更改它:
I guess it might be problem with the jdbc.url, change it like this:
# from:
spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
# to:
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
这篇关于如何配置spring-boot以使用基于文件的H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!