Inheritance: ISinglePhaseNotification, IDisposable
Esempio n. 1
0
			internal TransactionExecutionContext(TransactionContext context)
			{
				this.TransactionContext = context;

				if (context.executionVersionNesting == 0)
				{
					context.executionVersion++;

					this.TransactionContext.dataAccessModel.AsyncLocalExecutionVersion = context.executionVersion;
				}

				this.Version = context.executionVersion;

				context.executionVersionNesting++;
			}
Esempio n. 2
0
		private static TransactionContext GetOrCreateCurrent(DataAccessModel dataAccessModel, bool forWrite, bool createTransactionIfNotExist)
		{
			TransactionContext context;
			var dataAccessTransaction = DataAccessTransaction.Current;

			if (dataAccessTransaction == null && Transaction.Current != null)
			{
				dataAccessTransaction = DataAccessTransaction.Current = new DataAccessTransaction(DataAccessIsolationLevel.Unspecified);
			}

			if (dataAccessTransaction == null)
			{
				if (forWrite)
				{
					throw new InvalidOperationException("Write operation must be performed inside a scope");
				}

				context = dataAccessModel.AsyncLocalAmbientTransactionContext;

				if (context == null || context.disposed)
				{
					if (!createTransactionIfNotExist)
					{
						if (context != null)
						{
							dataAccessModel.AsyncLocalAmbientTransactionContext = null;
						}

						return null;
					}

					context = new TransactionContext(null, dataAccessModel);

					dataAccessModel.AsyncLocalAmbientTransactionContext = context;
				}

				if (context.currentlyCommiting)
				{
					throw new InvalidOperationException("The context is currently committing");
				}

				return context;
			}
			
			if (dataAccessTransaction.systemTransactionCompleted && dataAccessTransaction.systemTransactionStatus == TransactionStatus.Aborted)
			{
				throw new TransactionAbortedException();
			}

			if (dataAccessTransaction.SystemTransaction?.TransactionInformation.Status == TransactionStatus.Aborted)
			{
				throw new TransactionAbortedException();
			}

			var contexts = dataAccessTransaction.transactionContextsByDataAccessModel;

			var skipTest = false;

			if (contexts == null)
			{
				skipTest = true;
				contexts = dataAccessTransaction.transactionContextsByDataAccessModel = new Dictionary<DataAccessModel, TransactionContext>();
			}

			if (skipTest || !contexts.TryGetValue(dataAccessModel, out context))
			{
				context = new TransactionContext(dataAccessTransaction, dataAccessModel);
				contexts[dataAccessModel] = context;

				dataAccessTransaction.AddTransactionContext(context);
			}

			if (context.currentlyCommiting)
			{
				throw new InvalidOperationException("The context is currently committing");
			}

			return context;
		}
        public virtual bool TryGetCurrentContext(bool forWrite, out TransactionContext retval)
        {
            Transaction transaction;

            try
            {
                transaction = Transaction.Current;
            }
            catch (InvalidOperationException)
            {
                transaction = Transaction.Current = null;
            }

            if (transaction == null)
            {
                transaction = currentlyCommitingTransaction;
            }

            if (transaction == null && forWrite)
            {
                retval = null;

                return false;
            }

            if (transaction != null)
            {
                if (transaction.TransactionInformation.Status == TransactionStatus.Aborted)
                {
                    throw new TransactionAbortedException();
                }

                if (!this.transactionContextsByTransaction.TryGetValue(transaction, out retval))
                {
                    retval = new TransactionContext(this.DataAccessModel, transaction);

                    transaction.TransactionCompleted += delegate
                    {
                        transactionContextsByTransaction.Remove(transaction);
                    };

                    transaction.EnlistVolatile(retval, EnlistmentOptions.None);
                    this.transactionContextsByTransaction[transaction] = retval;
                }
            }
            else
            {
                if (this.rootContext == null)
                {
                    retval = new TransactionContext(this.DataAccessModel, null);

                    this.rootContext = retval;
                }
                else
                {
                    retval = this.rootContext;
                }
            }

            return true;
        }
 public DatabaseTransactionContextAcquisition(TransactionContext transactionContext, SqlDatabaseContext sqlDatabaseContext, SqlTransactionalCommandsContext sqlDatabaseCommandsContext)
 {
     this.TransactionContext = transactionContext;
     this.SqlDatabaseContext = sqlDatabaseContext;
     this.SqlDatabaseCommandsContext = sqlDatabaseCommandsContext;
 }
Esempio n. 5
0
		public static TransactionContext GetCurrentContext(DataAccessModel dataAccessModel, bool forWrite)
		{
			TransactionContext context;
			var dataAccessTransaction = DataAccessTransaction.Current;
			
			if (dataAccessTransaction == null && Transaction.Current != null)
			{
				dataAccessTransaction = DataAccessTransaction.Current = new DataAccessTransaction(DataAccessIsolationLevel.Unspecified);
			}

			if (dataAccessTransaction == null)
			{
				if (forWrite)
				{
					throw new InvalidOperationException("Write operation must be performed inside a scope");
				}

				context = dataAccessModel.AsyncLocalTransactionContext;

				if (context == null || context.disposed)
				{
					context = new TransactionContext(null, dataAccessModel);

					dataAccessModel.AsyncLocalTransactionContext = context;
				}

				return context;
			}

			if (dataAccessTransaction.SystemTransaction?.TransactionInformation.Status == TransactionStatus.Aborted)
			{
				throw new TransactionAbortedException();
			}

			var contexts = dataAccessTransaction.transactionContextsByDataAccessModel;

			var skipTest = false;
			
			if (contexts == null)
			{
				skipTest = true;
				contexts = dataAccessTransaction.transactionContextsByDataAccessModel = new Dictionary<DataAccessModel, TransactionContext>();
			}

			if (skipTest || !contexts.TryGetValue(dataAccessModel, out context))
			{
				context = new TransactionContext(dataAccessTransaction, dataAccessModel);
				contexts[dataAccessModel] = context;
				dataAccessModel.AsyncLocalTransactionContext = context;

				dataAccessTransaction.AddTransactionContext(context);
			}

			return context;
		}