Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Last active January 29, 2017 20:53
Show Gist options
  • Save hyrmn/fd56499b1e6ca02f1286 to your computer and use it in GitHub Desktop.
Save hyrmn/fd56499b1e6ca02f1286 to your computer and use it in GitHub Desktop.
public static class AzureConManagerSqlPersistenceWireupExtensions
{
public static SqlPersistenceWireup UseInAzureConfiguredSqlPersistence(this Wireup wireup, string connectionName)
{
var factory = new AzureConfigurationConnectionFactory(connectionName);
return new SqlPersistenceWireup(wireup, factory);
}
}
public class AzureConfigurationConnectionFactory : ConfigurationConnectionFactory
{
private const string providerType = "System.Data.SqlClient";
public AzureConfigurationConnectionFactory(string connectionName)
: base(connectionName)
{
}
protected override ConnectionStringSettings GetConnectionStringSettings(string connectionName)
{
var connectionString = TypedCloudConfigurationManager.GetSetting(connectionName);
return new ConnectionStringSettings(connectionName, connectionString, providerType);
}
protected override IDbConnection Open(string connectionString, ConnectionStringSettings setting)
{
var connection = new ReliableSqlConnection(connectionString);
connection.Open();
return connection;
}
}
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();
}
}
}
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; } }
}
@hyrmn
Copy link
Author

hyrmn commented Sep 17, 2015

Usage in NES wireup

eventStore = NEventStore.Wireup.Init() .UseInAzureConfiguredSqlPersistence("sqlDb.connStr.standard.eventStore")
//....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment