We all learn that resources, such as database connections, must be received later and released earlier.
However, applying this principle to SQLite database connections on Android caused me some headache.
I have an application that downloads updates from the backend server in a service running in the background, regularly writing updates to the database. The problem I'm experiencing occurs when the following sequence occurs:
- The service provides write access to connect to the database
- Some actions open a readable database connection
- The service closes the database connection at the same time as the activity read data.
- Failed to complete due to database shutdown.
Both services and activity use the same class SQLiteOpenHelper, although different instances to open their connections. My initial assumption was that this should work fine, but for some reason it seems that the underlying connection is shared between two database instances.
To work around the problem, I ended up not closing the database objects, closing only all open cursors. This seems to work, although I'm not sure there is no memory leak here.
Is there something obvious I'm missing here?
source
share