【发布时间】:2016-12-24 03:16:03
【问题描述】:
尝试使用 EmbeddedDatabase H2 测试我的代码时遇到奇怪的异常:
java.lang.RuntimeException:
java.lang.RuntimeException: org.h2.jdbc.JdbcBatchUpdateException: Timeout trying to lock table "CO_SCENARIO_1"; SQL statement:
UPDATE CO_SCENARIO_1 SET VALUE = ? WHERE ATTRIBUTE = ? AND MODIFIER = ? [50200-156]
at de.telekom.skses.test.dao.DatabaseFileConverterTest.fromFile(DatabaseFileConverterTest.java:99)
这是我要测试的代码的一部分:
case "overriddenVariables.txt": {
try (Scanner sc = new Scanner(bOut.toString())) {
try (Connection con = dataSource.getConnection()) {
String aQuery = "UPDATE $tableName SET VALUE = ? WHERE ATTRIBUTE = ? AND MODIFIER = ?";
String line = "";
if (sc.hasNext()) {
line = sc.nextLine();
}
while (sc.hasNext()) {
if (line.startsWith("+")) {
String setting = line.substring(1);
try (PreparedStatement ps = con.prepareStatement(aQuery.replace("$tableName", setting))) {
while (sc.hasNext() && !(line = sc.nextLine()).startsWith("+")) {
int del1 = line.indexOf(":");
int del2 = line.indexOf(':', del1 + 1);
String attr = line.substring(0, del1);
String modifier = line.substring(del1 + 1, del2);
String value = line.substring(del2 + 1, line.length());
Clob myClob = con.createClob();
myClob.setString(1, value);
ps.setClob(1, myClob);
ps.setString(2, attr);
ps.setString(3, modifier);
ps.addBatch();
}
ps.executeBatch();
}
}
}
} catch (SQLException e) {
logger.error("Error", e);
throw new RuntimeException(e);
}
} catch (Exception ex) {
logger.error("Error", ex);
throw new RuntimeException(ex);
}
break;
}
StackOverflow 说我应该设置锁定超时,但我没有找到如何为 EmbeddedDatabase 设置它。此外,在以前的版本中,我为每个查询打开了新的 PreparedStatement,并为每个 PreparedStatement 打开了新的连接,一切正常,我不明白为什么现在不行。你能解释一下我必须做什么才能让它再次工作吗?
抱歉,如果有什么不正确的地方,我是 Java EE 的新手。
【问题讨论】: