closed some cursors under error conditions

This commit is contained in:
Daniel Gultsch 2016-03-31 13:55:46 +02:00
parent 343a6b4e6b
commit 8d98c52803

View file

@ -524,8 +524,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
Cursor cursor = db.query(Conversation.TABLENAME, null, Cursor cursor = db.query(Conversation.TABLENAME, null,
Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
+ " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null); + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null);
if (cursor.getCount() == 0) if (cursor.getCount() == 0) {
cursor.close();
return null; return null;
}
cursor.moveToFirst(); cursor.moveToFirst();
Conversation conversation = Conversation.fromCursor(cursor); Conversation conversation = Conversation.fromCursor(cursor);
cursor.close(); cursor.close();
@ -575,12 +577,15 @@ public class DatabaseBackend extends SQLiteOpenHelper {
try { try {
cursor.moveToFirst(); cursor.moveToFirst();
int count = cursor.getInt(0); int count = cursor.getInt(0);
cursor.close();
return (count > 0); return (count > 0);
} catch (SQLiteCantOpenDatabaseException e) { } catch (SQLiteCantOpenDatabaseException e) {
return true; // better safe than sorry return true; // better safe than sorry
} catch (RuntimeException e) { } catch (RuntimeException e) {
return true; // better safe than sorry return true; // better safe than sorry
} finally {
if (cursor != null) {
cursor.close();
}
} }
} }
@ -642,11 +647,12 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
public Pair<Long, String> getLastMessageReceived(Account account) { public Pair<Long, String> getLastMessageReceived(Account account) {
Cursor cursor = null;
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1"; String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1";
String[] args = {account.getUuid()}; String[] args = {account.getUuid()};
Cursor cursor = db.rawQuery(sql, args); cursor = db.rawQuery(sql, args);
if (cursor.getCount() == 0) { if (cursor.getCount() == 0) {
return null; return null;
} else { } else {
@ -655,6 +661,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} finally {
if (cursor != null) {
cursor.close();
}
} }
} }