I discover that
db.Close() doesn’t wait for the db to be closed. The db will eventually get closed after I call
db.Close(), it might be immediately after the call or 100ms after.
I was trying to make a copy of the db right after it’s closed. Sometimes, the copied db contains
.sqlite-wal file. Normally, the wal file will get removed when sqlite is closed successfully. So if the copy of the database contains wal file, it means the database is still being opened while I’m copying it.
Is this behavior by design or is it a bug? Well, I would prefer
db.Close() will block until it really closes. Currently I will use a busy loop to check if the wal file is gone to ensure the db is really closed. Hopefully there will be a better way to do that.
Closing a database should be synchronous … what version of CBL are you using, and what platform?
If you’re using C# or Java, this might be because the underlying SQLite database doesn’t fully close until all open ‘statement’ objects on it are closed. In terms of CBL, those are queries and query results. The problem is that those objects are in a garbage-collected environment and won’t free their statements until they’re finalized; and finalization can take a little while.
Forget to provide the platform detail. I’m experiencing this behavior when using C# 2.0db22.
It’s there a better way to ensure the finalization is done?
For C#, just be responsible about disposing objects which are marked as IDisposable. Otherwise, you cannot ensure it other than by forcing a garbage collection (bad idea), which won’t work if something is still referencing your object anyway. If you are still seeing bad behavior and you are disposing everything try to create a reproduction case and post it as an issue.
I checked the C# source code.
Dispose(), so I suppose it won’t help if I call Dispose() again.
Nope, Close is a wrapper for Dispose so that the API can remain consistent. But check other things you have kept open as well (such as documents, queries, etc). Any one of those left open could leave the actual database connection active.
Thanks for the information. And that explains why the C# tutorial use the
After doing a naive test, it’s not necessary to
Dispose Document/MutableDocument. But the db will stays opened until the Query is disposed. So be sure to Dispose any query before closing the db.