As part of learning android, I am following the NotePad tutorial
One thing I noticed in the tutorials, the DB connection isn't being closed explicitly and it makes sense since startManagingCursor() handles it itself.
But if start/stop the app in a speedy manner (I manually started app by clicking it's icon and closed it by pressing back) I noticed DB wasn't getting closed & the following error was getting generated:
08-23 18:11:55.637: WARN/SQLiteCompiledSql(10784): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT _id, title, body FROM notes
08-23 18:11:55.637: WARN/SQLiteCompiledSql(10784): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database o开发者_JAVA技巧bject that was opened here
Closing the DB connection explicitly in onDestroy resolved the issue, but I guess this shouldn't have happened. Is it ok ?
I think that's an invalid question, since it relates to two different concepts. Cursor and SQLiteOpenHelper instances are different and should be close separately. When we use startManagingCursor() the cursor instance is closed but we need to manually close the SQLiteOpenHelper instance, which I was doing in onDestroy().
I tested this by not closing the SQLiteOpenHelper instance and used the startManagingCursor() to verify the error and I was getting it. Even closing the SQLiteOpenHelper instance and not closing the cursor instance (if you're not using startManagingCursor()) can lead to this error.
精彩评论