getDatabase(dbName) is returning null

Hi,

I am working on an app in Android. getDatabase is working fine when Android application is started. If DB does not exist, it creates one. However, it is not working as expected while browsing the application.

Here is the scenario … When application is started, startSyncingAuthor is called to start multiple pull replications.
startSyncingAuthor(“abc-1”);
startSyncingAuthor(“abc-2”);
startSyncingAuthor(“abc-3”);

public void startSyncingAuthor(String authorDBName) {

    if (authorDBName == null)
        return;

    try {

        String serverDbAddress = StaticVariables.HTTP_ADDRESS_WITH_PORT + authorDBName;

        this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);

        if (this.couchbaseDatabase != null) {
            URL syncServerAddressUrl = new URL(serverDbAddress);
            Replication pullReplication = this.couchbaseDatabase.createPullReplication(syncServerAddressUrl);
            pullReplication.setContinuous(true);
            pullReplication.setFilter("by_type/by_android_replication");
            Authenticator auth = new BasicAuthenticator(StaticVariables.USERNAME, StaticVariables.PASSWORD);
            pullReplication.setAuthenticator(auth);
            pullReplication.start();
        }

    } catch (CouchbaseLiteException e) {
        Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
        Crashlytics.getInstance().core.logException(e);
        e.printStackTrace();
    } catch (MalformedURLException e) {
        Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
        Crashlytics.getInstance().core.logException(e);
        throw new RuntimeException(e);
    } catch (Exception e) {
        Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
        Crashlytics.getInstance().core.logException(e);
        e.printStackTrace();
    }
}

It works as expected… Later, within the app, the same function startSyncingAuthor is called again but the statement
this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);
returns null.

It returns null when DB does not exist. As per document it should create DB if it does not exist. If DB already exists, it returns DB as expected.

I tested it separately by creating dummy DBs by calling following function testingNewDBs within the app. Again, getDatabase() is returning null for non existing DBs. However this same function works as expected if called when application is first started.

public void testingNewDBs() {
    try {
        String authorDBName;
        for (int i = 0; i <= 5; i++) {
            authorDBName = "new-database" + i;
            this.couchbaseDatabase = this.couchbaseManager.getDatabase(authorDBName);
            if (this.couchbaseDatabase != null) {
                LogMessenger.printMessage("this.couchbaseDatabase", "IS NOT NULL");
            } else {
                LogMessenger.printMessage("this.couchbaseDatabase", "IS NULL");
            }
        }
    } catch (CouchbaseLiteException e) {
        Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
        Crashlytics.getInstance().core.logException(e);
        e.printStackTrace();
    } catch (Exception e) {
        Crashlytics.log(android.util.Log.ERROR, "", LogMessenger.getMessage(e.getMessage()));
        Crashlytics.getInstance().core.logException(e);
        e.printStackTrace();
    }
}

Any pointers would be of great help.

Thanks

Hi,

I resolved the issue.

Just to help others.

getDatabase(dbName) is going to fails/throws exceptions under following conditions

  1. If database name is null or empty. Check it before calling getDatabase(). I think most of us are aware of this.
  2. If a second parameter is passed. mustExist = true or false. If it is set to true. It will fail.
  3. If you change the options Manager.DEFAULT_OPTIONS such that you set the variable readOnly to True.

In my case the database was not getting created because of point 3 above.

One more thing, if you execute 3rd point above, you will always get getDatabase(dbName) as null till you close your Android application and rerun it again.

I don’t know if it should be called a bug.

Thanks

1 Like

Sounds correct to me — read-only means it won’t create a database. What’s the behavior you would expect to see?

I am new to CouchDB and I wasn’t aware of it.

However,
One more behavior I observed is that once the statement getDatabase(dbName) (//where readOnly = True in Manager.DEFAULT_OPTIONS ) is run, you will keep getting null for running getDatabase(dbName) even for those cases in which readOnly = False.

For example

  1. Run getDatabase(dbName) (//where readOnly = True)
  2. And then Run getDatabase(dbName) (//where readOnly = False)

Both would return null. The revere is obviously not true (1 followed by 2)

Thanks