Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Transaction.EnlistVolatile Method (IEnlistmentNotification, EnlistmentOptions)

Enlists a volatile resource manager that supports two phase commit to participate in a transaction.

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

public Enlistment EnlistVolatile(
	IEnlistmentNotification enlistmentNotification,
	EnlistmentOptions enlistmentOptions
)

Parameters

enlistmentNotification
Type: System.Transactions.IEnlistmentNotification

An object that implements the IEnlistmentNotification interface to receive two phase commit notifications.

enlistmentOptions
Type: System.Transactions.EnlistmentOptions

EnlistDuringPrepareRequired if the resource manager wants to perform additional work during the prepare phase.

Return Value

Type: System.Transactions.Enlistment
An Enlistment object that describes the enlistment.

Volatile resource managers cannot recovery from failure to complete a transaction in which they were participating. To obtain a durable enlistment in a transaction, use the EnlistDurable method. For more information on volatile and durable resources, as well as how to enlist a resource, see Implementing A Resource Manager. For more information on how a resource manager responds to commit notification and prepare the commit, see Committing A Transaction In Single-Phase and Multi-Phase.

Resource managers enlisted for participation in a transaction through this method receive two phase commit notifications that correspond to the methods defined on the IEnlistmentNotification interface.

The following example shows an implementation of IEnlistmentNotification interface, as well as enlisting the object as a participant in a transaction using the EnlistVolatile method.

		static void Main(string[] args)
		{
			try
			{
				using (TransactionScope scope = new TransactionScope())
				{
				
					//Create an enlistment object
					myEnlistmentClass myElistment = new myEnlistmentClass();

					//Enlist on the current transaction with the enlistment object
					Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);

					//Perform transactional work here. 

					//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'))
						{
							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;
			}
		}

		class myEnlistmentClass : IEnlistmentNotification
		{
			public void Prepare(PreparingEnlistment preparingEnlistment)
			{
				Console.WriteLine("Prepare notification received");

				//Perform transactional work 

				//If work finished correctly, reply prepared
				preparingEnlistment.Prepared();

				// otherwise, do a ForceRollback
				preparingEnlistment.ForceRollback();
			}

			public void Commit(Enlistment enlistment)
			{
				Console.WriteLine("Commit notification received");

				//Do any work necessary when commit notification is received 

				//Declare done on the enlistment
				enlistment.Done();
			}

			public void Rollback(Enlistment enlistment)
			{
				Console.WriteLine("Rollback notification received");

				//Do any work necessary when rollback notification is received 

				//Declare done on the enlistment
				enlistment.Done();
			}

			public void InDoubt(Enlistment enlistment)
			{
				Console.WriteLine("In doubt notification received");

				//Do any work necessary when indout notification is received 
				
				//Declare done on the enlistment
				enlistment.Done();
			}
		}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft