Commit 15f19d80 authored by Dave Cridland's avatar Dave Cridland Committed by GitHub

Merge pull request #686 from akrherz/of1229

OF-1229 Safeguard JDBC.executeBatch() calls on empty batch
parents 626417c6 476ab027
...@@ -1314,9 +1314,10 @@ public class PubSubPersistenceManager { ...@@ -1314,9 +1314,10 @@ public class PubSubPersistenceManager {
try { try {
LinkedListNode<PublishedItem> delHead = delItem.previous; LinkedListNode<PublishedItem> delHead = delItem.previous;
pstmt = con.prepareStatement(DELETE_ITEM); pstmt = con.prepareStatement(DELETE_ITEM);
Boolean hasBatchItems = false;
while (delItem != delHead) while (delItem != delHead)
{ {
hasBatchItems = true;
PublishedItem item = delItem.object; PublishedItem item = delItem.object;
pstmt.setString(1, item.getNode().getService().getServiceID()); pstmt.setString(1, item.getNode().getService().getServiceID());
pstmt.setString(2, encodeNodeID(item.getNode().getNodeID())); pstmt.setString(2, encodeNodeID(item.getNode().getNodeID()));
...@@ -1325,7 +1326,7 @@ public class PubSubPersistenceManager { ...@@ -1325,7 +1326,7 @@ public class PubSubPersistenceManager {
delItem = delItem.next; delItem = delItem.next;
} }
pstmt.executeBatch(); if (hasBatchItems) pstmt.executeBatch();
} catch (SQLException ex) { } catch (SQLException ex) {
log.error("Failed to delete published item(s) from DB", ex); log.error("Failed to delete published item(s) from DB", ex);
// do not re-throw here; continue with insert operation if possible // do not re-throw here; continue with insert operation if possible
...@@ -1359,8 +1360,10 @@ public class PubSubPersistenceManager { ...@@ -1359,8 +1360,10 @@ public class PubSubPersistenceManager {
PublishedItem item = null; PublishedItem item = null;
try { try {
pstmt = con.prepareStatement(ADD_ITEM); pstmt = con.prepareStatement(ADD_ITEM);
Boolean hasBatchItems = false;
while (addItem != addHead) while (addItem != addHead)
{ {
hasBatchItems = true;
wrappedItem = addItem.object; wrappedItem = addItem.object;
item = wrappedItem.get(); item = wrappedItem.get();
pstmt.setString(1, item.getNode().getService().getServiceID()); pstmt.setString(1, item.getNode().getService().getServiceID());
...@@ -1386,7 +1389,7 @@ public class PubSubPersistenceManager { ...@@ -1386,7 +1389,7 @@ public class PubSubPersistenceManager {
} }
addItem = addItem.next; addItem = addItem.next;
} }
if (batch) { pstmt.executeBatch(); } if (batch && hasBatchItems) { pstmt.executeBatch(); }
} catch (SQLException se) { } catch (SQLException se) {
log.error("Failed to persist published items as batch; will retry individually", se); log.error("Failed to persist published items as batch; will retry individually", se);
// caught by caller; should not cause a transaction rollback // caught by caller; should not cause a transaction rollback
...@@ -1880,8 +1883,10 @@ public class PubSubPersistenceManager { ...@@ -1880,8 +1883,10 @@ public class PubSubPersistenceManager {
PreparedStatement purgeNode = con PreparedStatement purgeNode = con
.prepareStatement(getPurgeStatement(DbConnectionManager.getDatabaseType())); .prepareStatement(getPurgeStatement(DbConnectionManager.getDatabaseType()));
Boolean hasBatchItems = false;
while (rs.next()) while (rs.next())
{ {
hasBatchItems = true;
String svcId = rs.getString(1); String svcId = rs.getString(1);
String nodeId = rs.getString(2); String nodeId = rs.getString(2);
int maxItems = rs.getInt(3); int maxItems = rs.getInt(3);
...@@ -1890,7 +1895,7 @@ public class PubSubPersistenceManager { ...@@ -1890,7 +1895,7 @@ public class PubSubPersistenceManager {
purgeNode.addBatch(); purgeNode.addBatch();
} }
purgeNode.executeBatch(); if (hasBatchItems) purgeNode.executeBatch();
} }
catch (Exception sqle) catch (Exception sqle)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment