📜  H2数据库-提交(1)

📅  最后修改于: 2023-12-03 14:41:40.692000             🧑  作者: Mango

H2数据库-提交

H2是一个纯Java编写的嵌入式关系型数据库,具有高速和功能强大的特点,支持多种模式,包括嵌入模式、服务器模式和混合模式,同时也支持SQL、JDBC和ODBC访问。H2数据库的提交功能是相对于事务的一种重要功能,可以将多个操作一次性提交,从而在一次连接中提高执行效率,并确保操作的原子性。

嵌入式模式

在H2数据库的嵌入式模式下,提交操作可以简单地通过Java代码实现,示例如下:

public class H2Database {

    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
        connection.setAutoCommit(false);

        try {
            // 执行操作
            PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO USER (NAME, AGE) VALUES (?, ?)");
            insertStatement.setString(1, "Alice");
            insertStatement.setInt(2, 18);
            insertStatement.executeUpdate();

            PreparedStatement updateStatement = connection.prepareStatement("UPDATE USER SET AGE = ? WHERE NAME = ?");
            updateStatement.setInt(1, 19);
            updateStatement.setString(2, "Bob");
            updateStatement.executeUpdate();

            // 提交操作
            connection.commit();
        } catch (SQLException e) {
            // 回滚操作
            connection.rollback();
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }
}

在以上示例中,首先获取H2数据库的连接,然后将自动提交模式关闭,开启事务。接着依次执行插入和更新操作,并在操作结束后对数据库进行提交操作,即将改动保存到数据库中。在操作过程中如果发生异常,将实行回滚操作,即将所有事务都撤销。

服务器模式

在H2数据库的服务器模式下,提交操作还需要与服务器进行交互,示例如下:

public class H2RemoteDatabase {

    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test", "sa", "");
        connection.setAutoCommit(false);

        try {
            // 执行操作
            PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO USER (NAME, AGE) VALUES (?, ?)");
            insertStatement.setString(1, "Alice");
            insertStatement.setInt(2, 18);
            insertStatement.executeUpdate();

            PreparedStatement updateStatement = connection.prepareStatement("UPDATE USER SET AGE = ? WHERE NAME = ?");
            updateStatement.setInt(1, 19);
            updateStatement.setString(2, "Bob");
            updateStatement.executeUpdate();

            // 提交操作
            Statement commitStatement = connection.createStatement();
            commitStatement.execute("COMMIT");

        } catch (SQLException e) {
            // 回滚操作
            connection.rollback();
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }
}

在以上示例中,首先获取H2数据库的远程连接,然后将自动提交模式关闭,开启事务。接着依次执行插入和更新操作,并在操作结束后对数据库进行提交操作,即将改动保存到数据库中。注意此处的提交操作需要使用Statement对象执行,而不是PreparedStatement对象,否则连接会自动回滚。

混合模式

在H2数据库的混合模式下,可以通过命令行和Java代码相结合的方式进行提交操作,示例如下:

public class H2MixedDatabase {

    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
        connection.setAutoCommit(false);

        try {
            // 执行操作
            PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO USER (NAME, AGE) VALUES (?, ?)");
            insertStatement.setString(1, "Alice");
            insertStatement.setInt(2, 18);
            insertStatement.executeUpdate();

            PreparedStatement updateStatement = connection.prepareStatement("UPDATE USER SET AGE = ? WHERE NAME = ?");
            updateStatement.setInt(1, 19);
            updateStatement.setString(2, "Bob");
            updateStatement.executeUpdate();

            // 提交操作
            ProcessBuilder commitBuilder = new ProcessBuilder("cmd.exe", "/c", "h2.bat -script=commit.sql");
            commitBuilder.directory(new File("D:\\Program Files (x86)\\H2\\bin"));
            commitBuilder.start().waitFor();

        } catch (SQLException | IOException | InterruptedException e) {
            // 回滚操作
            connection.rollback();
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }
}

在以上示例中,首先获取H2数据库的嵌入式连接,然后将自动提交模式关闭,开启事务。接着依次执行插入和更新操作,并在操作结束后执行脚本文件,其中脚本文件中包含了提交操作的SQL语句,即将改动保存到数据库中。如果在操作过程中发生异常,将实行回滚操作,即将所有事务都撤销。注意此处的命令行操作需要使用ProcessBuilder对象执行,在执行命令时需要指定h2.bat脚本所在的目录,并在代码中捕获异常以便进行回滚操作。

链接
  1. H2 Database Engine
  2. H2数据库-官方文档