This question is a little old, but still very relevant. Note that if you are doing something in a βmodernβ way (for example, using LoaderManager and creating CursorLoaders to request a ContentProvider in the background thread), make sure you do not call db.close () in your implementation of ContentProvider. I got all kinds of crashes related to CursorLoader / AsyncTaskLoader when it tried to access the ContentProvider in the background thread, which were fixed by removing the db.close () calls.
So, if you run into crashes like this (Jelly Bean 4.1.1):
Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed. at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962) at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:677) at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348) at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894) at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834) at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143) at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133) at android.content.ContentResolver.query(ContentResolver.java:388) at android.content.ContentResolver.query(ContentResolver.java:313) at com.hindsightlabs.paprika.loaders.GroceryListLoader.loadInBackground(GroceryListLoader.java:147) at com.hindsightlabs.paprika.loaders.GroceryListLoader.loadInBackground(GroceryListLoader.java:1) at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240) at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51) at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40) at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) ... 4 more
Or this (ICS 4.0.4):
Caused by: java.lang.IllegalStateException: database /data/data/com.hindsightlabs.paprika/databases/Paprika.db (conn
Or if you see error messages in LogCat that look like this:
Cursor: invalid statement in fillWindow()
Then check the implementation of ContentProvider and make sure that you are not closing the database prematurely. Accordingly , the ContentProvider will be automatically cleared when the process is killed, so you do not need to close its database ahead of time.
However, make sure you are still correct:
- Closing your cursors that return from ContentProvider.query () . (CursorLoader / LoaderManager does this automatically for you, but if you run direct queries outside the scope of LoaderManager or you have implemented your own subclass CursorLoader / AsyncTaskLoader, you need to make sure that you clear your cursors properly).
- Implementing your ContentProvider in a thread-safe manner. (The easiest way to do this is to make sure that the database access methods are wrapped in a synchronized block.)
Steven Aug 09 2018-12-12T00: 00Z
source share