Commit 516a1dbb authored by Matt Tucker's avatar Matt Tucker Committed by matt

Database logic cleanup. Removed unecessary delete when there are no offline messages.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@4288 b35dd754-fafc-0310-a699-88a17e54d16e
parent b2aa22a2
...@@ -130,10 +130,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -130,10 +130,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
// Update the cached size if it exists. // Update the cached size if it exists.
...@@ -177,8 +174,9 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -177,8 +174,9 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
messages.add(message); messages.add(message);
} }
rs.close(); rs.close();
// Check if the offline messages loaded should be deleted // Check if the offline messages loaded should be deleted, and that there are
if (delete) { // messages to delete.
if (delete && !messages.isEmpty()) {
pstmt.close(); pstmt.close();
pstmt = con.prepareStatement(DELETE_OFFLINE); pstmt = con.prepareStatement(DELETE_OFFLINE);
...@@ -193,11 +191,10 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -193,11 +191,10 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
} }
finally { finally {
// Return the sax reader to the pool // Return the sax reader to the pool
if (xmlReader != null) xmlReaders.add(xmlReader); if (xmlReader != null) {
try { if (pstmt != null) { pstmt.close(); } } xmlReaders.add(xmlReader);
catch (Exception e) { Log.error(e); } }
try { if (con != null) { con.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
} }
return messages; return messages;
} }
...@@ -214,6 +211,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -214,6 +211,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
OfflineMessage message = null; OfflineMessage message = null;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
SAXReader xmlReader = null; SAXReader xmlReader = null;
try { try {
// Get a sax reader from the pool // Get a sax reader from the pool
...@@ -222,18 +220,16 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -222,18 +220,16 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
pstmt = con.prepareStatement(LOAD_OFFLINE_MESSAGE); pstmt = con.prepareStatement(LOAD_OFFLINE_MESSAGE);
pstmt.setString(1, username); pstmt.setString(1, username);
pstmt.setString(2, StringUtils.dateToMillis(creationDate)); pstmt.setString(2, StringUtils.dateToMillis(creationDate));
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String msgXML = rs.getString(1); String msgXML = rs.getString(1);
message = message = new OfflineMessage(creationDate,
new OfflineMessage(creationDate,
xmlReader.read(new StringReader(msgXML)).getRootElement()); xmlReader.read(new StringReader(msgXML)).getRootElement());
// Add a delayed delivery (JEP-0091) element to the message. // Add a delayed delivery (JEP-0091) element to the message.
Element delay = message.addChildElement("x", "jabber:x:delay"); Element delay = message.addChildElement("x", "jabber:x:delay");
delay.addAttribute("from", XMPPServer.getInstance().getServerInfo().getName()); delay.addAttribute("from", XMPPServer.getInstance().getServerInfo().getName());
delay.addAttribute("stamp", dateFormat.format(creationDate)); delay.addAttribute("stamp", dateFormat.format(creationDate));
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error("Error retrieving offline messages of username: " + username + Log.error("Error retrieving offline messages of username: " + username +
...@@ -241,11 +237,10 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -241,11 +237,10 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
} }
finally { finally {
// Return the sax reader to the pool // Return the sax reader to the pool
if (xmlReader != null) xmlReaders.add(xmlReader); if (xmlReader != null) {
try { if (pstmt != null) { pstmt.close(); } } xmlReaders.add(xmlReader);
catch (Exception e) { Log.error(e); } }
try { if (con != null) { con.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
} }
return message; return message;
} }
...@@ -270,10 +265,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -270,10 +265,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
Log.error("Error deleting offline messages of username: " + username, e); Log.error("Error deleting offline messages of username: " + username, e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -301,9 +293,9 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -301,9 +293,9 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
pstmt.setString(2, StringUtils.dateToMillis(creationDate)); pstmt.setString(2, StringUtils.dateToMillis(creationDate));
pstmt.executeUpdate(); pstmt.executeUpdate();
//force a refresh for next call to getSize(username) // Force a refresh for next call to getSize(username),
//its easier than loading the msg to be deleted just // it's easier than loading the message to be deleted just
//to update the cache. // to update the cache.
removeUsernameFromSizeCache(username); removeUsernameFromSizeCache(username);
} }
catch (Exception e) { catch (Exception e) {
...@@ -311,10 +303,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -311,10 +303,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
" creationDate: " + creationDate, e); " creationDate: " + creationDate, e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -333,15 +322,15 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -333,15 +322,15 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
int size = 0; int size = 0;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SELECT_SIZE_OFFLINE); pstmt = con.prepareStatement(SELECT_SIZE_OFFLINE);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
size = rs.getInt(1); size = rs.getInt(1);
} }
rs.close();
// Add the value to cache. // Add the value to cache.
sizeCache.put(username, size); sizeCache.put(username, size);
} }
...@@ -349,10 +338,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -349,10 +338,7 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return size; return size;
} }
...@@ -367,23 +353,20 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -367,23 +353,20 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
int size = 0; int size = 0;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SELECT_SIZE_ALL_OFFLINE); pstmt = con.prepareStatement(SELECT_SIZE_ALL_OFFLINE);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
size = rs.getInt(1); size = rs.getInt(1);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return size; return size;
} }
......
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