Frage

Here is my code for reading SQL from a file and then do batch updates

public void update(Connection conn, File f) {
    Statement st = null;
    Scanner sc = null;
    try {
        conn.setAutoCommit(false);
        st = conn.createStatement(); 

        //Scann the file line by line and addBatch each line...

        st.executeBatch();
        conn.commit();

        /************************/
        conn.setAutoCommit(true);
        /************************/

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (st != null) { st.close();}
            if (conn != null) { conn.close(); }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Database I have tried: HSQLDB(in-process mode), HSQLDB(memory mode), MySQL

DB Pooling I have tried:No Pooling(DriverManger), DBCP, BoneCP

My application runs in the following sequence:

1. one batchUpdate() to execute many "create table" and "insert" SQL statement
2. many executeQuery() to execute many "select" SQL statement
3. one batchUpdate() to execute many "drop table" statement

Almost all combinations of DB and DB Pool works perfectly without the conn.setAutoCommit(true); that I highlighted in the code, except for one: BoneCP+MySQL. For this combination to work, I have to put that conn.setAutoCommit(true); at the end of the update() code. Other wise, the program would hang at the beginning of the 3rd process(2nd batchUpdate).

My guess was that it hangs because it waits for the write lock to be released, and the only possible reason for my 1st batchUpdate() to hold the lock may be because I set the connection to not commit automatically and that has caused BoneCP to not release the write lock. So I added the setAutCommit(true) and it worked. The program doesn't hang anymore.

So, I just want to ask, was my guess right? or is it because of something else? Should it be considered as a bug, since no other combination exerts this kind of odd behavior? Thanks.

War es hilfreich?

Lösung

BoneCP had a bug (fixed in 0.8.0-rc3) whereby autocommit wasn't set to true by default as per spec.

You can set config.setDefaultAutoCommit(true) to get around the issue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top