Permalink
Browse files

fix: NPE in PreparedStatement.executeBatch in case of empty batch

This fixes regression in 42.1.2

fixes #866
  • Loading branch information...
vlsi committed Jul 14, 2017
1 parent 1843d01 commit 7514552d2d105cb8e637e70c8e14ab7e36000ed4
@@ -1550,7 +1550,10 @@ public void setURL(int parameterIndex, java.net.URL x) throws SQLException {
@Override
public int[] executeBatch() throws SQLException {
try {
if (batchParameters.size() > 1 && m_prepareThreshold > 0) {
// Note: in batch prepared statements batchStatements == 1, and batchParameters is equal
// to the number of addBatch calls
// batchParameters might be empty in case of empty batch
if (batchParameters != null && batchParameters.size() > 1 && m_prepareThreshold > 0) {
// Use server-prepared statements when there's more than one statement in a batch
// Technically speaking, it might cause to create a server-prepared statement
// just for 2 executions even for prepareThreshold=5. That however should be
@@ -140,6 +140,34 @@ public void testExecuteEmptyBatch() throws Exception {
stmt.close();
}
@Test
public void testExecuteEmptyPreparedBatch() throws Exception {
PreparedStatement ps = null;
try {
ps = con.prepareStatement("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
int[] updateCount = ps.executeBatch();
Assert.assertEquals("Empty batch should update empty result", 0, updateCount.length);
} finally {
TestUtil.closeQuietly(ps);
}
}
@Test
public void testPreparedNoParameters() throws SQLException {
PreparedStatement ps = null;
try {
ps = con.prepareStatement("INSERT INTO prep(a) VALUES (1)");
ps.addBatch();
ps.addBatch();
ps.addBatch();
ps.addBatch();
int[] actual = ps.executeBatch();
assertBatchResult("4 rows inserted via batch", new int[]{1, 1, 1, 1}, actual);
} finally {
TestUtil.closeQuietly(ps);
}
}
@Test
public void testClearBatch() throws Exception {
Statement stmt = con.createStatement();
@@ -160,6 +188,31 @@ public void testClearBatch() throws Exception {
stmt.close();
}
@Test
public void testClearPreparedNoArgBatch() throws Exception {
PreparedStatement ps = null;
try {
ps = con.prepareStatement("INSERT INTO prep(a) VALUES (1)");
ps.addBatch();
ps.clearBatch();
int[] updateCount = ps.executeBatch();
Assert.assertEquals("Empty batch should update empty result", 0, updateCount.length);
} finally {
TestUtil.closeQuietly(ps);
}
}
@Test
public void testClearPreparedEmptyBatch() throws Exception {
PreparedStatement ps = null;
try {
ps = con.prepareStatement("INSERT INTO prep(a) VALUES (1)");
ps.clearBatch();
} finally {
TestUtil.closeQuietly(ps);
}
}
@Test
public void testSelectInBatch() throws Exception {
Statement stmt = con.createStatement();

0 comments on commit 7514552

Please sign in to comment.