How to efficiently dump and restore a database snapshot?

197 views
Skip to first unread message

Benedikt Waldvogel

unread,
Mar 6, 2019, 4:48:03 PM3/6/19
to H2 Database
Hello,

we’re using H2 in some integration tests.
We have a rather long-running setup phase that populates the database with a basic set of data that can be used for a large variety of tests.
To speed-up the integration tests, we run the setup only once, create a snapshot and restore it before each test to keep the tests fully isolated.

We basically run "SCRIPT TO '/path/to/dump.zip'" to create the snapshot and "RUNSCRIPT FROM '/path/to/dump.zip'" to restore it.
Profiling indicates that restoring the snapshot can take a significant fraction of test time, especially if the test itself is small and runs quickly.

I’m wondering if there is a more efficient way to dump and restore the H2 database?
Is it possible to access the underlying MVStore and dump it to a file/directory and later replace the MVStore with the contents of the dump?

Any hint the points me in the right direction is appreciated.

Best regards
Benedikt Waldvogel



Noel Grandin

unread,
Mar 7, 2019, 1:15:18 AM3/7/19
to h2-da...@googlegroups.com
just make a file copy of the underlying database and copy it back for each test?

Benedikt Waldvogel

unread,
Mar 7, 2019, 2:16:44 AM3/7/19
to H2 Database
Hi Noel,

On Thursday, March 7, 2019 at 7:15:18 AM UTC+1, Noel Grandin wrote:
just make a file copy of the underlying database and copy it back for each test?

We currently have the database in-memory but I’ll give it a try to switch to on-disk, thanks.
However, I’ll need to measure the performance impact of having the DB on-disk instead of in-memory.

Do you have an idea how to achieve it when the database is in-memory?

Thanks
Benedikt

Noel Grandin

unread,
Mar 7, 2019, 2:54:16 AM3/7/19
to h2-da...@googlegroups.com
>
> Do you have an idea how to achieve it when the database is in-memory?
>

Our byte storage level backends all emulate a kind of filesystem abstraction, so in theory you could copy in-memory
files around too.

See the org.h2.store.fs.Filepath class

You'd need to do some experimentation, and you might wander into a bug, because I don't think anyone has ever done that
before :-)
Reply all
Reply to author
Forward
0 new messages