首页 > 解决方案 > H2 显示 DB_CLOSE DELAY 的值(由 SET DB_CLOSE_DELAY 设置)

问题描述

H2 数据库有一个以 SET 开头的命令列表,特别是 SET DB_CLOSE_DELAY。我想知道 DB_CLOSE_DELAY 的值是多少。我正在使用 JDBC。设置简单

cx.createStatement.execute("SET DB_CLOSE_DELAY 0")

但以下均不返回 DB_CLOSE_DELAY 的实际值:

cx.createStatement.executeQuery("DB_CLOSE_DELAY")
cx.createStatement.executeQuery("VALUES(@DB_CLOSE_DELAY)")
cx.createStatement.executeQuery("GET DB_CLOSE_DELAY")
cx.createStatement.executeQuery("SHOW DB_CLOSE_DELAY")

帮助将不胜感激。

标签: jdbch2in-memory-databaseh2db

解决方案


您可以访问INFORMATION_SCHEMA.SETTINGS表中的此设置和其他设置 - 例如:

String url = "jdbc:h2:mem:;DB_CLOSE_DELAY=3";
Connection conn = DriverManager.getConnection(url, "sa", "the password goes here");
Statement stmt = conn.createStatement();
        
ResultSet rs = stmt.executeQuery("SELECT * FROM INFORMATION_SCHEMA.SETTINGS where name = 'DB_CLOSE_DELAY'");
while (rs.next()) {
    System.out.println(rs.getString("name"));
    System.out.println(rs.getString("value"));
}

在这个测试中,我使用了一个未命名的内存数据库,并3在创建数据库时将延迟显式设置为秒。

打印语句的输出是:

DB_CLOSE_DELAY
3

推荐阅读