cloudant / sync-android

A JSON-based document datastore for Android applications
Apache License 2.0
267 stars 91 forks source link

sync-android is complaining that the database is locked #341

Closed eforth closed 8 years ago

eforth commented 8 years ago

All queries to the database are sequentially done. I used a Executors.newSingleThreadExecutor() to ensure only one task is executed at any time however after successfully executing one task and then moving on to the next I received the following message:

E/SQLiteLog: (5) database is locked and E/SQLiteDatabase: Failed to open database /data/data/app/app_datastores/database/extensions/com.cloudant.sync.query/indexes.sqlite

Below is an example of one of the queries:

` Datastore ds = DataManager.getInstance().getPersistenceService().getDs(); ConfigService configService = DataManager.getInstance().getConfigService(); IndexManager im = new IndexManager(ds); DocumentRevision documentRevision = new DocumentRevision();

Map<String, Object> query = new HashMap<>();
query.put("patientId", configService.getProfileId());
query.put("type", "testResults");
query.put("typeOfTest", "blood_pressure");

List<Map<String, String>> sortDocument = new ArrayList<>();
Map<String, String> sortByPatient = new HashMap<>();
Map<String, String> sortByType = new HashMap<>();
Map<String, String> sortByTestType = new HashMap<>();
Map<String, String> sortByTime = new HashMap<>();
sortByPatient.put("patientId", "desc");
sortByType.put("type", "desc");
sortByTestType.put("typeOfTest", "desc");
sortByTime.put("timeRecorded", "desc");
sortDocument.add(sortByPatient);
sortDocument.add(sortByType);
sortDocument.add(sortByTestType);
sortDocument.add(sortByTime);

QueryResult bloodPressureResult = im.find(query, 0, 1, null, sortDocument);

if (result != null) {
    for (DocumentRevision rev : bloodPressureResult) {
        documentRevision = rev;
    }
}

im.close();

return documentRevision;`

Can you give me a clue as to why after executing this query (Successfully) and then trying execute something similar that the database would complain that it is locked?

tomblench commented 8 years ago

Hi @eforth we'll try to reproduce your issue and let you know how we get on. The IndexManager class actually uses a queue internally for all database access so these should be serialised as long as you always use the same IndexManager.

Can you tell us which version of the library you are using?

I would advise you to try using the same IndexManager instance throughout the lifetime of your application rather than creating a new one each time. This may help with your issue.

eforth commented 8 years ago

I was using version 1.0.0 then upgraded to 1.1.0 but the issue persists. I try out your suggestion.

eforth commented 8 years ago

Your suggestion worked. Thanks!

rhyshort commented 8 years ago

Great glad it works for you :)