Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a H2 database into memory?

Tags:

h2

I have written a set of unit tests using H2 in embedded mode. Whatever changes tests make to DB stay there.

I know that the recommended approach is to create a blank in-memory database and create the schema when opening the connection.

However I am looking for an alternative approach. I would like to -

  1. Initialize an in memory database with an embedded database file.
  2. Or use embedded db in a way that all the changes are discarded as soon as the connection is closed.

How can I achieve this?

like image 415
Kshitiz Sharma Avatar asked Oct 21 '25 03:10

Kshitiz Sharma


1 Answers

What I do in cases similar to this is to write the SQL script that creates the database and populates the tables. Then the application applies a database migration using Flyway DB.

Other possibilities are to create the database and load the tables from CSV files. The other would be to create the database with a different application and create a file with the SCRIPT command to create a backup. Your main application would have to run the RUNSCRIPT command to restore the database.

like image 142
jschiavon Avatar answered Oct 25 '25 09:10

jschiavon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!