2016-06-19 51 views
23

我已经成功创建了一个使用内存中的H2嵌入式数据库的spring引导应用程序。现在我想将其更改为将保留的基于文件的版本。如何配置spring-boot以使用基于H2文件的数据库

我试图只是改变了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

好像春天的启动只是忽略这些设置,因为它只是开始如下:

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包含以下依赖性可能是相关的这篇文章:

<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> 

我从文档和一些帖子的理解是,配置应该只是工作,但对我来说没有运气。只是为了防止一些我已经尝试了基本的错误,并检查以下内容:

  1. 我的应用特性是在classspath:
  2. 我试图排除自动配置的注释@EnableAutoConfiguration
  3. 我有尝试使用注释@Primary,@ConfigurationProperties(prefix =“spring.datasource”)以及使用DataSourceBuilder以编程方式设置属性的组合注入一个dataSource bean。这会导致与该类型相关的其他错误为空。

好像我缺少一个关键概念或什么的。任何人都可以帮忙

更新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) 

`

更新2:添加驱动器,看着端点/ configprops。这里有趣的是我的配置已经被使用,并且数据库存在,但是当应用程序运行时它不使用这个数据源。

"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" 
     } 
    } 

回答

15

参考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 
+0

谢谢你,但配置实际上创建了一个正确的url(如配置见上面的UPDATE 2)运行的数据库实例。问题是应用程序没有使用它。它似乎是使用默认的'EmbeddedDatabase' – bitboy

+4

您是否将spring-boot-starter-jdbc添加到pom.xml中?这里是一个示例项目:https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-flyway,迁移迁移数据库和h2database通过文件模式嵌入数据库。 – lenicliu

+3

将spring-boot-starter-jdbc添加到pom.xml中!谢谢。很高兴知道为什么这个修复它。 – bitboy

-2

创建一个文件.h2.server.properties在你的类路径,并把下面的东西,然后再试一次。您可以在资源文件夹中创建此文件。

#H2 Server Properties 
0=H2 File|org.h2.Driver|jdbc\:h2\:file\:~/test;DB_CLOSE_ON_EXIT=FALSE 

# Enable if you want other applications to connect 
#webAllowOthers=true 
#webPort=8082 
#webSSL=false 
+0

我试着使用这两个文件名**。h2.server.properties **和** h2.server.properties **以上内容的建议。没有影响。 – bitboy

+0

查看了h2文档,并且此文件用于配置控制台。我没有问题连接到控制台或正在创建的数据库表或正在存储的数据。问题是应用程序用于连接到数据库的url不受application.properties配置的影响。 – bitboy

0

刚刚生成与几个依赖h2, JPA, web, devtools, actuator start.spring.io一个崭新的春天启动的项目。在添加一个简单的Entity和Spring Data存储库之后,默认情况下数据库确实在内存中创建。

添加以下到我的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 

时devtools启用http://localhost:8080/h2-console/我甚至可以用H2控制台连接到它。

下一个逻辑步骤是访问http://localhost:8080/autoconfig端点并检查自动配置状态。

在我的情况,以下是positiveMatches

DataSourceAutoConfiguration.NonEmbeddedConfiguration: [ 
{ 
    condition: "DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition", 
    message: "supported DataSource class found" 
}, 
{ 
    condition: "OnBeanCondition", 
    message: "@ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans" 
} 
], 

及以下的negativeMatches

DataSourceAutoConfiguration.EmbeddedConfiguration: [ 
{ 
    condition: "DataSourceAutoConfiguration.EmbeddedDataSourceCondition", 
    message: "existing non-embedded database detected" 
} 
], 

你能尝试以下,并检查这些自动配置报告?

+0

嗨,布赖恩,我的是你的反向:正面匹配包括'DataSourceAutoConfiguration.EmbeddedConfiguration',否定匹配包括'DataSourceAutoConfiguration.NonEmbeddedConfiguration'。我并不担心数据库是嵌入式的,但我担心我的数据存储在默认的嵌入式内存数据库中。我希望数据存储在我配置的基于文件的URL上。 – bitboy

1

使用下面的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; 

不要关闭数据库时,VM退出,是的,但还没有做出新的数据库,如果它已经存在。

jdbc:h2:<url>;IFEXISTS=TRUE 

spring.jpa.hibernate.ddl-auto = update 
+0

嗨@guntarion,请参阅以上lenicliu的建议。该问题已通过将spring-boot-starter-jdbc添加到工作的pom.xml中解决。 – bitboy

+0

是的,我注意到了这个建议,我试过了。但它不起作用。 – guntarion

+0

这是“spring.jpa.hibernate.ddl-auto = update”,为我做了诡计。尝试所有其他提示在这里。 –

4

我在添加这个答案以避免混淆和进一步的研究。

其实我有同样的问题,没有答案完全为我工作,而不是混合一些答案的工作。

下面是在弹簧启动时保持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 

这里spring.jpa.hibernate.ddl-auto=update的伎俩。没有其他要求。

无需添加spring-boot-starter-jdbc在pom.xml中

无需添加任何参数JDBC URL。

相关问题