Ejemplo n.º 1
0
		public FileTransaction(string name)
		{
			_Name = name;
		
			InnerBegin();

			_State = TransactionState.Active;
		}
		public FileTransaction(string name)
		{
			Contract.Requires(!string.IsNullOrEmpty(name));

			_Name = name;
			InnerBegin();

			_State = TransactionState.Active;
		}
Ejemplo n.º 3
0
		public Transaction(DependentTransaction dependent, uint stackDepth, ITransactionOptions creationOptions, Action onDispose)
		{
			Contract.Requires(creationOptions != null);
			Contract.Requires(dependent != null);
			Contract.Ensures(_State == TransactionState.Active);
			Contract.Ensures(((ITransaction)this).State == TransactionState.Active);

			_Dependent = dependent;
			_CreationOptions = creationOptions;
			_OnDispose = onDispose;
			_State = TransactionState.Active;
			_LocalIdentifier = dependent.TransactionInformation.LocalIdentifier + ":" + stackDepth;
		}
 internal InternalTransaction(Transaction outcomeSource, ITransactionPromoter promoter)
 {
     if (!TransactionManager._platformValidated)
     {
         TransactionManager.ValidatePlatform();
     }
     this.absoluteTimeout = 0x7fffffffffffffffL;
     this.outcomeSource = outcomeSource;
     this.transactionHash = TransactionManager.TransactionTable.Add(this);
     this.promoter = promoter;
     TransactionState._TransactionStateSubordinateActive.EnterState(this);
     this.promoteState = TransactionState._TransactionStateDelegatedSubordinate;
 }
 internal InternalTransaction(Transaction outcomeSource, OletxTransaction distributedTx)
 {
     if (!TransactionManager._platformValidated)
     {
         TransactionManager.ValidatePlatform();
     }
     this.promotedTransaction = distributedTx;
     this.absoluteTimeout = 0x7fffffffffffffffL;
     this.outcomeSource = outcomeSource;
     this.transactionHash = TransactionManager.TransactionTable.Add(this);
     TransactionState._TransactionStateNonCommittablePromoted.EnterState(this);
     this.promoteState = TransactionState._TransactionStateNonCommittablePromoted;
 }
 internal InternalTransaction(TimeSpan timeout, CommittableTransaction committableTransaction)
 {
     if (!TransactionManager._platformValidated)
     {
         TransactionManager.ValidatePlatform();
     }
     this.absoluteTimeout = TransactionManager.TransactionTable.TimeoutTicks(timeout);
     TransactionState._TransactionStateActive.EnterState(this);
     this.promoteState = TransactionState._TransactionStatePromoted;
     this.committableTransaction = committableTransaction;
     this.outcomeSource = committableTransaction;
     this.transactionHash = TransactionManager.TransactionTable.Add(this);
 }
Ejemplo n.º 7
0
		public Transaction(CommittableTransaction inner, uint stackDepth, ITransactionOptions creationOptions,
		                   Action onDispose)
		{
			Contract.Requires(creationOptions != null);
			Contract.Requires(inner != null);
			Contract.Ensures(_Inner != null);
			Contract.Ensures(_State == TransactionState.Active);
			Contract.Ensures(((ITransaction) this).State == TransactionState.Active);
			_Inner = inner;
			_StackDepth = stackDepth;
			_CreationOptions = creationOptions;
			_OnDispose = onDispose;
			_State = TransactionState.Active;
		}
Ejemplo n.º 8
0
		public Transaction(CommittableTransaction committable, uint stackDepth, ITransactionOptions creationOptions,
		                   Action onDispose)
		{
			Contract.Requires(creationOptions != null);
			Contract.Requires(committable != null);
			Contract.Ensures(_State == TransactionState.Active);
			Contract.Ensures(((ITransaction)this).State == TransactionState.Active);

			_Committable = committable;
			_CreationOptions = creationOptions;
			_OnDispose = onDispose;
			_State = TransactionState.Active;
			_LocalIdentifier = committable.TransactionInformation.LocalIdentifier + ":" + stackDepth;
		}
Ejemplo n.º 9
0
        // Construct an internal transaction
        internal InternalTransaction(Transaction outcomeSource, Oletx.OletxTransaction distributedTx)
        {
            if (!TransactionManager._platformValidated)
            {
                TransactionManager.ValidatePlatform();
            }

            this.promotedTransaction = distributedTx;

            this.absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            this.outcomeSource = outcomeSource;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add(this);

            // Start the transaction off as active
            TransactionState._TransactionStateNonCommittablePromoted.EnterState(this);

            // Until otherwise noted this transaction uses normal promotion.
            this.promoteState = TransactionState._TransactionStateNonCommittablePromoted;
        }
Ejemplo n.º 10
0
        // Construct an internal transaction
        internal InternalTransaction(TimeSpan timeout, CommittableTransaction committableTransaction)
        {
            if (!TransactionManager._platformValidated)
            {
                TransactionManager.ValidatePlatform();
            }

            // Calculate the absolute timeout for this transaction
            this.absoluteTimeout = TransactionManager.TransactionTable.TimeoutTicks(timeout);

            // Start the transaction off as active
            TransactionState._TransactionStateActive.EnterState(this);

            // Until otherwise noted this transaction uses normal promotion.
            this.promoteState = TransactionState._TransactionStatePromoted;

            // Keep a reference to the commitable transaction
            this.committableTransaction = committableTransaction;
            this.outcomeSource          = committableTransaction;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add(this);
        }
Ejemplo n.º 11
0
        // Construct an internal transaction
        internal InternalTransaction(Transaction outcomeSource, ITransactionPromoter promoter)
        {
            if (!TransactionManager._platformValidated)
            {
                TransactionManager.ValidatePlatform();
            }

            this.absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            this.outcomeSource = outcomeSource;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add(this);

            // Save the transaction promoter.
            this.promoter = promoter;

            // This transaction starts in a special state.
            TransactionState._TransactionStateSubordinateActive.EnterState(this);

            // This transaction promotes through delegation
            this.promoteState = TransactionState._TransactionStateDelegatedSubordinate;
        }
Ejemplo n.º 12
0
		/**
		 * Possible state changes
		 * 
		 * Default -> Constructed
		 * Constructed -> Disposed
		 * Constructed -> Active
		 * Active -> CommittedOrCompleted (depends on whether we are committable or not)
		 * Active -> InDoubt
		 * Active -> Aborted
		 * Aborted -> Disposed	# an active transaction may be disposed and then dispose must take take of aborting
		 */

		void ITransaction.Dispose()
		{
			try
			{
				try
				{
					if (_State == TransactionState.Active)
						InnerRollback();
				}
				finally
				{
					if (_OnDispose != null) _OnDispose();
					((IDisposable) this).Dispose();
				}
			}
			finally
			{
				_State = TransactionState.Disposed;
			}
		}
		private void InnerBegin()
		{
			Contract.Ensures(_State == TransactionState.Active);
			// we have a ongoing current transaction, join it!
			if (System.Transactions.Transaction.Current != null)
			{
				var ktx = (IKernelTransaction) TransactionInterop.GetDtcTransaction(System.Transactions.Transaction.Current);

				SafeKernelTransactionHandle handle;
				ktx.GetHandle(out handle);

				// even though _TransactionHandle can already contain a handle if this thread 
				// had been yielded just before setting this reference, the "safe"-ness of the wrapper should
				// not dispose the other handle which is now removed
				_TransactionHandle = handle;
				//IsAmbient = true; // TODO: Perhaps we created this item and we need to notify the transaction manager...
			}
			else _TransactionHandle = NativeMethods.createTransaction(string.Format("{0} Transaction", _Name));

			if (!_TransactionHandle.IsInvalid)
			{
				_State = TransactionState.Active;
				return;
			}

			throw new TransactionException(
				"Cannot begin file transaction. CreateTransaction failed and there's no ambient transaction.",
				LastEx());
		}
		private void AssertState(TransactionState status, string msg)
		{
			if (status != _State)
			{
				if (!string.IsNullOrEmpty(msg))
					throw new TransactionException(msg);

				throw new TransactionException(string.Format("State failure; should have been {0} but was {1}",
				                                             status, _State));
			}
		}
        private void TransactionEnded(long transactionId, TransactionState transactionState) {
            // When we get notification of a completed transaction
            // we null out the current transaction.

            if (null != _currentTransaction) {
#if DEBUG
                // Check null for case where Begin and Rollback obtained in the same message.
                if (0 != _currentTransaction.TransactionId) {
                    Debug.Assert(_currentTransaction.TransactionId == transactionId, "transaction id's are not equal!");
                }
#endif
                _currentTransaction.Completed(transactionState);
                _currentTransaction = null;
            }
        }
Ejemplo n.º 16
0
		private void Dispose(bool isManaged)
		{
			if (!isManaged)
				return;

			_Logger.Debug("disposing");

			if (_DependentTasks != null) 
				_DependentTasks.Clear();

			try
			{
				if (_OnDispose != null) 
					_OnDispose();

				Inner.Dispose();
			}
			finally
			{
				_State = TransactionState.Disposed;
			}
		}
Ejemplo n.º 17
0
		private void InnerRollback()
		{
			Contract.Ensures(_State == TransactionState.Aborted);

			try
			{
				_Logger.Info(() => string.Format("rolling back tx#{0}", _LocalIdentifier));
				Inner.Rollback();
			}
			finally
			{
				_State = TransactionState.Aborted;
			}
		}
Ejemplo n.º 18
0
		private void Dispose(bool isManaged)
		{
			if (!isManaged)
				return;

			try
			{
				Inner.Dispose();
			}
			finally
			{
				_State = TransactionState.Disposed;
			}
		}
Ejemplo n.º 19
0
        // Construct an internal transaction
        internal InternalTransaction(Transaction outcomeSource, DistributedTransaction distributedTx)
        {
            _promotedTransaction = distributedTx;

            _absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            _outcomeSource = outcomeSource;

            // Initialize the hash
            _transactionHash = TransactionManager.TransactionTable.Add(this);

            // Start the transaction off as active
            TransactionState.TransactionStateNonCommittablePromoted.EnterState(this);

            // Until otherwise noted this transaction uses normal promotion.
            _promoteState = TransactionState.TransactionStateNonCommittablePromoted;
        }
Ejemplo n.º 20
0
		private void Dispose(bool disposing)
		{
			// no unmanaged code here, just return.
			if (!disposing) return;

			if (_State == TransactionState.Disposed) return;
			// called via the Dispose() method on IDisposable, 
			// can use private object references.

			try
			{
				if (_State == TransactionState.Active)
					((ITransaction) this).Rollback();

				if (_TransactionHandle != null)
					_TransactionHandle.Dispose();

				if (_Inner != null)
					_Inner.Dispose();
			}
			finally
			{
				_State = TransactionState.Disposed;
			}
		}
        // Construct an internal transaction
        internal InternalTransaction( TimeSpan timeout, CommittableTransaction committableTransaction )
        {
            if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform();
            
            // Calculate the absolute timeout for this transaction
            this.absoluteTimeout = TransactionManager.TransactionTable.TimeoutTicks( timeout );

            // Start the transaction off as active
            TransactionState._TransactionStateActive.EnterState( this );
            
            // Until otherwise noted this transaction uses normal promotion.
            this.promoteState = TransactionState._TransactionStatePromoted;

            // Keep a reference to the commitable transaction
            this.committableTransaction = committableTransaction;
            this.outcomeSource = committableTransaction;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add( this );
        }
        // Construct an internal transaction
        internal InternalTransaction( Transaction outcomeSource, Oletx.OletxTransaction distributedTx )
        {
            if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform();

            this.promotedTransaction = distributedTx;

            this.absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            this.outcomeSource = outcomeSource;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add( this );

            // Start the transaction off as active
            TransactionState._TransactionStateNonCommittablePromoted.EnterState( this );
            
            // Until otherwise noted this transaction uses normal promotion.
            this.promoteState = TransactionState._TransactionStateNonCommittablePromoted;
        }
        // Construct an internal transaction
        internal InternalTransaction( Transaction outcomeSource, ITransactionPromoter promoter )
        {
            if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform();

            this.absoluteTimeout = long.MaxValue;

            // Store the initial creater as it will be the source of outcome events
            this.outcomeSource = outcomeSource;

            // Initialize the hash
            this.transactionHash = TransactionManager.TransactionTable.Add( this );

            // Save the transaction promoter.
            this.promoter = promoter;

            // This transaction starts in a special state.
            TransactionState._TransactionStateSubordinateActive.EnterState( this );

            // This transaction promotes through delegation
            this.promoteState = TransactionState._TransactionStateDelegatedSubordinate;
         }
Ejemplo n.º 24
0
		private void InnerRollback()
		{
			Contract.Ensures(_State == TransactionState.Aborted);

			try
			{
				Inner.Rollback();
			}
			finally
			{
				_State = TransactionState.Aborted;
			}
		}
Ejemplo n.º 25
0
		void ITransaction.Complete()
		{
			try
			{
				if (_Inner != null) _Inner.Commit();
				if (_Inner2 != null) _Inner2.Complete();

				_State = TransactionState.CommittedOrCompleted;
			}
			catch (TransactionInDoubtException e)
			{
				_State = TransactionState.InDoubt;
				throw new TransactionException("Transaction in doubt. See inner exception and help link for details",
				                               e,
				                               new Uri("http://support.microsoft.com/kb/899115/EN-US/"));
			}
			catch (TransactionAbortedException)
			{
				_State = TransactionState.Aborted;
				throw;
			}
			catch (Exception e)
			{
				InnerRollback();
				// we might have lost connection to the database
				// or any other myriad of problems might have occurred
				throw new TransactionException("unknown transaction problem, see inner exception", e,
				                               new Uri("http://stackoverflow.com/questions/224689/transactions-in-net"));
			}
		}
		void ITransaction.Rollback()
		{
			try
			{
				if (!NativeMethods.RollbackTransaction(_TransactionHandle))
					throw new TransactionException("Rollback failed.", LastEx());

				if (_Inner != null)
					_Inner.Rollback();
			}
			finally
			{
				_State = TransactionState.Aborted;
			}
		}
		private void InnerRollback()
		{
			Contract.Ensures(_State == TransactionState.Aborted);

			try
			{
				if (_Logger.IsInfoEnabled)
					_Logger.InfoFormat("rolling back tx#{0}", _LocalIdentifier);
				Inner.Rollback();
			}
			finally
			{
				_State = TransactionState.Aborted;
			}
		}
		void ITransaction.Complete()
		{
			try
			{
				if (_Inner != null && _Inner.State == TransactionState.Active)
					_Inner.Complete();

				if (!NativeMethods.CommitTransaction(_TransactionHandle))
					throw new TransactionException("Commit failed.", LastEx());

				_State = TransactionState.CommittedOrCompleted;
			}
			finally
			{
				_State = _State != TransactionState.CommittedOrCompleted
				         	? TransactionState.Aborted
				         	: TransactionState.CommittedOrCompleted;
			}
		}
Ejemplo n.º 29
0
		void ITransaction.Complete()
		{
			try
			{
				if (_Committable != null)
				{
					_Logger.Debug(() => string.Format("committing committable tx#{0}", _LocalIdentifier));

					if (beforeTopComplete != null) 
						beforeTopComplete();

					if (_DependentTasks != null && _CreationOptions.DependentOption == DependentCloneOption.BlockCommitUntilComplete)
						Task.WaitAll(_DependentTasks.ToArray()); // this might throw, and then we don't set the state to completed
					else if (_DependentTasks != null && _CreationOptions.DependentOption == DependentCloneOption.RollbackIfNotComplete)
						_DependentTasks.Do(x => x.IgnoreExceptions()).Run();

					_Committable.Commit();
				}
				else
				{
					_Logger.Debug(string.Format("completing dependent tx#{0}", _LocalIdentifier));

					_Dependent.Complete();
				}

				_State = TransactionState.CommittedOrCompleted;
			}
			catch (TransactionInDoubtException e)
			{
				_State = TransactionState.InDoubt;
				throw new TransactionException("Transaction in doubt. See inner exception and help link for details",
				                               e, new Uri("http://support.microsoft.com/kb/899115/EN-US/"));
			}
			catch (TimeoutException e)
			{
				_State = TransactionState.Aborted;
				_Logger.Warn("transaction timed out", e);
			}
			catch (TransactionAbortedException e)
			{
				_State = TransactionState.Aborted;
				_Logger.Warn("transaction aborted", e);
				throw;
			}
			catch (AggregateException e)
			{
				_State = TransactionState.Aborted;
				_Logger.Warn("dependent transactions failed, so we are not performing the rollback (as they will have notified their parent!)", e);
				throw;
			}
			catch (Exception e)
			{
				InnerRollback();

				// we might have lost connection to the database
				// or any other myriad of problems might have occurred
				throw new TransactionException("Unknown Transaction Problem. Read the Inner Exception", e,
				                               new Uri("http://stackoverflow.com/questions/224689/transactions-in-net"));
			}
		}
		private void AssertState(TransactionState state)
		{
			AssertState(state, null);
		}
Ejemplo n.º 31
0
		/**
		 * Possible state changes
		 * 
		 * Default -> Constructed
		 * Constructed -> Disposed
		 * Constructed -> Active
		 * Active -> CommittedOrCompleted (depends on whether we are committable or not)
		 * Active -> InDoubt
		 * Active -> Aborted
		 * Aborted -> Disposed	# an active transaction may be disposed and then dispose must take take of aborting
		 */

		void ITransaction.Dispose()
		{
			try
			{
				try
				{
					if (_State == TransactionState.Active)
						InnerRollback();
				}
				finally
				{
					// the question is; does committable transaction object to being disposed on exceptions?
					((IDisposable) this).Dispose(); 
				}
			}
			finally
			{
				_State = TransactionState.Disposed;
			}
		}
        private void TransactionEndedByServer(long transactionId, TransactionState transactionState) {
            // Some extra steps required when the server initiates the ending of a transaction unilaterally
            //  as opposed to the client initiating it.
            //  Basically, we have to make the delegated transaction (if there is one) aware of the situation.

            SqlDelegatedTransaction delegatedTransaction = DelegatedTransaction;
            if (null != delegatedTransaction) {
                delegatedTransaction.Transaction.Rollback();    // just to make sure...
                DelegatedTransaction = null;   // He's dead, Jim.
            }

            // Now handle the standard transaction-ended stuff.
            TransactionEnded(transactionId, transactionState);
        }