Couchbase Lite can't delete db : LiteCoreException {domain=1, code=16, msg=Can't delete db file while other connections are open}


I am trying to delete a database in android Java before each instrumentation test. For each test in @Before function, I create/open the database and truncate it and after the test runs in @After function I close the database. There are no replicators attached during instrumentation test only change listeners are attached which are also removed in the @After function using the token. For some of the tests, this works fine and randomly breaks for one test. I also tried adding wait after removing the listener in case it is asynchronous.

Caused by: LiteCoreException{domain=1, code=16, msg=Can't delete db file while other connections are open}

Extension to my previous comment, during debugging I looked at the database instance and all instance variables of listeners are always null with replicator and live query counts being zero.

I am using couchbaseLite 2.7

I believe the problem is that, in Java, the database connection is closed by a finalizer method which doesn’t run for some time. @blake.meike, is this accurate?

Try forcing a GC before deleting the database and see if that helps.

@jens Hi jens, thank you for your reply. I’ll test this out and let you know.

Jens is correct. Several database components are freed only by the Database finalizer. This is something that I expect to fix in our next release.

“Forcing” a GC is kind of a voudoo way to address this problem. We’ve had exactly the same problem in our own automated tests. I fixed them all by creating a new, uniquely named DB for each test, and deleting them all at once, best effort, in an @AfterClass method. Tests now run about twice as fast and the failure of one test is much less likely to affect the next.

1 Like

@blake.meike Thank you for the suggestion. I am already using a solution of such sort right now. Having a better fix for this in the next release sounds great. Looking forward to the next release.

Thank you.