我已经成功创建了一个在内存中使用H2嵌入式数据库的spring启动应用程序.我现在想将其更改为基于文件的版本,该版本将持续存在.
我尝试过只更改文件中的spring.datasource.*属性application.properties,它们看起来像这样:
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`
Run Code Online (Sandbox Code Playgroud)
看起来像spring boot只是忽略了这些设置,因为它只是如下所示:
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'
Run Code Online (Sandbox Code Playgroud)
我pom.xml包含以下可能与此帖子相关的依赖项:
<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>
Run Code Online (Sandbox Code Playgroud)
我从文档和一些帖子中了解到,配置应该正常工作,但对我来说没有运气.只是为了防止我尝试过的一些基本错误并检查以下内容:
@EnableAutoConfigurationdataSource带有注释组合的bean @Primary,@ConfigurationProperties(prefix = "spring.datasource")并以编程方式设置属性DataSourceBuilder.这会导致与类型相关的其他错误null.似乎我错过了一个关键概念或其他东西.谁能帮忙.
更新1:从我的自动配置报告中提取:
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)
Run Code Online (Sandbox Code Playgroud)
`
更新2:添加执行器并查看端点/configprops.这里有趣的是我的配置已经被采用并且数据库存在但是当应用程序运行时它不使用它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"
}
}
Run Code Online (Sandbox Code Playgroud)
Avi*_*ash 33
我正在添加这个答案,以避免混淆和进一步的研究.
实际上我有同样的问题,没有一个答案完全适合我,而不是一些答案的混合工作.
以下是在spring spring中持久保存H2 db所需的最小配置.
application.xml中
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)
这spring.jpa.hibernate.ddl-auto=update就是诀窍.不需要其他任何东西.
无需添加spring-boot-starter-jdbcpom.xml
无需在jdbc url中添加任何参数.
len*_*liu 32
请参阅http://www.h2database.com/html/cheatSheet.html
我想这可能是jdbc.url的问题,改变它如下:
# 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
Run Code Online (Sandbox Code Playgroud)
使用 application.properties 上的以下设置,即使在关闭并重新启动 SpringBoot 之后,甚至在重新启动计算机之后,我也设法保持数据持久化。
spring.datasource.name=japodb
spring.datasource.initialize=false
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:file:~/japodb;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
Run Code Online (Sandbox Code Playgroud)
不要在 VM 退出时关闭数据库,是的,但如果它已经存在,也不要创建新数据库。
jdbc:h2:<url>;IFEXISTS=TRUE
spring.jpa.hibernate.ddl-auto = update
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60457 次 |
| 最近记录: |