How and when to close the sqlite database in an application with a lot of actions and services

I read a lot about this topic, but no solution can be used in my application. Suppose there are several services and many actions that require access to db. Of course, only one activity is active. But in the worst case scenario there is one activity and two services performing background access to db.

So, when I close db from activity onResume - access to the db service will throw an exception. And the same thing when I close db from the onDestroy service and open the cursor in activity.

I am using some kind of DBManager in my application object as singleton. I am running DBManager in Application.onCreate. But there is no place to close db without risking that another Service and activity will join it.

So now I never close the DB - are there any consequences? Is there a simple (or not too complicated) solution for closing a database?

here are my applications on the market

Russian version

English version

+4
source share
2 answers

I do the same thing, I use a singleton database object and never close it until my application is destroyed. So far I have not noticed any problems. More detailed answer here:

fooobar.com/questions/22721 / ...

+3
source

You should try ContentProviders , the Android system will handle creating / opening / closing the database as needed.

+4
source

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


All Articles