-
Notifications
You must be signed in to change notification settings - Fork 145
ReliableConnection and Cloud Databases
Cloud database providers such as SQL Azure introduce some additional complexity for database connections. SQL Azure connections can have temporary errors that can cause connections to fail. The SQL Azure team has recommended that retry operations be supported in order to handle transient errors.
See Best Practices for Handling Transient Conditions for more information.
Insight makes this process much simpler by introducing the ReliableConnection class. It automatically handles retries for transient connection errors, in both synchronous and asynchronous scenarios.
Just replace your SqlConnection
with ReliableConnection<SqlConnection>
.
using Insight.Database.Reliable;
var connection = new ReliableConnection<SqlConnection>(connectionString);
var results = connection.Query<Beer>("FindBeer", new { Name = "IPA" });
You also have access to the parameters of the RetryStrategy.
RetryStrategy.Default.MaxRetryCount = 5;
RetryStrategy.Default.MinBackOff = new TimeSpan (0, 0, 0, 0, 100); // 100 ms
RetryStrategy.Default.MaxBackOff = new TimeSpan (0, 0, 0, 1, 0); // 1 s
RetryStrategy.Default.IncrementalBackOff = new TimeSpan (0, 0, 0, 0, 100); // 100 ms
You can also hook the Retrying event on a per-strategy basis:
RetryStrategy.Retrying += (sender, re) =>
{
Console.WriteLine("Retrying. Attempt {0}", re.Attempt);
};
You can create multiple RetryStrategies and pass them into your ReliableConnection if you need different ones.
var strategy = new RetryStrategy();
var connection = new ReliableConnection<SqlConnection>(connectionString, strategy);
For more advanced retry strategies, you could implement your own IRetryStrategy, but it's pretty complicated.
The Microsoft Enterprise Library team has a good Application Block that has a lot of options and works across SQL Azure, Azure Service Bus, Azure Storage and Azure Caching. This gives you consistent retry logic across the different types of resources.
You can integrate the Application Block by adding an adapter between IRetryStrategy and the Application Block RetryManager class.
- Home
- About
- Getting Started
- Connections
- Execute
- Getting Results
- Advanced Results & Mapping
- Insert/Update Considerations
- Data Types
- Write Even Less Code
- Performance & Speed
- Other Topics
- Supported Databases
- Working with the Code