提交 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 { ...@@ -23,6 +23,7 @@ public class JdbcBatchUpdateException extends BatchUpdateException {
*/ */
JdbcBatchUpdateException(SQLException next, int[] updateCounts) { JdbcBatchUpdateException(SQLException next, int[] updateCounts) {
super(next.getMessage(), next.getSQLState(), next.getErrorCode(), updateCounts); super(next.getMessage(), next.getSQLState(), next.getErrorCode(), updateCounts);
setNextException(next);
} }
/** /**
......
...@@ -1091,14 +1091,13 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -1091,14 +1091,13 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
try { try {
result[i] = executeUpdateInternal(); result[i] = executeUpdateInternal();
} catch (Exception re) { } catch (Exception re) {
SQLException e = DbException.toSQLException(re); SQLException e = logAndConvert(re);
if (next == null) { if (next == null) {
next = e; next = e;
} else { } else {
e.setNextException(next); e.setNextException(next);
next = e; next = e;
} }
logAndConvert(e);
//## Java 1.4 begin ## //## Java 1.4 begin ##
result[i] = Statement.EXECUTE_FAILED; result[i] = Statement.EXECUTE_FAILED;
//## Java 1.4 end ## //## Java 1.4 end ##
...@@ -1108,7 +1107,6 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -1108,7 +1107,6 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
batchParameters = null; batchParameters = null;
if (error) { if (error) {
JdbcBatchUpdateException e = new JdbcBatchUpdateException(next, result); JdbcBatchUpdateException e = new JdbcBatchUpdateException(next, result);
e.setNextException(next);
throw e; throw e;
} }
return result; return result;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
*/ */
package org.h2.jdbc; package org.h2.jdbc;
import java.sql.BatchUpdateException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -627,12 +626,19 @@ public class JdbcStatement extends TraceObject implements Statement { ...@@ -627,12 +626,19 @@ public class JdbcStatement extends TraceObject implements Statement {
int size = batchCommands.size(); int size = batchCommands.size();
int[] result = new int[size]; int[] result = new int[size];
boolean error = false; boolean error = false;
SQLException next = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String sql = batchCommands.get(i); String sql = batchCommands.get(i);
try { try {
result[i] = executeUpdateInternal(sql); result[i] = executeUpdateInternal(sql);
} catch (Exception e) { } catch (Exception re) {
logAndConvert(e); SQLException e = logAndConvert(re);
if (next == null) {
next = e;
} else {
e.setNextException(next);
next = e;
}
//## Java 1.4 begin ## //## Java 1.4 begin ##
result[i] = Statement.EXECUTE_FAILED; result[i] = Statement.EXECUTE_FAILED;
//## Java 1.4 end ## //## Java 1.4 end ##
...@@ -641,7 +647,7 @@ public class JdbcStatement extends TraceObject implements Statement { ...@@ -641,7 +647,7 @@ public class JdbcStatement extends TraceObject implements Statement {
} }
batchCommands = null; batchCommands = null;
if (error) { if (error) {
throw new BatchUpdateException(result); throw new JdbcBatchUpdateException(next, result);
} }
return result; return result;
} finally { } finally {
......
...@@ -53,12 +53,55 @@ public class TestBatchUpdates extends TestBase { ...@@ -53,12 +53,55 @@ public class TestBatchUpdates extends TestBase {
} }
public void test() throws SQLException { public void test() throws SQLException {
testRootCause();
testExecuteCall(); testExecuteCall();
testException(); testException();
testCoffee(); testCoffee();
deleteDb("batchUpdates"); 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 { private void testExecuteCall() throws SQLException {
deleteDb("batchUpdates"); deleteDb("batchUpdates");
conn = getConnection("batchUpdates"); conn = getConnection("batchUpdates");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论