Menyinkronkan Langganan Push

Berlaku untuk:SQL ServerAzure SQL Database

Topik ini menjelaskan cara menyinkronkan langganan push di SQL Server dengan menggunakan SQL Server Management Studio, agen replikasi, atau Objek Manajemen Replikasi (RMO).

Catatan

Azure SQL Managed Instance dapat menjadi penerbit, distributor, dan pelanggan untuk rekam jepret dan replikasi transaksional. Database di Azure SQL Database hanya dapat menjadi pelanggan pendorongan untuk rekam jepret dan replikasi transaksional. Untuk informasi selengkapnya, lihat Replikasi transaksional dengan Azure SQL Database dan Azure SQL Managed Instance.

Menggunakan SQL Server Management Studio

Langganan disinkronkan oleh Agen Distribusi (untuk rekam jepret dan replikasi transaksional) atau Agen Penggabungan (untuk replikasi penggabungan). Agen dapat berjalan terus menerus, berjalan sesuai permintaan, atau berjalan sesuai jadwal. Untuk informasi selengkapnya tentang menentukan jadwal sinkronisasi, lihat Menentukan Jadwal Sinkronisasi.

Sinkronkan langganan sesuai permintaan dari folder Publikasi Lokal dan Langganan Lokal di Microsoft SQL Server Management Studio dan tab Semua Langganan di Monitor Replikasi. Langganan ke publikasi Oracle tidak dapat disinkronkan sesuai permintaan dari Pelanggan. Untuk informasi tentang memulai Monitor Replikasi, lihat Memulai Monitor Replikasi.

Untuk menyinkronkan langganan push sesuai permintaan di Management Studio (di Publisher)

  1. Koneksi ke Publisher di Management Studio, lalu perluas simpul server.

  2. Perluas folder Replikasi, lalu perluas folder Publikasi Lokal.

  3. Perluas publikasi yang ingin Anda sinkronkan langganannya.

  4. Klik kanan langganan yang ingin Anda sinkronkan, lalu klik Tampilkan Status Sinkronisasi.

  5. Dalam kotak dialog Tampilkan Status Sinkronisasi - <Pelanggan>:<LanggananDatabase> , klik Mulai. Ketika sinkronisasi selesai, pesan Sinkronisasi selesai ditampilkan.

  6. Klik Tutup.

Untuk menyinkronkan langganan push sesuai permintaan di Management Studio (di Pelanggan)

  1. Koneksi ke Pelanggan di Management Studio, lalu perluas simpul server.

  2. Perluas folder Replikasi, lalu perluas folder Langganan Lokal.

  3. Klik kanan langganan yang ingin Anda sinkronkan, lalu klik Tampilkan Status Sinkronisasi.

  4. Pesan ditampilkan tentang membuat koneksi ke Distributor. Klik OK.

  5. Dalam kotak dialog Tampilkan Status Sinkronisasi - <Pelanggan>:<LanggananDatabase> , klik Mulai. Ketika sinkronisasi selesai, pesan Sinkronisasi selesai ditampilkan.

  6. Klik Tutup.

Untuk menyinkronkan langganan push sesuai permintaan di Monitor Replikasi

  1. Di Monitor Replikasi, perluas grup Publisher di panel kiri, perluas Publisher, lalu klik publikasi.

  2. Klik tab Semua Langganan .

  3. Klik kanan langganan yang ingin Anda sinkronkan, lalu klik Mulai Sinkronisasi.

  4. Untuk melihat kemajuan sinkronisasi, klik kanan langganan, lalu klik Tampilkan Detail.

Menggunakan Agen Replikasi

Langganan push dapat disinkronkan secara terprogram dan sesuai permintaan dengan memanggil file yang dapat dijalankan agen replikasi yang sesuai dari prompt perintah. File yang dapat dieksekusi agen replikasi yang dipanggil akan bergantung pada jenis publikasi tempat langganan push berada.

Untuk memulai Agen Distribusi untuk menyinkronkan langganan pendorongan ke publikasi transaksional

  1. Dari prompt perintah atau dalam file batch di Distributor, jalankan distrib.exe. Tentukan argumen baris perintah berikut:

    • -Penerbit

    • -PublisherDB

    • -Distributor

    • -Pelanggan

    • -SubscriberDB

    • -SubscriptionType = 0

    Jika Anda menggunakan Autentikasi SQL Server, Anda juga harus menentukan argumen berikut:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Penting

      Jika memungkinkan, gunakan autentikasi Windows.

Untuk memulai Agen Penggabungan untuk menyinkronkan langganan push ke publikasi gabungan

  1. Dari prompt perintah atau dalam file batch di Distributor, jalankan replmerg.exe. Tentukan argumen baris perintah berikut:

    • -Penerbit

    • -PublisherDB

    • -Publikasi

    • -Distributor

    • -Pelanggan

    • -SubscriberDB

    • -SubscriptionType = 0

    Jika Anda menggunakan Autentikasi SQL Server, Anda juga harus menentukan argumen berikut:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Penting

      Jika memungkinkan, gunakan autentikasi Windows.

Contoh (Agen Replikasi)

Contoh berikut memulai Agen Distribusi untuk menyinkronkan langganan push.

  
REM -- Declare the variables.  
SET Publisher=%instancename%  
SET Subscriber=%instancename%  
SET PublicationDB=AdventureWorks2022  
SET SubscriptionDB=AdventureWorks2022Replica   
SET Publication=AdvWorksProductsTran  
  
REM -- Start the Distribution Agent with four subscription streams.  
REM -- The following command must be supplied without line breaks.  
"C:\Program Files\Microsoft SQL Server\120\COM\DISTRIB.EXE" -Subscriber %Subscriber%   
-SubscriberDB %SubscriptionDB% -SubscriberSecurityMode 1 -Publication %Publication%   
-Publisher %Publisher% -PublisherDB %PublicationDB% -Distributor %Publisher%   
-DistributorSecurityMode 1 -Continuous -SubscriptionType 0 -SubscriptionStreams 4  
  

Contoh berikut memulai Agen Penggabungan untuk menyinkronkan langganan push.

  
REM -- Declare the variables.  
SET Publisher=%instancename%  
SET Subscriber=%instancename%  
SET PublicationDB=AdventureWorks2022  
SET SubscriptionDB=AdventureWorks2022Replica   
SET Publication=AdvWorksSalesOrdersMerge  
  
REM -- Start the Merge Agent.  
REM -- The following command must be supplied without line breaks.  
"C:\Program Files\Microsoft SQL Server\120\COM\REPLMERG.EXE"  -Publisher %Publisher%   
-Subscriber  %Subscriber%  -Distributor %Publisher% -PublisherDB  %PublicationDB%   
-SubscriberDB %SubscriptionDB% -Publication %Publication% -PublisherSecurityMode 1   
-OutputVerboseLevel 3  -Output -SubscriberSecurityMode 1  -SubscriptionType 0   
-DistributorSecurityMode 1  
  

Menggunakan Objek Manajemen Replikasi (RMO)

Anda dapat menyinkronkan langganan push secara terprogram dengan menggunakan Objek Manajemen Replikasi (RMO) dan akses kode terkelola ke fungsionalitas agen replikasi. Kelas yang Anda gunakan untuk menyinkronkan langganan push bergantung pada jenis publikasi tempat langganan berada.

Catatan

Jika Anda ingin memulai sinkronisasi yang berjalan secara otonom tanpa memengaruhi aplikasi Anda, mulai agen secara asinkron. Namun, jika Anda ingin memantau hasil sinkronisasi dan menerima panggilan balik dari agen selama proses sinkronisasi (misalnya, jika Anda ingin menampilkan bilah kemajuan), Anda harus memulai agen secara sinkron. Untuk Pelanggan edisi Microsoft SQL Server 2005 Express, Anda harus memulai agen secara sinkron.

Untuk menyinkronkan langganan push ke rekam jepret atau publikasi transaksional

  1. Buat koneksi ke Distributor dengan menggunakan ServerConnection kelas .

  2. Buat instans TransSubscription kelas dan atur properti berikut:

  3. LoadProperties Panggil metode untuk mendapatkan properti langganan yang tersisa. Jika metode ini mengembalikan false, verifikasi bahwa langganan ada.

  4. Mulai Agen Distribusi di Distributor dengan salah satu cara berikut:

    • SynchronizeWithJob Panggil metode pada instans TransSubscription dari langkah 2. Metode ini memulai Agen Distribusi secara asinkron, dan kontrol segera kembali ke aplikasi Anda saat pekerjaan agen berjalan. Anda tidak dapat memanggil metode ini jika langganan dibuat dengan nilai false untuk CreateSyncAgentByDefault.

    • Dapatkan instans TransSynchronizationAgent kelas dari SynchronizationAgent properti , dan panggil Synchronize metode . Metode ini memulai agen secara sinkron, dan kontrol tetap dengan pekerjaan agen yang sedang berjalan. Selama eksekusi sinkron, Anda dapat menangani Status peristiwa saat agen berjalan.

Untuk menyinkronkan langganan pendorongan ke publikasi gabungan

  1. Buat koneksi ke Distributor dengan menggunakan ServerConnection kelas .

  2. Buat instans MergeSubscription kelas, dan atur properti berikut:

  3. LoadProperties Panggil metode untuk mendapatkan properti langganan yang tersisa. Jika metode ini mengembalikan false, verifikasi bahwa langganan ada.

  4. Mulai Agen Penggabungan di Distributor dengan salah satu cara berikut:

    • SynchronizeWithJob Panggil metode pada instans MergeSubscription dari langkah 2. Metode ini memulai Agen Penggabungan secara asinkron, dan kontrol segera kembali ke aplikasi Anda saat pekerjaan agen berjalan. Anda tidak dapat memanggil metode ini jika langganan dibuat dengan nilai false untuk CreateSyncAgentByDefault.

    • Dapatkan instans MergeSynchronizationAgent kelas dari SynchronizationAgent properti , dan panggil Synchronize metode . Metode ini memulai Agen Penggabungan secara sinkron, dan kontrol tetap dengan pekerjaan agen yang sedang berjalan. Selama eksekusi sinkron, Anda dapat menangani Status peristiwa saat agen berjalan.

Contoh (RMO)

Contoh ini menyinkronkan langganan pendorongan ke publikasi transaksional, di mana agen dimulai secara asinkron menggunakan pekerjaan agen.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

/// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

TransSubscription subscription;

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Instantiate the push subscription.
    subscription = new TransSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        // Start the Distribution Agent asynchronously.
        subscription.SynchronizeWithJob();
    }
    else
    {
        // Do something here if the subscription does not exist.
        throw new ApplicationException(String.Format(
            "A subscription to '{0}' does not exists on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Dim subscription As TransSubscription

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Instantiate the push subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        ' Start the Distribution Agent asynchronously.
        subscription.SynchronizeWithJob()
    Else
        ' Do something here if the subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "A subscription to '{0}' does not exists on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

Contoh ini menyinkronkan langganan push ke publikasi transaksional, di mana agen dimulai secara sinkron.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

TransSubscription subscription;

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Define the push subscription.
    subscription = new TransSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription exists, start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.SubscriberSecurity != null)
        {
            // Synchronously start the Distribution Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize();
        }
        else
        {
            throw new ApplicationException("There is insufficent metadata to " +
                "synchronize the subscription. Recreate the subscription with " +
                "the agent job or supply the required agent properties at run time.");
        }
    }
    else
    {
        // Do something here if the push subscription does not exist.
        throw new ApplicationException(String.Format(
            "The subscription to '{0}' does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Dim subscription As TransSubscription

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Define the push subscription.
    subscription = New TransSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription exists, start the synchronization.
    If subscription.LoadProperties() Then
        ' Check that we have enough metadata to start the agent.
        If Not subscription.SubscriberSecurity Is Nothing Then

            ' Synchronously start the Distribution Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize()
        Else
            Throw New ApplicationException("There is insufficent metadata to " + _
             "synchronize the subscription. Recreate the subscription with " + _
             "the agent job or supply the required agent properties at run time.")
        End If
    Else
        ' Do something here if the push subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "The subscription to '{0}' does not exist on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

Contoh ini menyinkronkan langganan pendorongan ke publikasi penggabungan, di mana agen dimulai secara asinkron menggunakan pekerjaan agen.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

MergeSubscription subscription;

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Define push subscription.
    subscription = new MergeSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription and the job exists, start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        // Start the Merge Agent asynchronously.
        subscription.SynchronizeWithJob();
    }
    else
    {
        // Do something here if the subscription does not exist.
        throw new ApplicationException(String.Format(
            "A subscription to '{0}' does not exists on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Dim subscription As MergeSubscription

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Define push subscription.
    subscription = New MergeSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription and the job exists, start the agent job.
    If subscription.LoadProperties() And Not subscription.AgentJobId Is Nothing Then
        ' Start the Merge Agent asynchronously.
        subscription.SynchronizeWithJob()
    Else
        ' Do something here if the subscription does not exist.
        Throw New ApplicationException(String.Format( _
            "A subscription to '{0}' does not exists on {1}", _
            publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

Contoh ini menyinkronkan langganan pendorongan ke publikasi gabungan, di mana agen dimulai secara sinkron.

// Define the server, publication, and database names.
string subscriberName = subscriberInstance;
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string subscriptionDbName = "AdventureWorks2022Replica";
string publicationDbName = "AdventureWorks2022";

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

MergeSubscription subscription;

try
{
    // Connect to the Publisher
    conn.Connect();

    // Define the subscription.
    subscription = new MergeSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = publicationDbName;
    subscription.PublicationName = publicationName;
    subscription.SubscriptionDBName = subscriptionDbName;
    subscription.SubscriberName = subscriberName;

    // If the push subscription exists, start the synchronization.
    if (subscription.LoadProperties())
    {
        // Check that we have enough metadata to start the agent.
        if (subscription.SubscriberSecurity != null)
        {
            // Synchronously start the Merge Agent for the subscription.
            subscription.SynchronizationAgent.Synchronize();
        }
        else
        {
            throw new ApplicationException("There is insufficent metadata to " +
                "synchronize the subscription. Recreate the subscription with " +
                "the agent job or supply the required agent properties at run time.");
        }
    }
    else
    {
        // Do something here if the push subscription does not exist.
        throw new ApplicationException(String.Format(
            "The subscription to '{0}' does not exist on {1}",
            publicationName, subscriberName));
    }
}
catch (Exception ex)
{
    // Implement appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, publication, and database names.
Dim subscriberName As String = subscriberInstance
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publicationDbName As String = "AdventureWorks2022"

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Dim subscription As MergeSubscription

Try
    ' Connect to the Publisher
    conn.Connect()

    ' Define the subscription.
    subscription = New MergeSubscription()
    subscription.ConnectionContext = conn
    subscription.DatabaseName = publicationDbName
    subscription.PublicationName = publicationName
    subscription.SubscriptionDBName = subscriptionDbName
    subscription.SubscriberName = subscriberName

    ' If the push subscription exists, start the synchronization.
    If subscription.LoadProperties() Then
        ' Check that we have enough metadata to start the agent.
        If Not subscription.SubscriberSecurity Is Nothing Then
            ' Synchronously start the Merge Agent for the subscription.
            ' Log agent messages to an output file.
            subscription.SynchronizationAgent.Output = "mergeagent.log"
            subscription.SynchronizationAgent.OutputVerboseLevel = 2
            subscription.SynchronizationAgent.Synchronize()
        Else
            Throw New ApplicationException("There is insufficent metadata to " + _
             "synchronize the subscription. Recreate the subscription with " + _
             "the agent job or supply the required agent properties at run time.")
        End If
    Else
        ' Do something here if the push subscription does not exist.
        Throw New ApplicationException(String.Format( _
         "The subscription to '{0}' does not exist on {1}", _
         publicationName, subscriberName))
    End If
Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("The subscription could not be synchronized.", ex)
Finally
    conn.Disconnect()
End Try

Lihat Juga

Konsep Objek Manajemen Replikasi
Menyinkronkan Data
Praktik Terbaik Keamanan Replikasi