[英]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.