public void Disconnect(NativeBindings bindings, Connection connection) { lock (_locker) { // close connection itself var connectionPtr = connection.Pointer; bindings.DuckdbDisconnect(ref connectionPtr); var database = _connectionsToDatabases[connection]; _connectionsToDatabases.Remove(connection); if (!_connectionsToDatabases.ContainsValue(database)) { // close database if there are no connections to it var databasePtr = database.Pointer; bindings.DuckdbClose(ref databasePtr); var file = _filesToDatabases.FirstOrDefault(pair => pair.Value == database).Key; if (file != null) { _filesToDatabases.Remove(file); } var name = _namesToDatabases.FirstOrDefault(pair => pair.Value == database).Key; if (name != null) { _namesToDatabases.Remove(name); } } } }