IEnlistmentNotification Antarmuka

Definisi

Menjelaskan antarmuka yang harus diterapkan manajer sumber daya untuk menyediakan panggilan balik pemberitahuan penerapan dua fase untuk manajer transaksi setelah mendaftar untuk partisipasi.

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
Turunan

Contoh

Contoh berikut menunjukkan implementasi antarmuka ini, serta mendaftarkan objek sebagai peserta dalam transaksi menggunakan EnlistVolatile metode .

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

Keterangan

Agar manajer sumber daya dapat berpartisipasi dalam transaksi, manajer sumber daya harus mendaftar dalam transaksi melalui manajer transaksi. Kelas Transaction mendefinisikan sekumpulan metode yang namanya dimulai dengan Enlist yang menyediakan fungsionalitas ini. Metode yang berbeda Enlist sesuai dengan berbagai jenis pendaftaran yang mungkin dimiliki oleh manajer sumber daya.

Kelas ini menjelaskan antarmuka yang harus diterapkan manajer sumber daya untuk menyediakan panggilan balik pemberitahuan penerapan dua fase untuk manajer transaksi setelah mendaftar untuk partisipasi. Untuk implementasi IEnlistmentNotification setiap antarmuka resource manager, Anda harus mendaftarkannya menggunakan EnlistVolatile metode atau EnlistDurable metode Transaction kelas, tergantung pada apakah sumber daya Anda volatil atau tahan lama. Untuk informasi selengkapnya tentang pendaftaran dan 2PC, lihat Mendaftarkan Sumber Daya sebagai Peserta dalam Transaksi dan Melakukan Transaksi dalam Single-Phase dan Multi-Fase masing-masing .

Manajer transaksi memberi tahu objek yang terdaftar pada fase yang berbeda dari Protokol Penerapan Dua Fase dengan metode berikut.

Metode Deskripsi
Prepare Metode objek terdaftar ini digunakan sebagai panggilan balik oleh Manajer Transaksi selama fase pertama transaksi, ketika manajer transaksi bertanya kepada peserta apakah mereka dapat melakukan transaksi.
Commit Metode objek terdaftar ini digunakan sebagai panggilan balik oleh Manajer Transaksi selama fase kedua transaksi jika transaksi dilakukan.
Rollback Metode objek terdaftar ini digunakan sebagai panggilan balik oleh Manajer Transaksi selama fase kedua transaksi jika transaksi dibatalkan (yaitu, digulung balik).
InDoubt Metode objek terdaftar ini digunakan sebagai panggilan balik oleh Manajer Transaksi selama fase kedua transaksi jika transaksi ragu.

Note

Anda harus mengetahui bahwa pemberitahuan mungkin tidak dikirim secara berurutan, atau dalam urutan tertentu.

Metode

Nama Deskripsi
Commit(Enlistment)

Memberi tahu objek yang terdaftar bahwa transaksi sedang dilakukan.

InDoubt(Enlistment)

Memberi tahu objek yang terdaftar bahwa status transaksi ragu.

Prepare(PreparingEnlistment)

Memberi tahu objek yang terdaftar bahwa transaksi sedang disiapkan untuk komitmen.

Rollback(Enlistment)

Memberi tahu objek yang terdaftar bahwa transaksi sedang digulung balik (dibatalkan).

Berlaku untuk

Lihat juga