Beispiel #1
0
        public static async Task CreateDatabaseAsync(string connectionString, int pageSize = 4096, bool forcedWrites = true, bool overwrite = false, CancellationToken cancellationToken = default)
        {
            var options = new ConnectionString(connectionString);

            options.Validate();

            try
            {
                var db = new FbConnectionInternal(options);
                try
                {
                    await db.CreateDatabaseAsync(pageSize, forcedWrites, overwrite, cancellationToken).ConfigureAwait(false);
                }
                finally
                {
                    await db.DisconnectAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
Beispiel #2
0
        public static async Task DropDatabaseAsync(string connectionString, CancellationToken cancellationToken = default)
        {
            var options = new ConnectionString(connectionString);

            options.Validate();

            try
            {
                var db = new FbConnectionInternal(options);
                try
                {
                    await db.DropDatabaseAsync(cancellationToken).ConfigureAwait(false);
                }
                finally
                {
                    await db.DisconnectAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (IscException ex)
            {
                throw FbException.Create(ex);
            }
        }
Beispiel #3
0
 public ValueTask DisposeAsync()
 {
     return(new ValueTask(_connection.DisconnectAsync(CancellationToken.None)));
 }
Beispiel #4
0
        public override async Task OpenAsync(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(_connectionString))
            {
                throw new InvalidOperationException("Connection String is not initialized.");
            }
            if (!IsClosed && _state != ConnectionState.Connecting)
            {
                throw new InvalidOperationException("Connection already Open.");
            }

            try
            {
                OnStateChange(_state, ConnectionState.Connecting);

                var createdNew = default(bool);
                if (_options.Pooling)
                {
                    _innerConnection = FbConnectionPoolManager.Instance.Get(_options, out createdNew);
                }
                else
                {
                    _innerConnection = new FbConnectionInternal(_options);
                    createdNew       = true;
                }
                if (createdNew)
                {
                    try
                    {
                        await _innerConnection.ConnectAsync(cancellationToken).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException ex)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        throw new TimeoutException("Timeout while connecting.", ex);
                    }
                    catch
                    {
                        if (_options.Pooling)
                        {
                            FbConnectionPoolManager.Instance.Release(_innerConnection, false);
                        }
                        throw;
                    }
                }
                _innerConnection.SetOwningConnection(this);

                if (_options.Enlist)
                {
                    try
                    {
                        EnlistTransaction(System.Transactions.Transaction.Current);
                    }
                    catch
                    {
                        // if enlistment fails clean up innerConnection
                        await _innerConnection.DisposeTransactionAsync(cancellationToken).ConfigureAwait(false);

                        if (_options.Pooling)
                        {
                            FbConnectionPoolManager.Instance.Release(_innerConnection, true);
                        }
                        else
                        {
                            await _innerConnection.DisconnectAsync(cancellationToken).ConfigureAwait(false);

                            _innerConnection = null;
                        }

                        throw;
                    }
                }

                // Bind	Warning	messages event
                _innerConnection.Database.WarningMessage = OnWarningMessage;

                // Update the connection state
                OnStateChange(_state, ConnectionState.Open);
            }
            catch (IscException ex)
            {
                OnStateChange(_state, ConnectionState.Closed);
                throw FbException.Create(ex);
            }
            catch
            {
                OnStateChange(_state, ConnectionState.Closed);
                throw;
            }
        }