Spring的嵌入式H2数据源和DB_CLOSE_ON_EXIT [英] Spring’s embedded H2 datasource and DB_CLOSE_ON_EXIT
问题描述
对于单元测试(如果需要,可以调用它们进行集成测试)我在Spring配置中配置了一个嵌入式数据库,如下所示:
For unit tests (call them integration tests if you want) I have configured an embedded database in my Spring config like so:
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema_h2.sql" />
</jdbc:embedded-database>
现在,从命令行运行测试时,它们工作正常,但是我遇到了一些错误结束(无害,但有刺激性):
Now, when running the tests from the command line, they work fine, but I get some errors at the end (harmless, but irritating):
WARN 2013-03-25 12:20:22,656 [Thread-9] o.s.j.d.e.H2EmbeddedDatabaseConfigurer 'Could not shutdown embedded database'
org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-170]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.170.jar:1.3.170]
...
at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean.destroy(EmbeddedDatabaseFactoryBean.java:65) [spring-jdbc-3.2.1.RELEASE.jar:3.2.1.RELEASE]
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:238) [spring-beans-3.2.1.RELEASE.jar:3.2.1.RELEASE]
现在包含在e中的提示xception一般很好,但是如何将此属性添加到嵌入式数据源?我是否必须扩展它,手动配置它可以说,添加这样的'高级'功能?
Now the tip contained in the exception is fine in general, but how do I add this attribute to the embedded datasource? Do I have to expand it, configure it by hand so to speak, to add such ‘advanced’ features?
推荐答案
在JDBC URL中指定参数 jdbc:h2:〜/ test; DB_CLOSE_ON_EXIT = FALSE
Specify parameter in JDBC url jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
同样对于内存测试数据库,我建议你添加 DB_CLOSE_DELAY = -1
,就像这个:
Also for in-memory test database I suggest you to add DB_CLOSE_DELAY=-1
, like this:
jdbc:h2:mem:alm;MODE=Oracle;DB_CLOSE_DELAY=-1
要将JDBC连接URL添加到 embedded-dababase
,请将其更改为:
To add JDBC connection url to embedded-dababase
change it to:
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="classpath:schema_h2.sql" />
</jdbc:initialize-database>
这篇关于Spring的嵌入式H2数据源和DB_CLOSE_ON_EXIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!