Commit 27dc2164 authored by Tom Evans's avatar Tom Evans Committed by tevans

OF-205: Clean up duplicates when adding published items (per...

OF-205: Clean up duplicates when adding published items (per http://community.igniterealtime.org/message/225042#225042)

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13303 b35dd754-fafc-0310-a699-88a17e54d16e
parent 6def0fce
...@@ -1093,6 +1093,7 @@ public class PubSubPersistenceManager { ...@@ -1093,6 +1093,7 @@ public class PubSubPersistenceManager {
/** /**
* Creates and stores the published item in the database. * Creates and stores the published item in the database.
* Duplicate item (if found) is removed before storing the item.
* *
* @param item The published item to save. * @param item The published item to save.
*/ */
...@@ -1103,8 +1104,9 @@ public class PubSubPersistenceManager { ...@@ -1103,8 +1104,9 @@ public class PubSubPersistenceManager {
synchronized (itemsPending) { synchronized (itemsPending) {
LinkedListNode itemToReplace = itemsPending.remove(itemKey); LinkedListNode itemToReplace = itemsPending.remove(itemKey);
if (itemToReplace != null) { if (itemToReplace != null) {
itemToReplace.remove(); // delete from itemsToAdd linked list itemToReplace.remove(); // remove duplicate from itemsToAdd linked list
} }
itemsToDelete.addLast(item); // delete stored duplicate (if any)
LinkedListNode listNode = itemsToAdd.addLast(item); LinkedListNode listNode = itemsToAdd.addLast(item);
itemsPending.put(itemKey, listNode); itemsPending.put(itemKey, listNode);
} }
...@@ -1179,13 +1181,40 @@ public class PubSubPersistenceManager { ...@@ -1179,13 +1181,40 @@ public class PubSubPersistenceManager {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try // delete first (to remove possible duplicates), then add new items
{ if (delItem != null) {
try {
con = DbConnectionManager.getTransactionConnection(); con = DbConnectionManager.getTransactionConnection();
// Add all items that were cached LinkedListNode delHead = delList.getLast().next;
if (addItem != null) pstmt = con.prepareStatement(DELETE_ITEM);
while (delItem != delHead)
{ {
PublishedItem item = (PublishedItem) delItem.object;
pstmt.setString(1, item.getNode().getService().getServiceID());
pstmt.setString(2, encodeNodeID(item.getNode().getNodeID()));
pstmt.setString(3, item.getID());
pstmt.addBatch();
delItem = delItem.next;
}
pstmt.executeBatch();
}
catch (SQLException sqle)
{
log.error(sqle.getMessage(), sqle);
abortTransaction = true;
}
finally
{
DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
}
}
if (addItem != null) {
try {
con = DbConnectionManager.getTransactionConnection();
LinkedListNode addHead = addList.getLast().next; LinkedListNode addHead = addList.getLast().next;
pstmt = con.prepareStatement(ADD_ITEM); pstmt = con.prepareStatement(ADD_ITEM);
...@@ -1204,25 +1233,6 @@ public class PubSubPersistenceManager { ...@@ -1204,25 +1233,6 @@ public class PubSubPersistenceManager {
} }
pstmt.executeBatch(); pstmt.executeBatch();
} }
if (delItem != null)
{
LinkedListNode delHead = delList.getLast().next;
pstmt = con.prepareStatement(DELETE_ITEM);
while (delItem != delHead)
{
PublishedItem item = (PublishedItem) delItem.object;
pstmt.setString(1, item.getNode().getService().getServiceID());
pstmt.setString(2, encodeNodeID(item.getNode().getNodeID()));
pstmt.setString(3, item.getID());
pstmt.executeUpdate();
delItem = delItem.next;
}
}
}
catch (SQLException sqle) catch (SQLException sqle)
{ {
log.error(sqle.getMessage(), sqle); log.error(sqle.getMessage(), sqle);
...@@ -1233,6 +1243,7 @@ public class PubSubPersistenceManager { ...@@ -1233,6 +1243,7 @@ public class PubSubPersistenceManager {
DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction); DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
} }
} }
}
/** /**
* Removes the specified published item from the DB. * Removes the specified published item from the DB.
...@@ -1245,9 +1256,9 @@ public class PubSubPersistenceManager { ...@@ -1245,9 +1256,9 @@ public class PubSubPersistenceManager {
synchronized (itemsPending) synchronized (itemsPending)
{ {
itemsToDelete.addLast(item); itemsToDelete.addLast(item);
LinkedListNode itemToDelete = itemsPending.remove(itemKey); LinkedListNode itemToAdd = itemsPending.remove(itemKey);
if (itemToDelete != null) if (itemToAdd != null)
itemToDelete.remove(); itemToAdd.remove(); // drop from itemsToAdd linked list
} }
} }
......
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