Couchbase.lite android exception Delete Database Failed: there are still open connections

I tried to delete my couchbase database on the device with database.delete(), but pretty often it failed with error:
"error=Can’t delete db file while other connections are open. The open connections are tagged appOpened, appOpened, appOpened. (CouchbaseLite Android v3.0.0-192@33 ”

I don’t use replicator, or change listeners
And when I checked the database status before the delete, it appears as closed (database.count() return 0, and database.getPath() return null).
I don’t know why there are still open connections.
Could you please provide me help on this.

The message is telling you that you’ve opened other Database instances without closing them as well. Do you open the database multiple times? If so you need to close it an equal number of times. You won’t find that information from just one instance of a database instance that is closed, you need to make sure all of them are (even those that are out of scope, they will still be open until either closed or garbage collected).

1 Like

Thank you for the quick response; I’m declaring my database instance like below, and calling it each time I use it, do you think it’s the best way o declare it, or I need to make it as a singleton instead:

val database: Database
        get() {
            // Initialize the Couchbase Lite system

            // Get the database (and create it if it doesn’t exist).
            val config = DatabaseConfiguration()

   = this.context.filesDir.absolutePath

            // configure the encryption key
            config.encryptionKey = EncryptionKey(this.preferences.couchbaseDatabaseKey)

            // setup logs
            Database.log.console.level = LogLevel.ERROR
            val logsDirectory = this.context.cacheDir.absolutePath + "/" + LOGS_PATH
            Database.log.file.config = LogFileConfiguration(logsDirectory)

            val database = try {
                Database(DB_NAME, config)
            } catch (throwable: Throwable) {
                val message = "database, cannot load Couchbase db, error=${throwable.message}"
                Log.e(TAG, message)
                throw Error(message)

            // create indexes
            this.createIndex(TABLE_UNIQUE_ID, database)
            this.createIndex(SERVER_ID, database)

            return database

Thank you in advance :slight_smile:

Whether or not you make a singleton is up to you but you need to make sure that you close every time you return from this function.