I am working with a SQLite database on android. My database manager is a singleton and right now opens a connection to the database when it is initialized. It is safe to leave the database open the entire time so that when someone calls my class to work with the database it is already open? Or should I open and close the database before and after each access is needed. Is there any harm in just leaving it open the whole time?
Thanks!
i would keep it open the whole time, and close it in some lifecycle method such as
onStoporonDestroy. that way, you can easily check if the database is already in use by callingisDbLockedByCurrentThreadorisDbLockedByOtherThreadson the singleSQLiteDatabaseobject every time before you use it. this will prevent multiple manipulations to the database and save your application from a potential crashso in your singleton, you might have a method like this to get your single
SQLiteOpenHelperobject:so whenever you want to use your open helper object, call this getter method(make sure it’s threaded)
another method in your singleton may be(called EVERY TIME before you try to call the getter above):
you may want to close the database in the singleton as well:
if the users of your application have the ability to create many database interactions very quickly, you should use something like i have demonstrated above. but if there is minimal database interactions, i wouldn’t worry about it, and just create and close the database every time.