[英]jdbc executeBatch hangs when using the combination of MySQL and BoneCP
這是我的代碼,用於從文件讀取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();
}
}
}
我嘗試過的數據庫: HSQLDB(in-process mode)
, HSQLDB(memory mode)
, MySQL
我嘗試過的數據庫池化: No Pooling(DriverManger)
, DBCP
, BoneCP
我的應用程序按以下順序運行:
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
沒有conn.setAutoCommit(true);
幾乎所有DB和DB Pool的組合都能完美工作conn.setAutoCommit(true);
我在代碼中突出顯示的內容,但以下一項除外: BoneCP
+ MySQL
。 為了使這種組合有效,我必須將conn.setAutoCommit(true);
在update()
代碼的末尾。 否則,程序將在第三個進程(第二個batchUpdate)的開始處掛起。
我的猜測是它掛起是因為它等待write lock
被釋放,而我的第一個batchUpdate()
持有該鎖定的唯一可能原因可能是因為我將連接設置為不自動提交,從而導致BoneCP不能釋放write lock
。 所以我添加了setAutCommit(true)
並成功了。 該程序不再掛起。
所以,我只想問一下,我的猜測對嗎? 還是因為其他原因? 是否應該將其視為錯誤,因為沒有其他組合會產生這種奇怪的行為? 謝謝。
BoneCP有一個錯誤(已在0.8.0-rc3中修復),根據規范,默認情況下自動提交未設置為true。
您可以設置config.setDefaultAutoCommit(true)來解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.