Any ideas for saving a transaction in H2 Database mode?

The following code for an in-memory H2 database works fine until the connection is opened or the VM is started. But H2 db loses data when the connection is closed or when the VM shuts down. Is there another way to save data in multiple start-off / online-off cycles?

One way is to create a replica of the database in a disk-based database by tracking the DDL and DML released from the application, and the background synchronization process, which checks the integrity of the data on disk and in memory. Disk DML files can be slower + additional overhead for copying / loading disk data into memory each time it starts, it will, but until now persistence will be achievable to some extent.

Are there any other methods provided by H2 for the memmory save problem or any other workarounds?

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("org.h2.Driver"); DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1","sa","sa"); Statement stmt = con.createStatement(); //stmt.executeUpdate( "DROP TABLE table1" ); stmt.executeUpdate( "CREATE TABLE table1 ( user varchar(50) )" ); stmt.executeUpdate( "INSERT INTO table1 ( user ) VALUES ( 'John' )" ); stmt.executeUpdate( "INSERT INTO table1 ( user ) VALUES ( 'Smith' )" ); ResultSet rs = stmt.executeQuery("SELECT * FROM table1"); while( rs.next() ) { String name = rs.getString("user"); System.out.println( name ); } stmt.close(); con.close(); } catch( Exception e ) { System.out.println( e.getMessage() ); } } } 

Please help. Thanks.

+4
source share
2 answers

You can use persistent mode with a large cache . With "big", I mean that the entire database fits into memory. Thus, even a table scan will not be read from disk.

To save the database in memory, you can use the SCRIPT command, but you need to execute it manually.

+3
source

Please change spring.datasource.url = JDBC: h2: ~ / test; MVCC = true; DB_CLOSE_DELAY = -1; MODE = Oracle will create a test data file in the local system C: \ Users \ yourname .. here the test.mv file will create

-one
source

Source: https://habr.com/ru/post/1496348/


All Articles