DependentTransaction Classe

Definizione

Descrive un duplicato di una transazione che garantisce che non può essere eseguito il commit della transazione fino a quando nell'applicazione non si completano le operazioni relative alla transazione. La classe non può essere ereditata.

public ref class DependentTransaction sealed : System::Transactions::Transaction
public sealed class DependentTransaction : System.Transactions.Transaction
[System.Serializable]
public sealed class DependentTransaction : System.Transactions.Transaction
type DependentTransaction = class
    inherit Transaction
[<System.Serializable>]
type DependentTransaction = class
    inherit Transaction
Public NotInheritable Class DependentTransaction
Inherits Transaction
Ereditarietà
DependentTransaction
Attributi

Esempio

Nell'esempio seguente viene illustrato come creare una transazione dipendente.

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

Commenti

DependentTransaction è un clone di un Transaction oggetto creato usando il DependentClone metodo . L'unico scopo è consentire all'applicazione di rimanere inattivi e garantire che la transazione non possa eseguire il commit mentre il lavoro è ancora in esecuzione sulla transazione (ad esempio, in un thread di lavoro).

Quando il lavoro svolto all'interno della transazione clonata viene finalmente completato e pronto per il commit, può informare l'autore della transazione usando il Complete metodo . È quindi possibile mantenere la coerenza e la correttezza dei dati.

L'enumerazione DependentCloneOption viene utilizzata per determinare il comportamento al commit. Questo controllo del comportamento consente a un'applicazione di venire a riposo, oltre a fornire supporto per la concorrenza. Per altre informazioni sull'uso di questa enumerazione, vedere Managing Concurrency with DependentTransaction .For more information on how this enumeration is used, see Managing Concurrency with DependentTransaction.

Proprietà

IsolationLevel

Ottiene il livello di isolamento della transazione.

(Ereditato da Transaction)
PromoterType

Identifica in modo univoco il formato di byte[] restituito dal metodo Promote quando la transazione viene promossa.

(Ereditato da Transaction)
TransactionInformation

Recupera informazioni aggiuntive su una transazione.

(Ereditato da Transaction)

Metodi

Clone()

Crea un duplicato della transazione.

(Ereditato da Transaction)
Complete()

Tenta di completare la transazione dipendente.

DependentClone(DependentCloneOption)

Crea un duplicato dipendente della transazione.

(Ereditato da Transaction)
Dispose()

Rilascia le risorse contenute nell'oggetto.

(Ereditato da Transaction)
EnlistDurable(Guid, IEnlistmentNotification, EnlistmentOptions)

Integra una gestione risorse durature che supporta il commit a due fasi per la partecipazione a una transazione.

(Ereditato da Transaction)
EnlistDurable(Guid, ISinglePhaseNotification, EnlistmentOptions)

Integra una gestione risorse durature che supporta l'ottimizzazione del commit a una fase per la partecipazione a una transazione.

(Ereditato da Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification)

Inserisce un gestore risorse con una transazione interna che usa un'integrazione PSPE (Promotable Single Phase Enlistment).

(Ereditato da Transaction)
EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification, Guid)

Inserisce un gestore risorse con una transazione interna che usa un'integrazione PSPE (Promotable Single Phase Enlistment).

(Ereditato da Transaction)
EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Integra un gestore di risorse volatili che supporta il commit a due fasi per partecipare a una transazione.

(Ereditato da Transaction)
EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Integra una gestione risorse volatili che supporta l'ottimizzazione del commit a una fase per la partecipazione a una transazione.

(Ereditato da Transaction)
Equals(Object)

Determina se questa transazione e l'oggetto specificato sono uguali.

(Ereditato da Transaction)
GetHashCode()

Restituisce il codice hash per l'istanza.

(Ereditato da Transaction)
GetPromotedToken()

Ottiene l'oggetto byte[] restituito dal Promote metodo quando la transazione viene alzata di livello.

(Ereditato da Transaction)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
PromoteAndEnlistDurable(Guid, IPromotableSinglePhaseNotification, ISinglePhaseNotification, EnlistmentOptions)

Promuove e inserisce una gestione risorse duratura che supporta il commit a due fasi per la partecipazione a una transazione.

(Ereditato da Transaction)
Rollback()

Esegue il rollback (interruzione) della transazione.

(Ereditato da Transaction)
Rollback(Exception)

Esegue il rollback (interruzione) della transazione.

(Ereditato da Transaction)
SetDistributedTransactionIdentifier(IPromotableSinglePhaseNotification, Guid)

Imposta l'identificatore di transazione distribuita generato dal promotore non MSDTC.

(Ereditato da Transaction)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Eventi

TransactionCompleted

Indica che la transazione è stata completata.

(Ereditato da Transaction)

Implementazioni dell'interfaccia esplicita

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Ottiene un oggetto SerializationInfo con i dati richiesti per la serializzazione di questa transazione.

(Ereditato da Transaction)

Si applica a

Thread safety

Questo tipo è thread-safe.

Vedi anche