Export (0) Print
Expand All

DependentTransaction Class

Describes a clone of a transaction providing guarantee that the transaction cannot be committed until the application comes to rest regarding work on the transaction. This class cannot be inherited.

Namespace:  System.Transactions
Assembly:  System.Transactions (in System.Transactions.dll)

[SerializableAttribute]
public sealed class DependentTransaction : Transaction

The DependentTransaction is a clone of a Transaction object created using the DependentClone method. Its sole purpose is to allow the application to come to rest and guarantee that the transaction cannot commit while work is still being performed on the transaction (for example, on a worker thread).

When the work done within the cloned transaction is finally complete and ready to be committed, it can inform the creator of the transaction using the Complete method. Thus you can preserve the consistency and correctness of data.

The DependentCloneOption enumeration is used to determine the behavior on commit. This behavior control allows an application to come to rest, as well as provides concurrency support. For more information on how this enumeration is used, see Managing Concurrency with DependentTransaction.

The following example shows you how to create a dependent transaction.

		static void Main(string[] args)
		{
			try
			{
				using (TransactionScope scope = new TransactionScope())
				{
					// Perform transactional work here. 

					//Queue work item
					ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThread), Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete));

					//Display transaction information
					Console.WriteLine("Transaction information:");
					Console.WriteLine("ID:             {0}", Transaction.Current.TransactionInformation.LocalIdentifier);
					Console.WriteLine("status:         {0}", Transaction.Current.TransactionInformation.Status);
					Console.WriteLine("isolationlevel: {0}", Transaction.Current.IsolationLevel);

					//Call Complete on the TransactionScope based on console input
					ConsoleKeyInfo c;
					while (true)
					{
                        			Console.Write("Complete the transaction scope? [Y|N] ");
						c = Console.ReadKey();
						Console.WriteLine();

						if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
						{
							//Call complete on the scope
							scope.Complete();
							break;
						}
						else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
						{
							break;
						}
					}

				}
			}
			catch (System.Transactions.TransactionException ex)
			{
				Console.WriteLine(ex);
			}
			catch
			{
				Console.WriteLine("Cannot complete transaction");
				throw;
			}
			
		}

		private static void WorkerThread(object transaction)
		{
			//Create a DependentTransaction from the object passed to the WorkerThread
			DependentTransaction dTx = (DependentTransaction)transaction;

			//Sleep for 1 second to force the worker thread to delay
			Thread.Sleep(1000);

			//Pass the DependentTransaction to the scope, so that work done in the scope becomes part of the transaction passed to the worker thread 
			using (TransactionScope ts = new TransactionScope(dTx))
			{
				//Perform transactional work here. 

				//Call complete on the transaction scope
				ts.Complete();
			}

			//Call complete on the dependent transaction
			dTx.Complete();
		}
		static void Main(string[] args)
		{
			try
			{
				using (TransactionScope scope = new TransactionScope())
				{
					// Perform transactional work here. 

					//Queue work item
					ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThread), Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete));

					//Display transaction information
					Console.WriteLine("Transaction information:");
					Console.WriteLine("ID:             {0}", Transaction.Current.TransactionInformation.LocalIdentifier);
					Console.WriteLine("status:         {0}", Transaction.Current.TransactionInformation.Status);
					Console.WriteLine("isolationlevel: {0}", Transaction.Current.IsolationLevel);

					//Call Complete on the TransactionScope based on console input
					ConsoleKeyInfo c;
					while (true)
					{
                        			Console.Write("Complete the transaction scope? [Y|N] ");
						c = Console.ReadKey();
						Console.WriteLine();

						if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
						{
							//Call complete on the scope
							scope.Complete();
							break;
						}
						else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
						{
							break;
						}
					}

				}
			}
			catch (System.Transactions.TransactionException ex)
			{
				Console.WriteLine(ex);
			}
			catch
			{
				Console.WriteLine("Cannot complete transaction");
				throw;
			}
			
		}

		private static void WorkerThread(object transaction)
		{
			//Create a DependentTransaction from the object passed to the WorkerThread
			DependentTransaction dTx = (DependentTransaction)transaction;

			//Sleep for 1 second to force the worker thread to delay
			Thread.Sleep(1000);

			//Pass the DependentTransaction to the scope, so that work done in the scope becomes part of the transaction passed to the worker thread 
			using (TransactionScope ts = new TransactionScope(dTx))
			{
				//Perform transactional work here. 

				//Call complete on the transaction scope
				ts.Complete();
			}

			//Call complete on the dependent transaction
			dTx.Complete();
		}

System.Object
  System.Transactions.Transaction
    System.Transactions.DependentTransaction

This type is thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft