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);
} }
...@@ -1145,7 +1147,7 @@ public class PubSubPersistenceManager { ...@@ -1145,7 +1147,7 @@ public class PubSubPersistenceManager {
// Swap pending items so we can parse and save the contents from this point in time // Swap pending items so we can parse and save the contents from this point in time
// while not blocking new entries from being cached. // while not blocking new entries from being cached.
synchronized(itemsPending) synchronized(itemsPending)
{ {
addList = itemsToAdd; addList = itemsToAdd;
delList = itemsToDelete; delList = itemsToDelete;
...@@ -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) {
con = DbConnectionManager.getTransactionConnection(); try {
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,33 +1233,15 @@ public class PubSubPersistenceManager { ...@@ -1204,33 +1233,15 @@ public class PubSubPersistenceManager {
} }
pstmt.executeBatch(); pstmt.executeBatch();
} }
catch (SQLException sqle)
{
if (delItem != null) log.error(sqle.getMessage(), sqle);
{ abortTransaction = true;
LinkedListNode delHead = delList.getLast().next; }
pstmt = con.prepareStatement(DELETE_ITEM); finally
{
while (delItem != delHead) DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
{ }
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)
{
log.error(sqle.getMessage(), sqle);
abortTransaction = true;
}
finally
{
DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
} }
} }
...@@ -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