System.Data.Common.DbConnection.OpenAsync(System.TimeSpan, int)

Here are the examples of the csharp api class System.Data.Common.DbConnection.OpenAsync(System.TimeSpan, int) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Example 7

1. Example

Project: Alluvial
Source File: DbConnectionExtensions.cs
View license
public static async Task OpenAsync(
            this DbConnection connection,
            TimeSpan backoff,
            int numberOfRetries = 5)
        {
            if (connection.State == ConnectionState.Open)
            {
                return;
            }

            var attempt = 1;

            while (attempt++ <= numberOfRetries)
            {
                try
                {
                    await connection.OpenAsync();
                    return;
                }
                catch (InvalidOperationException exception)
                {
                    if (!exception.Message.Contains("The timeout period elapsed prior to obtaining a connection from the pool."))
                    {
                        throw;
                    }
                }

                var delay = TimeSpan.FromMilliseconds(backoff.TotalMilliseconds*attempt);

                Debug.WriteLine($"[SqlConnection] backing off on connection pool acquire: {delay.TotalSeconds}s");

                await Task.Delay(delay);
            }
        }