title: Any ideas for persisting H2 Database In-Memory mode transaction?
body: <p>The following code for H2 database with &quot;in-memory mode&quot; runs perfectly fine until connection is open or VM is running. But H2 db loses data when connection is closed or when VM shutdown takes place. Is there any other way to persist data across multiple startup-shutdown/online-offline cycles ?</p>
  <p>One way would be to create diskbased replica of in-memory database by tracking DDLs and DMLs issued from application and a sync process in background that checks for integrity of data on disk and memory. Diskbased DMLs might be slower + additional overhead of copying/loading disk data to memory on each startup, will be there but still persistence will be achievable to some extent.</p>
  <p>Are there any other ways provided by H2 for persistence issue with in-memeory mode or any other workarounds ?</p>
  <pre class="lang-java prettyprint-override"><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  public class H2InMemoryModeTest {        
  
  public static void main(String[] args)
      {
          try
          {
              Class.forName(&quot;org.h2.Driver&quot;);
              
             DriverManager.getConnection(&quot;jdbc:h2:mem:test;DB_CLOSE_DELAY=-1&quot;,&quot;sa&quot;,&quot;sa&quot;);
              
              Statement stmt = con.createStatement();
              
              //stmt.executeUpdate( &quot;DROP TABLE table1&quot; );
              stmt.executeUpdate( &quot;CREATE TABLE table1 ( user varchar(50) )&quot; );
              stmt.executeUpdate( &quot;INSERT INTO table1 ( user ) VALUES ( 'John' )&quot; );
              stmt.executeUpdate( &quot;INSERT INTO table1 ( user ) VALUES ( 'Smith' )&quot; );
              ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM table1&quot;);
              
              while( rs.next() )
              {
                  String name = rs.getString(&quot;user&quot;);
                  System.out.println( name );
              }
              stmt.close();
              con.close();
          }
          catch( Exception e )
          {
              System.out.println( e.getMessage() );
          }
      }
  }
  </code></pre>
  <p>Kindly Help. Thanks.</p>