DependentTransaction DependentTransaction DependentTransaction DependentTransaction Class

定義

描述交易的複製品 (Clone),其可保證在交易上執行工作時,必須等到應用程式停止後才能認可交易。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.

public ref class DependentTransaction sealed : System::Transactions::Transaction, System::Runtime::Serialization::ISerializable
[System.Serializable]
public sealed class DependentTransaction : System.Transactions.Transaction, System.Runtime.Serialization.ISerializable
type DependentTransaction = class
    inherit Transaction
    interface ISerializable
Public NotInheritable Class DependentTransaction
Inherits Transaction
Implements ISerializable
繼承
DependentTransactionDependentTransactionDependentTransactionDependentTransaction
屬性
實作

範例

下列範例會示範如何建立相依的交易。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();
}
Public Shared Sub Main()
	Try
		Using scope As TransactionScope = New TransactionScope()

			'Perform transactional work here.

			'Queue work item
			ThreadPool.QueueUserWorkItem(AddressOf 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
			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

Public Shared Sub WorkerThread(ByVal myTransaction As Object)

	'Create a DependentTransaction from the object passed to the WorkerThread
	Dim dTx As DependentTransaction
	dTx = CType(myTransaction, DependentTransaction)

	'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 ts As TransactionScope = New TransactionScope(dTx)
		'Perform transactional work here.

		'Call complete on the transaction scope
		ts.Complete()
	End Using

	'Call complete on the dependent transaction
	dTx.Complete()
End Sub

備註

DependentTransaction是的複製品Transaction使用所建立的物件DependentClone方法。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).

當複製的交易內完成的工作最後是完成並準備好認可時,它可以通知使用者的建立者的交易使用Complete方法。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.

DependentCloneOption列舉型別用來決定在認可的行為。The DependentCloneOption enumeration is used to determine the behavior on commit. 此行為的控制項可讓應用程式到 rest,以及提供並行支援。This behavior control allows an application to come to rest, as well as provides concurrency support. 如需有關如何使用這個列舉的詳細資訊,請參閱 < DependentTransaction 管理並行存取For more information on how this enumeration is used, see Managing Concurrency with DependentTransaction.

方法

BeginCommitInternal(AsyncCallback) BeginCommitInternal(AsyncCallback) BeginCommitInternal(AsyncCallback) BeginCommitInternal(AsyncCallback) Inherited from Transaction
Clone() Clone() Clone() Clone()

建立交易的複製品 (Clone)。Creates a clone of the transaction.

(Inherited from Transaction)
Complete() Complete() Complete() Complete()

嘗試完成相依交易。Attempts to complete the dependent transaction.

DependentClone(DependentCloneOption) DependentClone(DependentCloneOption) DependentClone(DependentCloneOption) DependentClone(DependentCloneOption)

建立交易的相依複製品。Creates a dependent clone of the transaction.

(Inherited from Transaction)
Dispose() Dispose() Dispose() Dispose()

釋放該物件所持有的資源。Releases the resources that are held by the object.

(Inherited from Transaction)
EndCommitInternal(IAsyncResult) EndCommitInternal(IAsyncResult) EndCommitInternal(IAsyncResult) EndCommitInternal(IAsyncResult) Inherited from Transaction
EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions) EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions) EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions) EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions)

登記支援兩階段交易認可 (Two-Phase Commit) 的長期資源管理員以參與交易。Enlists a durable resource manager that supports two phase commit to participate in a transaction.

(Inherited from Transaction)
EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions) EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions) EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions) EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions)

登記支援單一階段交易認可最佳化的長期資源管理員以參與交易。Enlists a durable resource manager that supports single phase commit optimization to participate in a transaction.

(Inherited from Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification)

使用可提升單一階段登記 (PSPE) 來登記具有內部交易的資源管理員。Enlists a resource manager that has an internal transaction using a promotable single phase enlistment (PSPE).

(Inherited from Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid) EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid)

使用可提升單一階段登記 (PSPE) 來登記具有內部交易的資源管理員。Enlists a resource manager that has an internal transaction using a promotable single phase enlistment (PSPE).

(Inherited from Transaction)
EnlistVolatile(IEnlistmentNotification, EnlistmentOptions) EnlistVolatile(IEnlistmentNotification, EnlistmentOptions) EnlistVolatile(IEnlistmentNotification, EnlistmentOptions) EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登錄暫時性資源管理員,支援兩階段交易認可參與交易。Enlists a volatile resource manager that supports two phase commit to participate in a transaction.

(Inherited from Transaction)
EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions) EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions) EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions) EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登記支援單一階段交易認可最佳化的暫時性 (Volatile) 資源管理員以參與交易。Enlists a volatile resource manager that supports single phase commit optimization to participate in a transaction.

(Inherited from Transaction)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷這個交易和指定的物件是否相等。Determines whether this transaction and the specified object are equal.

(Inherited from Transaction)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(Inherited from Transaction)
GetPromotedToken() GetPromotedToken() GetPromotedToken() GetPromotedToken()

當此交易升級時,取得 Promote 方法傳回的 byte[]Gets the byte[] returned by the Promote method when the transaction is promoted.

(Inherited from Transaction)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions) PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions) PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions) PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions)

提升和登記支援兩階段交易認可 (Two-Phase Commit) 的長期資源管理員以參與異動。Promotes and enlists a durable resource manager that supports two phase commit to participate in a transaction.

(Inherited from Transaction)
Rollback() Rollback() Rollback() Rollback()

復原 (中止) 交易。Rolls back (aborts) the transaction.

(Inherited from Transaction)
Rollback(Exception) Rollback(Exception) Rollback(Exception) Rollback(Exception)

復原 (中止) 交易。Rolls back (aborts) the transaction.

(Inherited from Transaction)
SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid) SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid) SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid) SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid)

設定非 MSDTC Promoter 所產生的分散式異動識別碼。Sets the distributed transaction identifier generated by the non-MSDTC promoter.

(Inherited from Transaction)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

屬性

IsolationLevel IsolationLevel IsolationLevel IsolationLevel

取得交易的隔離等級 (Isolation Level)。Gets the isolation level of the transaction.

(Inherited from Transaction)
PromoterType PromoterType PromoterType PromoterType

可唯一識別 byte[] 的格式,其由 Promote 方法在交易升級時傳回。Uniquely identifies the format of the byte[] returned by the Promote method when the transaction is promoted.

(Inherited from Transaction)
TransactionInformation TransactionInformation TransactionInformation TransactionInformation

擷取交易的其他資訊。Retrieves additional information about a transaction.

(Inherited from Transaction)

事件

TransactionCompleted TransactionCompleted TransactionCompleted TransactionCompleted

指出交易已完成。Indicates that the transaction is completed.

(Inherited from Transaction)

適用於

執行緒安全性

此型別具備執行緒安全。This type is thread safe.

另請參閱