What happens if I do not close the database in Android?

This name may seem a little crazy, but this is what bothers me. My application makes heavy use of local database operations. As suggested in Android docs and some blogs, I expanded the class SQLiteOpenHelperand defined all of my database operations. Since some of my database operations are performed in threads, opening and closing db causes some IllegalStateExceptions. So, I made a DB helper like Singleton, and he solved these problems, also got rid of operations openand closefor every DB action. Now everything works fine, although I never close the database.

  • My confusion is that you need to close the database?
  • If so, why is this a suitable place, is it located in the onDestroymain activity or somewhere else?
  • If I do not close the DB, what are the side effects?
+5
source share
3 answers

You can catch IllegalStateExceptionif you try to open the same database again.

If you create an instance of a method DBHelperin the onCreatemain activity, this will be a record to close db in onDestroy. So, you can be sure that the next time onCreateyour database will not be open already.

If you have a link to DBHelperthe service, it should be open and closed in the service, not in action.

Application db, (, BroadcastReceiver)

0

, , , / .

, onDestroy() mainActivity.

0

. , , . , , , " " db .

0

All Articles