8

In my database adapter class, I have many methods like this:

public long getContactId(final String phoneNumber) throws SQLException {
    final Cursor cur = mDb.rawQuery(
            "select contact_id from contactphones where number=? limit 1;",
            new String[] { phoneNumber });
    return cur.moveToFirst() ? cur.getLong(0) : -1;
}

I appreciate the brevity of a method like that. But I am not calling Cursor.close(), and I'm not sure if that is a problem or not. Would the Cursor be closed and its resources freed in the Cursor.finalize()? Otherwise I would have to do:

public long getContactId(final String phoneNumber) throws SQLException {
    final Cursor cur = mDb.rawQuery(
            "select contact_id from contactphones where number=? limit 1;",
            new String[] { phoneNumber });
    final boolean retVal = cur.moveToFirst() ? cur.getLong(0) : -1;
    cur.close();
    return retVal;
}
adam.baker
  • 1,447
  • 1
  • 14
  • 30

3 Answers3

2

Yes, it's recommended to close the cursor when you are done using that cursor object so that cursor can do whatever house keeping work it wants to do upon closure.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Dalvinder Singh
  • 2,129
  • 4
  • 21
  • 20
1

Cursor is not a class but an interface. If your Cursor object is from a SQLite query, it is a SQLiteCursor. In that definition (\Android\android-sdk\source\android\database\sqlite\SQLiteCursor.java), close() is called in the finalize() function. This may be different in other cursor types, since the Cursor interface does not specify this behavior.

Rathakrishnan Ramasamy
  • 1,612
  • 2
  • 25
  • 46
adam.baker
  • 1,447
  • 1
  • 14
  • 30
  • 20
    This doesn't answer the question. – wvdz May 15 '14 at 20:31
  • close() is called in finalize() method right but that does not mean we don't need to close the cursor! In fact we have to close the cursor after we are done with the cursor. Why? because we don't know when finalize() method is going to be called or worst we don't know if it's getting called at all. java does not grantee execution of finalize() method at all!! there are other reason too like performance issues of running codes in finalize() method and exception handling problem (no exception handling is possible in finalize method). – Alireza Ahmadi Mar 20 '15 at 17:57
  • So why google developers put close() inside finalize() simple: they thought if some lazy developer forgot to call close() they should free up resources even if it's late and it's not guaranteed. It's still better than nothing. right? In conclusion close() should be called all the times. You can also set cursor to null after closing it. it allow garbage collector to remove it from memory faster but i don't recommend it since it's just one java object and performance gain by this is not going to change anything. – Alireza Ahmadi Mar 20 '15 at 18:03
0

Don't close the database. There is no need (data gets safely written to persistent storage at the earliest opportunity anyway). Open it once, on application startup, and reuse the same connection throughout your application's lifetime

Please refer this link

Community
  • 1
  • 1
Juned
  • 6,290
  • 7
  • 45
  • 93