任何想法坚持H2数据库内存模式事务? [英] Any ideas for persisting H2 Database In-Memory mode transaction?

查看:1473
本文介绍了任何想法坚持H2数据库内存模式事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内存模式H2数据库的代码运行完全正常,直到连接打开或VM正在运行。但是当连接关闭或VM关闭发生时,H2 db丢失数据。是否有其他方法在多个启动/关闭/在线/离线周期内保持数据?

The following code for H2 database with "in-memory mode" 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 ?

一种方法是通过跟踪从应用程序发出的DDL和DML以及后台同步过程来创建磁盘内存数据库副本,以检查磁盘上数据的完整性和内存。 Diskbased DML可能较慢+在每次启动时将磁盘数据复制/加载到内存的额外开销将会存在,但是仍然可以在一定程度上实现持久性。

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.

任何其他由H2提供的用于在内存模式或任何其他解决方法中的持久性问题的方式或任何其他解决方法?

Are there any other ways provided by H2 for persistence issue with in-memeory mode 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() );
        }
    }
}

感谢。

推荐答案

您可以使用持久模式大缓存。 大我的意思是使整个数据库适合内存。

You could use the persistent mode with a large cache. With 'large' I mean so that the whole database fits in memory. That way even table scans will not read from disk.

要持久化内存数据库,您可以使用 SCRIPT 命令,但您需要手动执行。

To persist an in-memory database, you could use the SCRIPT command, but you need to execute it manually.

这篇关于任何想法坚持H2数据库内存模式事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆