Using H2 with application server for tests

67 views
Skip to first unread message

Niklas Mehner

unread,
Jun 20, 2019, 5:32:38 AM6/20/19
to H2 Database
Hi,

We are using H2 for end-to-end tests for our JEE application.

So basically the setup is:

* start H2 (in memory) and application server
* run many testcases
* stop H2 and application server

Each testcase brings its own data and clears all tables before running.
Although H2 is pretty fast the setup of the data takes some time.

What I am thinking about is finding a way to start H2 e.g. from a file that contains some prefilled data and then only add the special data the test needs on top.
But I have not found any way to reset the database to the initial state after the test. 
Of course copying the file again and restarting H2 would work, but this is probably also slow when running very many tests.

Has anybody a similar setup and have you solved this problem somehow? Running in-memory but backed by a read-only file for example?

Thanks & best regards,
  Niklas

Noel Grandin

unread,
Jun 20, 2019, 5:37:30 AM6/20/19
to h2-da...@googlegroups.com

This has come up several times before, and the reply I gave there was that we have a FilePath abstraction which sits
between our engine and various kind of storage backends (memory, disk, etc).

It should be possible to
(a) restore a disk file to an in-memory backend, since the FilePath abstraction looks like a filesystem
(b) make a copy of a populated database from an in-memory backend to a disk file.
(c) overwrite an existing in-memory database "virtual file" from another in-memory "virtual file"

However, to my knowledge, no-one has ever done the work to actually make this happen.


Christian MICHON

unread,
Jun 20, 2019, 10:45:16 AM6/20/19
to H2 Database
any chance that using savepoint/rollback could solve your issue?

Christian MICHON

unread,
Jun 20, 2019, 10:49:05 AM6/20/19
to H2 Database
Argh... only application until a transaction is committed or rolledback. No go, forget my suggestion
Reply all
Reply to author
Forward
0 new messages