Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

h2 database test maven

Testing java application using H2 database 1.3.169

I'm testing my app in two different ways.

  1. In-memory database is created in a separate process from the application. I start H2 console and create database from scripts. All tests pass.
  2. In-memory database is created in the same process with the application when application start. All tests pass in Intellij IDEA 11.3. Using Maven 2 for testing some tests fail(create, update, delete for the one entity, expected dataset does not match with result). It seems as database wasn't updated. It doesn't happen every time, sometime build is successful. All tests are verified, all works on Oracle and DB2.

What could be the cause of the problem?

like image 880
shprotova Avatar asked Dec 27 '12 08:12

shprotova


Video Answer


1 Answers

THis situation occurs when you configure connection to your in-memory H2 DB by default, for example: dbc:h2:mem:test

In that case DB exist until he has at least one active connection. When last connection closed, DB also closed. Depends of what you use/re-use connection (maybe pooling) and running test delays, you can get racing conditions and get unexpected results.

It can be fixed with connection parameter DB_CLOSE_DELAY=-1, what mean db was closed only when VM died.

This is helphul link http://www.h2database.com/html/features.html#closing_a_database

like image 156
user1516873 Avatar answered Sep 23 '22 01:09

user1516873