Is it good practice maintain the database open while app is running?
It is a personal preference.
I would personally keep it open the whole time, and close it in some lifecycle method such as onStop()
or onDestroy()
. That way, you can easily check if the database is already in use by calling isDbLockedByCurrentThread()
or isDbLockedByOtherThreads()
on the single SQLiteDatabase object every time before you use it. This will prevent multiple manipulations to the database and save your application from a potential crash, which is possible if you accidentally have two open instances in the case where you open/close per read/write.
Keep in mind that your application may be killed while you have an open database connection.
Also, you should use transactions to reduce the write operations to the disk at a time.
Does it matter?
A database is just another file on the disk at the end of the day. If you try to write to it from multiple threads/instances at the same time, you will run into problems.