【发布时间】:2014-06-29 11:31:33
【问题描述】:
我有一个应用程序,我在其中使用 H2 临时表来存储大型缓存。但是当我关闭所有连接并关闭数据库时,我的表的序列没有被删除。过了一会儿,我有很多随机名称的泄露序列。
问题:
不删除临时表的序列是设计使然?
我怎样才能确定任何表都没有使用具体序列,以便我可以删除该序列?
这里是简单的junit4测试:
import org.junit.Test;
import java.io.File;
import java.sql.*;
import static org.junit.Assert.assertEquals;
public class H2Test {
String databaseUrl = "jdbc:h2:./SST;";
String userName = "sa";
String password = "";
@Test
public void tempTableTest() throws Exception {
Class.forName("org.h2.Driver");
File databaseFile = new File("./SST.mv.db");
if(databaseFile.exists()){
databaseFile.delete();
}
assertCount("SELECT count(*) FROM INFORMATION_SCHEMA.SEQUENCES", 0);
assertCount("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES where table_name = 'SST'", 0);
try (Connection connection = DriverManager.getConnection(databaseUrl, userName, password)) {
execute(connection, "CREATE CACHED LOCAL TEMPORARY TABLE SST ( id BIGINT IDENTITY, value VARCHAR(2048) NULL)");
execute(connection, "INSERT INTO sst(value) VALUES('some value')");
execute(connection, "SHUTDOWN");
}
assertCount("SELECT count(*) FROM INFORMATION_SCHEMA.TABLES where table_name = 'SST'", 0);
assertCount("SELECT count(*) FROM INFORMATION_SCHEMA.SEQUENCES", 0);
}
private void assertCount(String query, int count) throws SQLException {
try (Connection connection = DriverManager.getConnection(databaseUrl, userName, password) ) {
try (Statement st = connection.createStatement()) {
ResultSet resultSet = st.executeQuery(query);
resultSet.next();
assertEquals(count, resultSet.getInt(1));
execute(connection, "SHUTDOWN");
}
}
}
public void execute(Connection connection, String statement) throws SQLException {
try (Statement st = connection.createStatement()) {
st.execute(statement);
}
}
}
【问题讨论】: