Condividi tramite


Transaction.EnlistVolatile Metodo

Definizione

Integra un gestore di risorse volatile per partecipare a una transazione.

Overload

Nome Descrizione
EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

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

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Integra un gestore di risorse volatile che supporta l'ottimizzazione del commit a fase singola per partecipare a una transazione.

Commenti

I gestori di risorse volatili non possono eseguire il ripristino da un errore per completare una transazione in cui sono stati coinvolti.

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs

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

public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::IEnlistmentNotification ^ enlistmentNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile(System.Transactions.IEnlistmentNotification enlistmentNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
member this.EnlistVolatile : System.Transactions.IEnlistmentNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (enlistmentNotification As IEnlistmentNotification, enlistmentOptions As EnlistmentOptions) As Enlistment

Parametri

enlistmentNotification
IEnlistmentNotification

Oggetto che implementa l'interfaccia IEnlistmentNotification per ricevere notifiche di commit in due fasi.

enlistmentOptions
EnlistmentOptions

EnlistDuringPrepareRequired se resource manager vuole eseguire operazioni aggiuntive durante la fase di preparazione.

Valori restituiti

Oggetto Enlistment che descrive l'integrazione.

Esempio

Nell'esempio seguente viene illustrata un'implementazione dell'interfaccia IEnlistmentNotification e l'integrazione dell'oggetto come partecipante in una transazione tramite il EnlistVolatile metodo .

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();
    }
}
    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

Commenti

I gestori di risorse volatili non possono eseguire il ripristino da un errore per completare una transazione in cui sono stati coinvolti. Per ottenere un'integrazione durevole in una transazione, usare il EnlistDurable metodo .

I gestori di risorse inseriti per la partecipazione a una transazione tramite questo metodo ricevono notifiche di commit in due fasi che corrispondono ai metodi definiti nell'interfaccia IEnlistmentNotification .

Si applica a

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs
Origine:
Transaction.cs

Integra un gestore di risorse volatile che supporta l'ottimizzazione del commit a fase singola per partecipare a una transazione.

public:
 System::Transactions::Enlistment ^ EnlistVolatile(System::Transactions::ISinglePhaseNotification ^ singlePhaseNotification, System::Transactions::EnlistmentOptions enlistmentOptions);
public System.Transactions.Enlistment EnlistVolatile(System.Transactions.ISinglePhaseNotification singlePhaseNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
member this.EnlistVolatile : System.Transactions.ISinglePhaseNotification * System.Transactions.EnlistmentOptions -> System.Transactions.Enlistment
Public Function EnlistVolatile (singlePhaseNotification As ISinglePhaseNotification, enlistmentOptions As EnlistmentOptions) As Enlistment

Parametri

singlePhaseNotification
ISinglePhaseNotification

Oggetto che implementa l'interfaccia ISinglePhaseNotification che deve essere in grado di ricevere notifiche di commit a fase singola e commit in due fasi.

enlistmentOptions
EnlistmentOptions

EnlistDuringPrepareRequired se resource manager vuole eseguire operazioni aggiuntive durante la fase di preparazione.

Valori restituiti

Oggetto Enlistment che descrive l'integrazione.

Commenti

I gestori di risorse volatili non possono eseguire il ripristino da un errore per completare una transazione in cui sono stati coinvolti. Per ottenere un'integrazione durevole in una transazione, usare il EnlistDurable metodo .

È consigliabile notare che anche quando l'implementazione di Resource Manager si integra con questo metodo, non è garantito che riceva un singolo commit di fase. Il gestore delle transazioni può comunque inviare notifiche di commit in due fasi.

Si applica a