Commit 375667ac authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

More JDBC enhancements.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1751 b35dd754-fafc-0310-a699-88a17e54d16e
parent 53104675
...@@ -187,6 +187,8 @@ public class RosterItemProvider { ...@@ -187,6 +187,8 @@ public class RosterItemProvider {
pstmt.setLong(1, rosterItemID); pstmt.setLong(1, rosterItemID);
pstmt.executeUpdate(); pstmt.executeUpdate();
// Close now the statement (do not wait to be GC'ed)
pstmt.close();
// Remove roster // Remove roster
pstmt = con.prepareStatement(DELETE_ROSTER_ITEM); pstmt = con.prepareStatement(DELETE_ROSTER_ITEM);
...@@ -280,15 +282,19 @@ public class RosterItemProvider { ...@@ -280,15 +282,19 @@ public class RosterItemProvider {
public Iterator<RosterItem> getItems(String username) { public Iterator<RosterItem> getItems(String username) {
LinkedList<RosterItem> itemList = new LinkedList<RosterItem>(); LinkedList<RosterItem> itemList = new LinkedList<RosterItem>();
Connection con = null; Connection con = null;
Connection con2 = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
PreparedStatement gstmt = null;
try { try {
con2 = DbConnectionManager.getConnection();
gstmt = con2.prepareStatement(LOAD_ROSTER_ITEM_GROUPS);
// Load all the contacts in the roster
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ROSTER); pstmt = con.prepareStatement(LOAD_ROSTER);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
// TODO: this code must be refactored ASAP. Not legal to have two open pstmts
// TODO: on many databases.
while (rs.next()) { while (rs.next()) {
// Create a new RosterItem (ie. user contact) from the stored information
RosterItem item = new RosterItem(rs.getLong(2), RosterItem item = new RosterItem(rs.getLong(2),
new JID(rs.getString(1)), new JID(rs.getString(1)),
RosterItem.SubType.getTypeFromInt(rs.getInt(3)), RosterItem.SubType.getTypeFromInt(rs.getInt(3)),
...@@ -296,24 +302,17 @@ public class RosterItemProvider { ...@@ -296,24 +302,17 @@ public class RosterItemProvider {
RosterItem.RecvType.getTypeFromInt(rs.getInt(5)), RosterItem.RecvType.getTypeFromInt(rs.getInt(5)),
rs.getString(6), rs.getString(6),
null); null);
Connection con2 = DbConnectionManager.getConnection(); // Load the groups for the loaded contact
PreparedStatement gstmt = null;
ResultSet gs = null; ResultSet gs = null;
try { gstmt.setLong(1, item.getID());
gstmt = con2.prepareStatement(LOAD_ROSTER_ITEM_GROUPS); gs = gstmt.executeQuery();
gstmt.setLong(1, item.getID()); while (gs.next()) {
gs = gstmt.executeQuery(); item.getGroups().add(gs.getString(1));
while (gs.next()) {
item.getGroups().add(gs.getString(1));
}
itemList.add(item);
}
finally {
try { if (gstmt != null) { gstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con2 != null) { con2.close(); } }
catch (Exception e) { Log.error(e); }
} }
// Close the result set
gs.close();
// Add the loaded RosterItem (ie. user contact) to the result
itemList.add(item);
} }
} }
catch (SQLException e) { catch (SQLException e) {
...@@ -324,6 +323,10 @@ public class RosterItemProvider { ...@@ -324,6 +323,10 @@ public class RosterItemProvider {
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } } try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); } catch (Exception e) { Log.error(e); }
try { if (gstmt != null) { gstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con2 != null) { con2.close(); } }
catch (Exception e) { Log.error(e); }
} }
return itemList.iterator(); return itemList.iterator();
} }
......
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