Represents a transaction context.
Inheritance: IDisposable
Example #1
0
		private Task<DataSessionState> BeginSession(TransactionContext transactionContext)
		{
			if (transactionContext == null) throw new ArgumentNullException(nameof(transactionContext));

			return Task.Run(
				() =>
				{
					var state = new DataSessionState();
					WriteOperationTrace(state, "beginSession");

					WriteOperationTrace(state, "createConnection");
					state.Connection = new MockConnection { ConnectionString = $"MockDataSource.Id={this.Id},TransactionGroupName={this.TransactionGroupName}" };

					WriteOperationTrace(state, "openConnection");
					state.Connection.Open();

					if (transactionContext.Affinity != TransactionContextAffinity.NotSupported)
					{
						WriteOperationTrace(state, "beginTransaction");
						state.Transaction = state.Connection.BeginTransaction(transactionContext.IsolationLevel);
					}

					return state;
				});
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="TransactionContextCreatedEventArgs"/> class.
		/// </summary>
		/// <param name="context">The context.</param>
		public TransactionContextCreatedEventArgs(TransactionContext context)
			: base()
		{
			if (context == null) throw new ArgumentNullException(nameof(context));

			this.NewTransactionContext = context;
		}
		public static void ValidStateTransitionTest_Created_Entered(TransactionContextAffinity affinity)
		{
			using (var context = new TransactionContext(affinity))
			{
				Assert.AreEqual(TransactionContextState.Entered, context.State);
			}
		}
		public static void ValidStateTransitionTest_Entered_ToBeCommitted(TransactionContextAffinity affinity)
		{
			using (var context = new TransactionContext(affinity))
			{
				context.VoteCommit();
				Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State);
			}
		}
Example #5
0
		internal DataSession(TransactionContext context, string transactionGroupName, Func<DataSession, Task> commit, Func<DataSession, Task> rollback, Func<DataSession, Task> end)
		{
			if (context == null) throw new ArgumentNullException(nameof(context));
			if (string.IsNullOrEmpty(transactionGroupName)) throw new ArgumentNullException(nameof(transactionGroupName));
			if (commit == null) throw new ArgumentNullException(nameof(commit));
			if (rollback == null) throw new ArgumentNullException(nameof(rollback));
			if (end == null) throw new ArgumentNullException(nameof(end));

			this.TransactionContext = context;
			this.TransactionGroupName = transactionGroupName;
			this.Commit = commit;
			this.Rollback = rollback;
			this.End = end;
		}
		public static void ValidStateTransitionTest_ToBeCommitted_ToBeCommitted(TransactionContextAffinity affinity)
		{
			using (var context = new TransactionContext(affinity))
			{
				context.VoteCommit();
				Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State);

				context.StateChanged +=
					(sender, e) =>
					{
						if (e.NewState == TransactionContextState.ToBeCommitted)
							Assert.Fail("Changed event should not be raised again");
					};

				context.VoteCommit();
				Assert.AreEqual(TransactionContextState.ToBeCommitted, context.State);
			}
		}
		public static void ValidStateTransitionTest_Entered_Exited(TransactionContextAffinity affinity)
		{
			using (var context = new TransactionContext(affinity))
			{
				context.StateChanged +=
					(sender, e) =>
					{
						if (e.OldState == TransactionContextState.Entered)
						{
							Assert.AreEqual(TransactionContextState.ToBeRollbacked, context.State);
							Assert.AreEqual(TransactionContextState.ToBeRollbacked, e.NewState);
						}
						else
						{
							Assert.AreEqual(TransactionContextState.Exited, context.State);
							Assert.AreEqual(TransactionContextState.ToBeRollbacked, e.OldState);
							Assert.AreEqual(TransactionContextState.Exited, e.NewState);
						}
					};

				context.Exit();
				Assert.AreEqual(TransactionContextState.Exited, context.State);
			}
		}
		public static void InvalidStateTransitionTest_Exited_ToBeRollbacked(TransactionContextAffinity affinity)
		{
			var ex = Assert.Throws<InvalidOperationException>(
				() =>
				{
					using (var context = new TransactionContext(affinity))
					{
						context.Exit();
						Assert.AreEqual(TransactionContextState.Exited, context.State);

						context.VoteRollback();
					}
				});
		}
		public static void ValidStateTransitionTest_ToBeRollbacked_Exited(TransactionContextAffinity affinity)
		{
			using (var context = new TransactionContext(affinity))
			{
				context.VoteRollback();
				Assert.AreEqual(TransactionContextState.ToBeRollbacked, context.State);

				context.Exit();
				Assert.AreEqual(TransactionContextState.Exited, context.State);
			}
		}