Spring的嵌入式H2数据源和DB_CLOSE_ON_EXIT
对于单元测试(如果需要,可将其称为集成测试),我已经在Spring配置中配置了嵌入式数据库,如下所示:
<jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:schema_h2.sql" />
</jdbc:embedded-database>
现在,从命令行运行测试时,它们可以正常工作,但是最后我得到一些错误(无害,但很烦人):
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]
现在,包含在异常中的提示通常很好,但是如何将此属性添加到嵌入式数据源?我是否必须扩展它,手动配置它以添加此类 “高级” 功能?
回答:
在JDBC网址jdbc:h2:〜/
test中指定参数;
DB_CLOSE_ON_EXIT =
FALSE
同样对于内存中的测试数据库,我建议您添加DB_CLOSE_DELAY=-1
,如下所示:
jdbc:h2:mem:alm;MODE=Oracle;DB_CLOSE_DELAY=-1
要添加JDBC连接URL来embedded-dababase
将其更改为:
<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 的全部内容, 来源链接: utcz.com/qa/428990.html