Android sqlite leaving connection open
I create a SQLiteDatabase instance and call getReadableDatabase()
or getWritableDatabase()
and manipulate data that way but I never close the database 开发者_JAVA百科via db.close()
. Is it bad to not close it? I tried adding db.close()
to my onStop()
or onDestroy()
methods but it just force closes.
Open a connection as late as possible. Close it as soon as possible. Close it in a "finally" block to make sure it gets closed.
And in general, dispose of resources in the reverse order that you obtain them.
You are basically bloating your application by having an extra cursor open. Best practice is to close the connection.
I have noticed that there are a lot of exceptions thrown if you dont do it.
If you don't close the database connections, they'll cause memory leaks over time. you can use startManagingCursor but along with this you have to close the db connection. by the way what type of error or exception you are getting while closing db connection in onStop() or onDestroy() method
精彩评论