2

私はHSQLDBを使用しています。Java 関数内で、一時テーブルを作成し、データを入力しています。一時テーブルのデータで通常のテーブルを更新した直後。

関数内のコードをデバッグして実行するとSELECT、両方のテーブルでデータが実際に更新されたことがわかりますが、変更をコミットした後、テーブルは更新されなくなりました! (一時テーブルではなく、通常のテーブル)。

私が使用した一時テーブルを作成したとき:ON COMMIT PRESERVE ROWSしかし、それは役に立ちません。

なぜこれが起こっているのか誰にも分かりますか?

ありがとう!

コードは次のとおりです。

public synchronized void foo(String[] objectsToMark) throws Exception {

        String createTempTableQuery = "CREATE TEMP TABLE "+TEMP_TABLE_NAME+"(OBJECTID VARCHAR) " +
                "ON COMMIT PRESERVE ROWS";
        String dropTempTableQuery = "DROP TABLE "+TEMP_TABLE_NAME;

        String updateQueryString = "update " + TABlE_A + " set " +TABlE_A+".X = 'N' where "+TABlE_A+".RESULT_ID in (select "+TABlE_A+".RESULT_ID FROM "+TABlE_A
                        +" inner join "+TABLE_B+" on "+TABLE_B+".OBJECTID = "+TABlE_A+".OBJECTID"
                        +" inner join TEMP_TABLE on "+TABLE_B+".OBJECTID = TEMP_TABLE.OBJECTID"
                        +" where "+TABlE_A+".IS_X = 'Y')";

        String insertObjectIDs = "INSERT INTO " + TEMP_TABLE_NAME + " (OBJECTID) VALUES (?)";
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement psInsertObjectIDs = null;
        try {
            conn = getConnection(getClass().getName() + "");
            stmt = conn.createStatement();


            stmt.execute(createTempTableQuery);

            // insert object's IDs into table
            psInsertObjectIDs = conn.prepareStatement(insertObjectIDs);

            for (String id : objectsToMark) {
                int i = 0;
                psInsertObjectIDs.setString(++i, id);
                psInsertObjectIDs.addBatch();
            }
            psInsertObjectIDs.executeBatch();

            stmt.execute(updateQueryString);

        } catch (Exception e) {
        } finally {
            // close resources
            close(stmt);
            stmt = null;
            // drop the temporary tables
            if (conn != null) {
                try {
                    stmt = conn.createStatement();
                    stmt.execute(dropTempTableQuery);
                } catch (SQLException e) {
                }
                close(stmt);
            }

            close(psInsertObjectIDs);
            closeConnection(conn);
        }
    }

ここにはコミットはありませんが、自動コミットを実行するように接続が構成されていることに注意してください。

4

1 に答える 1

1

As the code works when you debug, the problem must be outside this code.

If you are running this code as an isolated test on a file: database, and you have not specified a write delay or shutdown option in the jdbc connection url, then the engine does not get enough time to write the data to disk.

The default settings of HSQLDB are for production, where a SHUTDOWN is issued by the application program. For tests, use either of the properties below.

This one writes the changes after each commit or auto-commit, therefore slows down production databases:

jdbc:hsqldb:file:mydb;hsqldb.write_delay=false

This one shuts down the database when the only remaining connection is closed:

jdbc:hsqldb:file:mydb;shutdown=true

As this issue comes up quite often, future versions of HSQLDB from 2.2.9 will always write the data to disk when the only remaining connection is closed.

于 2012-04-28T19:37:35.477 に答える