提交 e96c9031 authored 作者: Thomas Mueller's avatar Thomas Mueller

Batch update exceptions now include the root cause and all chained exceptions (getNextException).

上级 9dcb6c46
......@@ -23,6 +23,7 @@ public class JdbcBatchUpdateException extends BatchUpdateException {
*/
JdbcBatchUpdateException(SQLException next, int[] updateCounts) {
super(next.getMessage(), next.getSQLState(), next.getErrorCode(), updateCounts);
setNextException(next);
}
/**
......
......@@ -1091,14 +1091,13 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
try {
result[i] = executeUpdateInternal();
} catch (Exception re) {
SQLException e = DbException.toSQLException(re);
SQLException e = logAndConvert(re);
if (next == null) {
next = e;
} else {
e.setNextException(next);
next = e;
}
logAndConvert(e);
//## Java 1.4 begin ##
result[i] = Statement.EXECUTE_FAILED;
//## Java 1.4 end ##
......@@ -1108,7 +1107,6 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
batchParameters = null;
if (error) {
JdbcBatchUpdateException e = new JdbcBatchUpdateException(next, result);
e.setNextException(next);
throw e;
}
return result;
......
......@@ -6,7 +6,6 @@
*/
package org.h2.jdbc;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
......@@ -627,12 +626,19 @@ public class JdbcStatement extends TraceObject implements Statement {
int size = batchCommands.size();
int[] result = new int[size];
boolean error = false;
SQLException next = null;
for (int i = 0; i < size; i++) {
String sql = batchCommands.get(i);
try {
result[i] = executeUpdateInternal(sql);
} catch (Exception e) {
logAndConvert(e);
} catch (Exception re) {
SQLException e = logAndConvert(re);
if (next == null) {
next = e;
} else {
e.setNextException(next);
next = e;
}
//## Java 1.4 begin ##
result[i] = Statement.EXECUTE_FAILED;
//## Java 1.4 end ##
......@@ -641,7 +647,7 @@ public class JdbcStatement extends TraceObject implements Statement {
}
batchCommands = null;
if (error) {
throw new BatchUpdateException(result);
throw new JdbcBatchUpdateException(next, result);
}
return result;
} finally {
......
......@@ -53,12 +53,55 @@ public class TestBatchUpdates extends TestBase {
}
public void test() throws SQLException {
testRootCause();
testExecuteCall();
testException();
testCoffee();
deleteDb("batchUpdates");
}
private void testRootCause() throws SQLException {
deleteDb("batchUpdates");
conn = getConnection("batchUpdates");
stat = conn.createStatement();
stat.addBatch("select * from test_x");
stat.addBatch("select * from test_y");
try {
stat.executeBatch();
} catch (SQLException e) {
assertContains(e.toString(), "TEST_Y");
e = e.getNextException();
assertTrue(e != null);
assertContains(e.toString(), "TEST_Y");
e = e.getNextException();
assertTrue(e != null);
assertContains(e.toString(), "TEST_X");
e = e.getNextException();
assertTrue(e == null);
}
stat.execute("create table test(id int)");
PreparedStatement prep = conn.prepareStatement("insert into test values(?)");
prep.setString(1, "TEST_X");
prep.addBatch();
prep.setString(1, "TEST_Y");
prep.addBatch();
try {
prep.executeBatch();
} catch (SQLException e) {
assertContains(e.toString(), "TEST_Y");
e = e.getNextException();
assertTrue(e != null);
assertContains(e.toString(), "TEST_Y");
e = e.getNextException();
assertTrue(e != null);
assertContains(e.toString(), "TEST_X");
e = e.getNextException();
assertTrue(e == null);
}
stat.execute("drop table test");
conn.close();
}
private void testExecuteCall() throws SQLException {
deleteDb("batchUpdates");
conn = getConnection("batchUpdates");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论