Saving in-memory H2 database to disk

后端 未结 2 1914
灰色年华
灰色年华 2021-01-01 16:28

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

相关标签:
2条回答
  • 2021-01-01 17:03

    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.

    0 讨论(0)
  • 2021-01-01 17:15

    As I found out it is possible to use H2 virtual FS that is much faster then default FS. So, I do this way:

    //to save dump
    connection.prepareStatement("SCRIPT TO 'memFS:data.sql'").execute();
    //to load dump
    connection.prepareStatement("RUNSCRIPT FROM 'memFS:data.sql'").execute();
    
    0 讨论(0)
提交回复
热议问题