Forked from hyrmn/AzureConManagerSqlPersistenceWireupExtensions.cs
Last active
January 14, 2019 10:41
-
-
Save oguzhaneren/00a6fc2ce51526dafb8bc7ae3e1658ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReliableSqlCommand : IDbCommand | |
{ | |
private static readonly Policy RetryPolicy = Policy | |
.Handle<SqlException>(ex => ex.Number == 11001 || ex.Number == 1205) | |
.WaitAndRetry(new[] | |
{ | |
TimeSpan.FromSeconds(1), | |
TimeSpan.FromSeconds(2), | |
TimeSpan.FromSeconds(3) | |
}); | |
public ReliableSqlCommand() | |
{ | |
RealCommand = new SqlCommand(); | |
} | |
public ReliableSqlCommand(string commandText) | |
{ | |
RealCommand = new SqlCommand(commandText); | |
} | |
public ReliableSqlCommand(string commandText, SqlConnection connection) | |
{ | |
RealCommand = new SqlCommand(commandText, connection); | |
} | |
public ReliableSqlCommand(string commandText, SqlConnection connection, SqlTransaction transaction) | |
{ | |
RealCommand = new SqlCommand(commandText, connection, transaction); | |
} | |
protected SqlCommand RealCommand { get; set; } | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
public void Prepare() | |
{ | |
RealCommand.Prepare(); | |
} | |
public void Cancel() | |
{ | |
RealCommand.Cancel(); | |
} | |
public IDbDataParameter CreateParameter() | |
{ | |
return RealCommand.CreateParameter(); | |
} | |
public int ExecuteNonQuery() | |
{ | |
return RetryPolicy.Execute(() => RealCommand.ExecuteNonQuery()); | |
} | |
public IDataReader ExecuteReader() | |
{ | |
return RetryPolicy.Execute(() => RealCommand.ExecuteReader()); | |
} | |
public IDataReader ExecuteReader(CommandBehavior behavior) | |
{ | |
return RetryPolicy.Execute(() => RealCommand.ExecuteReader(behavior)); | |
} | |
public object ExecuteScalar() | |
{ | |
return RetryPolicy.Execute(() => RealCommand.ExecuteScalar()); | |
} | |
public IDbConnection Connection | |
{ | |
get { return RealCommand.Connection; } | |
set { RealCommand.Connection = value as SqlConnection; } | |
} | |
public IDbTransaction Transaction | |
{ | |
get { return RealCommand.Transaction; } | |
set { RealCommand.Transaction = value as SqlTransaction; } | |
} | |
public string CommandText | |
{ | |
get { return RealCommand.CommandText; } | |
set { RealCommand.CommandText = value; } | |
} | |
public int CommandTimeout | |
{ | |
get { return RealCommand.CommandTimeout; } | |
set { RealCommand.CommandTimeout = value; } | |
} | |
public CommandType CommandType | |
{ | |
get { return RealCommand.CommandType; } | |
set { RealCommand.CommandType = value; } | |
} | |
public IDataParameterCollection Parameters | |
{ | |
get { return RealCommand.Parameters; } | |
} | |
public UpdateRowSource UpdatedRowSource | |
{ | |
get { return RealCommand.UpdatedRowSource; } | |
set { RealCommand.UpdatedRowSource = value; } | |
} | |
private void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
RealCommand.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReliableSqlConnection : IDbConnection | |
{ | |
private static readonly Policy RetryPolicy = Policy | |
.Handle<SqlException>(ex => ex.Number == 11001) | |
.WaitAndRetry(new[] | |
{ | |
TimeSpan.FromSeconds(1), | |
TimeSpan.FromSeconds(2), | |
TimeSpan.FromSeconds(3) | |
}); | |
public ReliableSqlConnection() | |
{ | |
UnderlyingConnection = new SqlConnection(); | |
} | |
public ReliableSqlConnection(string connectionString) | |
{ | |
UnderlyingConnection = new SqlConnection(connectionString); | |
} | |
public SqlConnection UnderlyingConnection { get; set; } | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
private void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
if (UnderlyingConnection.State == ConnectionState.Open) | |
{ | |
UnderlyingConnection.Close(); | |
} | |
UnderlyingConnection.Dispose(); | |
} | |
} | |
public IDbTransaction BeginTransaction() | |
{ | |
return UnderlyingConnection.BeginTransaction(); | |
} | |
public IDbTransaction BeginTransaction(IsolationLevel il) | |
{ | |
return UnderlyingConnection.BeginTransaction(il); | |
} | |
public void Close() | |
{ | |
UnderlyingConnection.Close(); | |
} | |
public void ChangeDatabase(string databaseName) | |
{ | |
UnderlyingConnection.ChangeDatabase(databaseName); | |
} | |
public IDbCommand CreateCommand() | |
{ | |
return new ReliableSqlCommand {Connection = UnderlyingConnection}; | |
} | |
public void Open() | |
{ | |
RetryPolicy.Execute(() => | |
{ | |
if (UnderlyingConnection.State != ConnectionState.Open) | |
{ | |
UnderlyingConnection.Open(); | |
} | |
}); | |
} | |
public string ConnectionString { get { return UnderlyingConnection.ConnectionString; } set { UnderlyingConnection.ConnectionString = value; } } | |
public int ConnectionTimeout { get { return UnderlyingConnection.ConnectionTimeout; } } | |
public string Database { get { return UnderlyingConnection.Database; } } | |
public ConnectionState State { get { return UnderlyingConnection.State; } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment