/// <summary> /// Gets a <see cref="UnitOfWorkScopeTransaction"/> instance that can be used by a <see cref="UnitOfWorkScope"/> instance. /// </summary> /// <param name="scope">The <see cref="UnitOfWorkScope"/> instance that is requesting the transaction.</param> /// <param name="isolationLevel">One of the values of <see cref="IsolationLevel"/> that specifies the transaction isolation level.</param> /// <param name="options">One of the values of <see cref="UnitOfWorkScopeTransactionOptions"/> that specifies options for using existing /// transacitons or creating new ones.</param> /// <returns>A <see cref="UnitOfWorkScopeTransaction"/> instance.</returns> public static UnitOfWorkScopeTransaction GetTransactionForScope(UnitOfWorkScope scope, IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions options) { var useCompatibleTx = (options & UnitOfWorkScopeTransactionOptions.UseCompatible) == UnitOfWorkScopeTransactionOptions.UseCompatible; var createNewTx = (options & UnitOfWorkScopeTransactionOptions.CreateNew) == UnitOfWorkScopeTransactionOptions.CreateNew; Guard.Against <InvalidOperationException>(useCompatibleTx && createNewTx, "Cannot start a transaction with both UseCompatible and CreateNew specified " + "as a UnitOfWorkScopeTransactionOptions"); if (options == UnitOfWorkScopeTransactionOptions.UseCompatible) { var transaction = (from t in CurrentTransactions where t.IsolationLevel == isolationLevel select t).FirstOrDefault(); if (transaction != null) { transaction.AttachScope(scope); return(transaction); } } var factory = ServiceLocator.Current.GetInstance <IUnitOfWorkFactory>(); var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel); newTransaction.AttachScope(scope); CurrentTransactions.AddFirst(newTransaction); return(newTransaction); }
/// <summary> /// Overloaded Constructor. /// Creates a new instance of <see cref="UnitOfWorkScope"/> with the specified transaction isolation level, option connection and /// a transaction option that specifies if an existing transaction should be used or to create a new transaction. /// </summary> /// <param name="isolationLevel"></param> /// <param name="transactionOptions"></param> public UnitOfWorkScope(IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions transactionOptions) { _disposed = false; _autoComplete = (transactionOptions & UnitOfWorkScopeTransactionOptions.AutoComplete) == UnitOfWorkScopeTransactionOptions.AutoComplete; _currentTransaction = UnitOfWorkScopeTransaction.GetTransactionForScope(this, isolationLevel, transactionOptions); RegisterScope(this); }
/// <summary> /// Disposes off the managed and un-managed resources used. /// </summary> /// <param name="disposing"></param> private void Dispose(bool disposing) { if (!disposing) { return; } if (_disposed) { return; } if (_currentTransaction != null) { if (_autoComplete) { try { _currentTransaction.Commit(this); } catch { _currentTransaction.Rollback(this); //Continue disposing _currentTransaction = null; UnRegisterScope(this); _disposed = true; //throw; throw; } } else { _currentTransaction.Rollback(this); } _currentTransaction = null; } UnRegisterScope(this); _disposed = true; }
/// <summary> /// Gets a <see cref="UnitOfWorkScopeTransaction"/> instance that can be used by a <see cref="UnitOfWorkScope"/> instance. /// </summary> /// <param name="scope">The <see cref="UnitOfWorkScope"/> instance that is requesting the transaction.</param> /// <param name="isolationLevel">One of the values of <see cref="IsolationLevel"/> that specifies the transaction isolation level.</param> /// <param name="options">One of the values of <see cref="UnitOfWorkScopeTransactionOptions"/> that specifies options for using existing /// transacitons or creating new ones.</param> /// <returns>A <see cref="UnitOfWorkScopeTransaction"/> instance.</returns> public static UnitOfWorkScopeTransaction GetTransactionForScope(UnitOfWorkScope scope, IsolationLevel isolationLevel, UnitOfWorkScopeTransactionOptions options) { var useCompatibleTx = (options & UnitOfWorkScopeTransactionOptions.UseCompatible) == UnitOfWorkScopeTransactionOptions.UseCompatible; var createNewTx = (options & UnitOfWorkScopeTransactionOptions.CreateNew) == UnitOfWorkScopeTransactionOptions.CreateNew; Guard.Against<InvalidOperationException>(useCompatibleTx && createNewTx, "Cannot start a transaction with both UseCompatible and CreateNew specified " + "as a UnitOfWorkScopeTransactionOptions"); if (options == UnitOfWorkScopeTransactionOptions.UseCompatible) { var transaction = (from t in CurrentTransactions where t.IsolationLevel == isolationLevel select t).FirstOrDefault(); if (transaction != null) { transaction.AttachScope(scope); return transaction; } } var factory = ServiceLocator.Current.GetInstance<IUnitOfWorkFactory>(); var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel); newTransaction.AttachScope(scope); CurrentTransactions.AddFirst(newTransaction); return newTransaction; }
///<summary> /// Commits the current running transaction in the scope. ///</summary> public void Commit() { Guard.Against <ObjectDisposedException>(_disposed, "Cannot commit a disposed UnitOfWorkScope instance."); _currentTransaction.Commit(this); _currentTransaction = null; }
/// <summary> /// Disposes off the managed and un-managed resources used. /// </summary> /// <param name="disposing"></param> private void Dispose(bool disposing) { if (!disposing) return; if (_disposed) return; if (_currentTransaction != null) { if (_autoComplete) { try { _currentTransaction.Commit(this); } catch { _currentTransaction.Rollback(this); //Continue disposing _currentTransaction = null; UnRegisterScope(this); _disposed = true; //throw; throw; } } else _currentTransaction.Rollback(this); _currentTransaction = null; } UnRegisterScope(this); _disposed = true; }
///<summary> /// Commits the current running transaction in the scope. ///</summary> public void Commit() { Guard.Against<ObjectDisposedException>(_disposed, "Cannot commit a disposed UnitOfWorkScope instance."); _currentTransaction.Commit(this); _currentTransaction = null; }