简体   繁体   English

当结合使用MySQL和BoneCP时,jdbc executeBatch挂起

[英]jdbc executeBatch hangs when using the combination of MySQL and BoneCP

Here is my code for reading SQL from a file and then do batch updates 这是我的代码,用于从文件读取SQL,然后进行批处理更新

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 我尝试过的数据库: HSQLDB(in-process mode)HSQLDB(memory mode)MySQL

DB Pooling I have tried: No Pooling(DriverManger) , DBCP , BoneCP 我尝试过的数据库池化: No Pooling(DriverManger)DBCPBoneCP

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); 没有conn.setAutoCommit(true);几乎所有DB和DB Pool的组合都能完美工作conn.setAutoCommit(true); that I highlighted in the code, except for one: BoneCP + MySQL . 我在代码中突出显示的内容,但以下一项除外: BoneCP + MySQL For this combination to work, I have to put that conn.setAutoCommit(true); 为了使这种组合有效,我必须将conn.setAutoCommit(true); at the end of the update() code. update()代码的末尾。 Other wise, the program would hang at the beginning of the 3rd process(2nd batchUpdate). 否则,程序将在第三个进程(第二个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 . 我的猜测是它挂起是因为它等待write lock被释放,而我的第一个batchUpdate()持有该锁定的唯一可能原因可能是因为我将连接设置为不自动提交,从而导致BoneCP不能释放write lock So I added the setAutCommit(true) and it worked. 所以我添加了setAutCommit(true)并成功了。 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. 谢谢。

BoneCP had a bug (fixed in 0.8.0-rc3) whereby autocommit wasn't set to true by default as per spec. BoneCP有一个错误(已在0.8.0-rc3中修复),根据规范,默认情况下自动提交未设置为true。

You can set config.setDefaultAutoCommit(true) to get around the issue. 您可以设置config.setDefaultAutoCommit(true)来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM