/// <summary> /// Create connection at handle set transaction if necessary /// </summary> /// <param name="connectionString"></param> /// <returns></returns> protected static ConnectionHolder CreateConnection(string connectionString) { var connection = new SqlConnection(connectionString); connection.Open(); if (Transaction.Current == null) { var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); return(ConnectionHolder.ForTransactionalWork(connection, transaction)); } return(ConnectionHolder.ForNonTransactionalWork(connection)); }
/// <summary> /// Constructs the persister with the ability to create connections to SQL Server using the specified connection string. /// This also means that the persister will manage the connection by itself, closing it when it has stopped using it. /// </summary> public SqlServerSagaPersister(string connectionString, string sagaIndexTableName, string sagaTableName) : this(sagaIndexTableName, sagaTableName) { getConnection = () => { var connection = new SqlConnection(connectionString); connection.Open(); var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); return(ConnectionHolder.ForTransactionalWork(connection, transaction)); }; commitAction = h => h.Commit(); rollbackAction = h => h.RollBack(); releaseConnection = c => c.Dispose(); }
/// <summary> /// Create connection at handle set transaction if necessary /// </summary> /// <param name="connectionString"></param> /// <returns></returns> protected static ConnectionHolder CreateConnection(string connectionStringOrConnectionStringName) { var connectionStringToUse = Rebus.Shared.ConnectionStringUtil.GetConnectionStringToUse(connectionStringOrConnectionStringName); var connection = new SqlConnection(connectionStringToUse); connection.Open(); if (Transaction.Current == null) { var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); return(ConnectionHolder.ForTransactionalWork(connection, transaction)); } return(ConnectionHolder.ForNonTransactionalWork(connection)); }
/// <summary> /// Constructs the storage with the ability to create connections to SQL Server using the specified connection string. /// This also means that the storage will manage the connection by itself, closing it when it has stopped using it. /// </summary> public SqlServerSubscriptionStorage(string connectionString, string subscriptionsTableName) { this.subscriptionsTableName = subscriptionsTableName; getConnection = () => { var connection = new SqlConnection(connectionString); connection.Open(); var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); return(ConnectionHolder.ForTransactionalWork(connection, transaction)); }; commitAction = h => h.Commit(); rollbackAction = h => h.RollBack(); releaseConnection = c => c.Dispose(); }
ConnectionHolder GetOrCreateConnection() { if (currentConnection != null) { return(currentTransaction == null ? ConnectionHolder.ForNonTransactionalWork(currentConnection) : ConnectionHolder.ForTransactionalWork(currentConnection, currentTransaction)); } var newConnection = new SqlConnection(ConnectionStrings.SqlServer); newConnection.Open(); currentConnection = newConnection; return(ConnectionHolder.ForNonTransactionalWork(newConnection)); }