Search code examples
androidandroid-contentproviderreadwritelock

Android contentprovider lock


Should I use a ReadWriteLock on the functions of the contentprovider?

In the query of the contentprovider I do getReadableDatabase, then check if its open and do the query. But sometimes it crashes on DatabaseIsClosed exeption. This could be that an other process does an insert that does a getWritebleDatabase which closes the first one.

I am using an app and a service who query and insert on the contentprovider constantly. So it seems that the contentprovider isn't threadsafe.

Could it help to make the function synchronized?

Thanks!


Solution

  • In the query of the contentprovider I do getReadableDatabase, then check if its open and do the query. But sometimes it crashes on DatabaseIsClosed exeption. This could be that an other process does an insert that does a getWritebleDatabase which closes the first one.

    You should be keeping your database open for the lifetime of the ContentProvider, AFAIK.

    So it seems that the contentprovider isn't threadsafe.

    AFAIK, a ContentProvider is only ever called on one thread.

    ContentProvider is mostly a facade and does not provide any intrinsic thread safety.