Export (0) Print
Expand All

IEnlistmentNotification Interface

Describes an interface that a resource manager should implement to provide two phase commit notification callbacks for the transaction manager upon enlisting for participation.

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

'Declaration
Public Interface IEnlistmentNotification
'Usage
Dim instance As IEnlistmentNotification

In order for a resource manager to participate in a transaction, it must enlist in the transaction through the transaction manager. The Transaction class defines a set of methods whose names begin with Enlist that provide this functionality. The different Enlist methods correspond to the different types of enlistment that a resource maanger may have.

This class describes an interface that a resource manager should implement to provide two phase commit notification callbacks for the transaction manager upon enlisting for participation. For each resource manager's implementation of the IEnlistmentNotification interface, you should enlist it using the EnlistVolatile method or the EnlistDurable method of the Transaction class, depending on whether your resource is volatile or durable. For more information on enlistment and 2PC, see Enlisting Resources As Participants In A Transaction and Committing A Transaction In Single-Phase and Multi-Phase respectively.

The transaction manager notifies the enlisted object at different phases of the Two Phase Commit Protocol by the following methods.

Method

Description

Prepare

This method of an enlisted object is used as a callback by the Transaction Manager during the first phase of a transaction, when the transaction manager asks participants whether they can commit the transaction.

Commit

This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is commited.

Rollback

This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is aborted (that is, rolled back).

InDoubt

This method of an enlisted object is used as a callback by the Transaction Manager during the second phase of a transaction if the transaction is in doubt.

NoteNote:

You should be aware that notifications might not be sent sequentially, or in a particular order.

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

Public Shared Sub Main()
			Try 
				Using scope As TransactionScope = New TransactionScope()

					'Create an enlistment object 
					Dim myEnlistmentClass As New EnlistmentClass

					'Enlist on the current transaction with the enlistment object
					Transaction.Current.EnlistVolatile(myEnlistmentClass, EnlistmentOptions.None)

					'Perform transactional work here. 

					'Call complete on the TransactionScope based on console input 
					Dim c As ConsoleKeyInfo
					While (True)
						Console.Write("Complete the transaction scope? [Y|N] ")
						c = Console.ReadKey()
						Console.WriteLine()
						If (c.KeyChar = "Y") Or (c.KeyChar = "y") Then
							scope.Complete()
							Exit While 
						ElseIf ((c.KeyChar = "N") Or (c.KeyChar = "n")) Then 
							Exit While 
						End If 
					End While 
				End Using 
			Catch ex As TransactionException
				Console.WriteLine(ex)
			Catch
				Console.WriteLine("Cannot complete transaction")
				Throw 
			End Try 
		End Sub 
	End Class 

	Public Class EnlistmentClass
		Implements IEnlistmentNotification

		Public Sub Prepare(ByVal myPreparingEnlistment As PreparingEnlistment) Implements System.Transactions.IEnlistmentNotification.Prepare
			Console.WriteLine("Prepare notification received")

			'Perform transactional work 
	
			'If work finished correctly, reply with prepared
			myPreparingEnlistment.Prepared()
		End Sub 

		Public Sub Commit(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Commit
			Console.WriteLine("Commit notification received")

			'Do any work necessary when commit notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 

		Public Sub Rollback(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Rollback
			Console.WriteLine("Rollback notification received")

			'Do any work necessary when rollback notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 

		Public Sub InDoubt(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.InDoubt
			Console.WriteLine("In doubt notification received")

			'Do any work necessary when indout notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 
	End Class
Public Shared Sub Main()
			Try 
				Using scope As TransactionScope = New TransactionScope()

					'Create an enlistment object 
					Dim myEnlistmentClass As New EnlistmentClass

					'Enlist on the current transaction with the enlistment object
					Transaction.Current.EnlistVolatile(myEnlistmentClass, EnlistmentOptions.None)

					'Perform transactional work here. 

					'Call complete on the TransactionScope based on console input 
					Dim c As ConsoleKeyInfo
					While (True)
						Console.Write("Complete the transaction scope? [Y|N] ")
						c = Console.ReadKey()
						Console.WriteLine()
						If (c.KeyChar = "Y") Or (c.KeyChar = "y") Then
							scope.Complete()
							Exit While 
						ElseIf ((c.KeyChar = "N") Or (c.KeyChar = "n")) Then 
							Exit While 
						End If 
					End While 
				End Using 
			Catch ex As TransactionException
				Console.WriteLine(ex)
			Catch
				Console.WriteLine("Cannot complete transaction")
				Throw 
			End Try 
		End Sub 
	End Class 

	Public Class EnlistmentClass
		Implements IEnlistmentNotification

		Public Sub Prepare(ByVal myPreparingEnlistment As PreparingEnlistment) Implements System.Transactions.IEnlistmentNotification.Prepare
			Console.WriteLine("Prepare notification received")

			'Perform transactional work 
	
			'If work finished correctly, reply with prepared
			myPreparingEnlistment.Prepared()
		End Sub 

		Public Sub Commit(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Commit
			Console.WriteLine("Commit notification received")

			'Do any work necessary when commit notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 

		Public Sub Rollback(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Rollback
			Console.WriteLine("Rollback notification received")

			'Do any work necessary when rollback notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 

		Public Sub InDoubt(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.InDoubt
			Console.WriteLine("In doubt notification received")

			'Do any work necessary when indout notification is received 

			'Declare done on the enlistment
			myEnlistment.Done()
		End Sub 
	End Class

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