Android sqlite database error: database is locked code 5

#1

Greetings, I am trying to access the sqlite db from two different activities and this is causing the error database locked code 5. Is there any way to work around this so that i can access the db from two different activities? or do i have to reduce the manager instances accessing the same db?

Your help is desperately needed. thanks

#2

Hi @mikekara,

To understand the problem, could you please share how to make instance of Manager and Database, and how to access database from multiple activities?

do i have to reduce the manager instances accessing the same db?

We recommend one Manager instance per application. Following sample is how to create single instance of Manager in Application class.
https://github.com/couchbaselabs/ToDoLite-Android/blob/master/ToDoLite/src/main/java/com/couchbase/todolite/Application.java

Creating multiple Database instances per Manager is fine. But each database name should be unique.

This might solve your issue.

#3

hi @hideki, thank you for the reply, I am creating the manager instance like this:

    public static final String DATABASE_NAME = "date-jack";
    public static final String designDocName = "date-local";
    public static final String byDateViewName = "byDate";
    public static final String SYNC_URL = "https://blaaaah;
    protected static Manager; 
    private Database data;
    protected void startCBLite() throws Exception {

        Manager.enableLogging(TAGz, com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG,  com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG_SYNC_ASYNC_TASK, com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG_SYNC, com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG_QUERY, com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG_VIEW, com.couchbase.lite.util.Log.VERBOSE);
        Manager.enableLogging(com.couchbase.lite.util.Log.TAG_DATABASE, com.couchbase.lite.util.Log.VERBOSE);
        
        manag = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
        Log.d(TAGz, "manager created");
        
        databas = manag.getDatabase(DATABASE_NAME);
        
        com.couchbase.lite.View viewItemsByDate = databas.getView(String.format("%s/%s", designDocNamez, byDateViewNamez));

        viewItemsByDate.setMap(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                Object createdAt = document.get("numbers");
                Log.d("object ", " " + createdAt);
                List<Object> data = (List<Object>) createdAt;
                List<Object> po = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    po.add(data.get(i));
                }
               
                String nulla = null;
                if (pho == null) {
                    emitter.emit(creat.toString(), null);
                } else {
                    //!createdAt.equals(nulla)
                    if (po.contains(pho)) {
                        emitter.emit(createdAt.toString(), null);
                        Log.d("objects ", " " + po.toString());
                    } else {
                        Log.e("error ", " " + po.toString());
                    }
                }
            }
        }, "2.0");
    }

I am using this view to populate a listiview from the same database with same documents, in two activities, however the logcat error keeps showing this:

07-15 21:40:48.336 2452-2743/com.example.user.sportsasa E/Batcher﹕ com.couchbase.lite.support.Batcher$1@5368da18: BatchProcessor throw exception
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method)
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:548)
at android.database.sqlite.SQLiteSession.beginTransactionUnchecked(SQLiteSesson.java:323)

#4

Hi @mikekara,

Are startCBLite() method and Manager variable defined in Activity? Try to move Manager and Database initializer in Application.java like ToDoLite sample.

I guess SQLite Lock issue could be caused by accessing sqlite database file from multiple SQLite Java instances. If this is issue, above solution could solve the problem.

Thanks.