Android sqlite leaving connection open

I create an instance of SQLiteDatabase and call getReadableDatabase()or getWritableDatabase()and process the data this way, but never close the database with db.close(). Is it bad not to close it? I tried adding db.close()to my methods onStop()or onDestroy(), but just forcibly closes.

+2
source share
3 answers

Open the connection as late as possible. Close it as soon as possible. Close it in the finally block to make sure it is closed.

In general, dispose of resources in reverse order to receive them.

+5
source

, . .

, , .

+2

If you do not close connections to the database, they will cause a memory leak over time. you can use startManagingCursor, but with that you need to close the db connection. By the way, what type of error or exception do you get when you close the db connection in the onStop () or onDestroy () methods

0
source

Source: https://habr.com/ru/post/1791194/


All Articles