Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving in-memory H2 database to disk

How can I save/load full embedded h2 in-memory database to some file or directory in binary mode for faster loading.

I want to use this for caching data so I don't have to run all the lines of create table/insert clauses every time.

like image 912
mikkom Avatar asked Mar 07 '12 11:03

mikkom


People also ask

Where does H2 database store data?

H2 can be configured to run as an in-memory database, but it can also be persistent, e.g., its data will be stored on disk.

How do I persist data in H2 database?

If we want to persist the data in the H2 database, we should store data in a file. To achieve the same, we need to change the datasource URL property. In the above property, the sampledata is a file name.

Is H2 an in-memory database?

H2 is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk.

How do I access H2 in-memory database?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.


1 Answers

Instead of using an in-memory database, you could use a regular (persisted) database. You can still use in-memory tables even then (create memory table).

The easiest way to persist a completely in-memory database to disk is to use the SCRIPT TO 'fileName' SQL statement. This will create an SQL script. The data is stored in text form, which doesn't sound like the most efficient solution. However usually the bottleneck is the disk anyway and not formatting / parsing the text.

Another option is to create another database, link the tables with the in-memory database (using create linked table or the link_schema function), and then use create table as select to persist the tables.

like image 173
Thomas Mueller Avatar answered Oct 20 '22 06:10

Thomas Mueller