Ejemplo n.º 1
0
        /// <summary>
        /// Ensure that database connection opened. If opened connection missing, it will be opened asynchronously.
        /// </summary>
        /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
        /// <returns>Async operation task.</returns>
        public async Task EnsureConnectionAsync(CancellationToken cancellationToken = default)
        {
            if (_connection == null)
            {
                IDbConnection connection;
                if (_connectionFactory != null)
                {
                    connection = _connectionFactory();
                }
                else
                {
                    connection = DataProvider.CreateConnection(ConnectionString);
                }

                _connection = AsyncFactory.Create(connection);

                if (RetryPolicy != null)
                {
                    _connection = new RetryingDbConnection(this, _connection, RetryPolicy);
                }
            }

            if (_connection.State == ConnectionState.Closed)
            {
                try
                {
                    var task = OnBeforeConnectionOpenAsync?.Invoke(this, _connection.Connection, cancellationToken);
                    if (task != null)
                    {
                        await task;
                    }

                    await _connection.OpenAsync(cancellationToken);

                    _closeConnection = true;

                    task = OnConnectionOpenedAsync?.Invoke(this, _connection.Connection, cancellationToken);
                    if (task != null)
                    {
                        await task;
                    }
                }
                catch (Exception ex)
                {
                    if (TraceSwitch.TraceError)
                    {
                        OnTraceConnection(new TraceInfo(TraceInfoStep.Error)
                        {
                            TraceLevel     = TraceLevel.Error,
                            DataConnection = this,
                            StartTime      = DateTime.UtcNow,
                            Exception      = ex,
                            IsAsync        = true,
                        });
                    }

                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Ensure that database connection opened. If opened connection missing, it will be opened asynchronously.
        /// </summary>
        /// <param name="cancellationToken">Asynchronous operation cancellation token.</param>
        /// <returns>Async operation task.</returns>
        public async Task EnsureConnectionAsync(CancellationToken cancellationToken = default)
        {
            CheckAndThrowOnDisposed();

            try
            {
                if (_connection == null)
                {
                    DbConnection connection;
                    if (_connectionFactory != null)
                    {
                        connection = _connectionFactory();
                    }
                    else
                    {
                        connection = DataProvider.CreateConnection(ConnectionString !);
                    }

                    _connection = AsyncFactory.Create(connection);

                    if (RetryPolicy != null)
                    {
                        _connection = new RetryingDbConnection(this, _connection, RetryPolicy);
                    }
                }

                if (_connection.State == ConnectionState.Closed)
                {
                    if (_connectionInterceptor != null)
                    {
                        await _connectionInterceptor.ConnectionOpeningAsync(new (this), _connection.Connection, cancellationToken)
                        .ConfigureAwait(Configuration.ContinueOnCapturedContext);
                    }

                    await _connection.OpenAsync(cancellationToken).ConfigureAwait(Configuration.ContinueOnCapturedContext);

                    _closeConnection = true;

                    if (_connectionInterceptor != null)
                    {
                        await _connectionInterceptor.ConnectionOpenedAsync(new (this), _connection.Connection, cancellationToken)
                        .ConfigureAwait(Configuration.ContinueOnCapturedContext);
                    }
                }
            }
            catch (Exception ex)
            {
                if (TraceSwitchConnection.TraceError)
                {
                    OnTraceConnection(new TraceInfo(this, TraceInfoStep.Error, TraceOperation.Open, true)
                    {
                        TraceLevel = TraceLevel.Error,
                        StartTime  = DateTime.UtcNow,
                        Exception  = ex,
                    });
                }

                throw;
            }
        }
Ejemplo n.º 3
0
        internal async Task InitCommandAsync(CommandType commandType, string sql, DataParameter[] parameters, CancellationToken cancellationToken)
        {
            if (_connection == null)
            {
                _connection = DataProvider.CreateConnection(ConnectionString);

                if (RetryPolicy != null)
                {
                    _connection = new RetryingDbConnection(this, (DbConnection)_connection, RetryPolicy);
                }
            }

            if (_connection.State == ConnectionState.Closed)
            {
                if (_connection is RetryingDbConnection)
                {
                    await((RetryingDbConnection)_connection).OpenAsync(cancellationToken);
                }
                else
                {
                    await((DbConnection)_connection).OpenAsync(cancellationToken);
                }

                _closeConnection = true;
            }

            InitCommand(commandType, sql, parameters, null);
        }
        public async Task EnsureConnectionAsync(CancellationToken cancellationToken = default)
        {
            if (_connection == null)
            {
                _connection = DataProvider.CreateConnection(ConnectionString);

                if (RetryPolicy != null)
                {
                    _connection = new RetryingDbConnection(this, (DbConnection)_connection, RetryPolicy);
                }
            }

            if (_connection.State == ConnectionState.Closed)
            {
                try
                {
                    if (_connection is RetryingDbConnection retrying)
                    {
                        await retrying.OpenAsync(cancellationToken);
                    }
                    else
                    {
                        await((DbConnection)_connection).OpenAsync(cancellationToken);
                    }

                    _closeConnection = true;
                }
                catch (Exception ex)
                {
                    if (TraceSwitch.TraceError)
                    {
                        OnTraceConnection(new TraceInfo(TraceInfoStep.Error)
                        {
                            TraceLevel     = TraceLevel.Error,
                            DataConnection = this,
                            Exception      = ex,
                            IsAsync        = true,
                        });
                    }

                    throw;
                }
            }
        }