Spring’s embedded H2 datasource and DB_CLOSE_ON_EXIT
Asked Answered
A

3

44

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]

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?

Accessible answered 25/3, 2013 at 11:24 Comment(0)
T
72

Specify parameter in JDBC url jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE

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

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>
Therese answered 25/3, 2013 at 12:15 Comment(4)
I found this much in the H2 documentation. You did not say how to add that to <jdbc:embedded-database />.Accessible
Just to be clear, the updated answer is stating that it cannot be used with embedded-dababase? You need to use a datasource bean and initialize-database?Moody
From my understanding, mem in the url makes the h2 embeddedMariano
Is it something similar to #30806653 ?Bakelite
B
2

I had the same issue as Michael Piefel's one and tried to implement the solution that Michail Nikolaev explained. but it did not work, somehow spring-batch, then, where are the metadata JOB_* tables are. So, as the version of spring-jdbc used by my application is 3.0.5 and increasing the spring-framework one enters in conflict with dwr (i use it in my app) it's a geo localization based on spring, dwr and gmaps api. I downloaded the spring-jdbc 4.0.3 release and get from it the H2EmbeddedDatabaseConfigurer.class who has DB_CLOSE_ON_EXIT=FALSE by default and replace with it the one on the spring-jdbc 3.0.5 Release and deploy-it in the war file and it works, the shutdown of the VM didn't provoke the closing of the in-memory database.

Hope this unusual solution helps if other people like me wouldn't be able to implement the other solution.

Buster answered 27/5, 2016 at 9:43 Comment(0)
R
0

I had the same problem, but it was because I forgot to add the annotation @Entity on one of my entities. I add it and it work now ! I hope this helps someone.

Raina answered 23/1, 2020 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.