在PostgreSQL中,我面臨競爭條件。系統中的單獨進程可能會刪除我的表和模式。如果模式和表存在,則使用成語,則讀取內容因此一般不起作用,因爲表可能在語句中間不存在。PostgreSQL information_schema.tables和TRANSACTION ISOLATION LEVEL
我不明白的一件事是爲什麼SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
沒有幫助。我想我可能期望在我的交易中對模式和表格有一致的看法,但我不這樣認爲。下面是我的Java代碼:
pgConnection = DriverManager.getConnection(/* ... */);
pgConnection.setAutoCommit(false);
PreparedStatement statement = pgConnection.prepareStatement(
"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;");
statement.execute();
statement = pgConnection.prepareStatement(
"SELECT ('myschema','config') IN " +
"(SELECT table_schema,table_name FROM information_schema.tables);");
ResultSet result = statement.executeQuery();
result.next();
if(result.getBoolean(1)) {
statement = pgConnection.prepareStatement("SELECT key,value FROM myschema.config;");
result = statement.executeQuery(); // here I'm often getting an exception
/* ... */
}
我得到的例外是:
org.postgresql.util.PSQLException: ERROR: relation "myschema.config" does not exist
這怎麼可能?我認爲ISOLATION LEVEL SERIALIZABLE
將保護我免受這種情況。那是因爲刪除模式過於具體操作以保持隔離?或者我在做一些根本性錯誤?
並非所有的數據庫都認爲DDL是「在」一個事務中。 (我不確定pg的立場是什麼) – user2864740 2015-03-05 22:04:53