Beispiel #1
0
    public override async Task CloseAsync()
#endif
    {
        if (!IsClosed && _innerConnection != null)
        {
            try
            {
                await _innerConnection.CloseEventManagerAsync(CancellationToken.None).ConfigureAwait(false);

                if (_innerConnection.Database != null)
                {
                    _innerConnection.Database.WarningMessage = null;
                }

                await _innerConnection.DisposeTransactionAsync(CancellationToken.None).ConfigureAwait(false);

                await _innerConnection.ReleasePreparedCommandsAsync(CancellationToken.None).ConfigureAwait(false);

                if (_options.Pooling)
                {
                    if (_innerConnection.CancelDisabled)
                    {
                        await _innerConnection.EnableCancelAsync(CancellationToken.None).ConfigureAwait(false);
                    }

                    var broken = _innerConnection.Database.ConnectionBroken;
                    FbConnectionPoolManager.Instance.Release(_innerConnection, !broken);
                    if (broken)
                    {
                        await DisconnectEnlistedHelper().ConfigureAwait(false);
                    }
                }
                else
                {
                    await DisconnectEnlistedHelper().ConfigureAwait(false);
                }
            }
            catch
            { }
            finally
            {
                OnStateChange(_state, ConnectionState.Closed);
            }
        }

        async Task DisconnectEnlistedHelper()
        {
            if (!_innerConnection.IsEnlisted)
            {
                await _innerConnection.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
            }
            _innerConnection = null;
        }
    }