Megosztás:


Push előfizetés szinkronizálása

A következőkre vonatkozik:SQL ServerAzure SQL Database

Ez a témakör azt ismerteti, hogyan szinkronizálhat leküldéses előfizetéseket az SQL Server Management Studióval, replikációs ügynökökkel vagy replikációkezelési objektumokkal (RMO).

Megjegyzés:

A felügyelt Azure SQL-példány közzétevő, terjesztő és előfizető lehet a pillanatképek és a tranzakciós replikáció számára. Az Azure SQL Database adatbázisai csak pillanatkép- és tranzakciós replikáció push előfizetői lehetnek. További információ: Tranzakciós replikáció az Azure SQL Database-sel és a felügyelt Azure SQL-példányokkal.

Az SQL Server Management Studio használata

Az előfizetéseket a terjesztési ügynök (pillanatkép és tranzakciós replikáció) vagy az egyesítési ügynök (az egyesítési replikációhoz) szinkronizálja. Az ügynökök folyamatosan, igény szerint vagy ütemezés alapján futtathatók. A szinkronizálási ütemezések megadásáról további információt a Szinkronizálási ütemezések megadása című témakörben talál.

Igény szerinti előfizetés szinkronizálása a Microsoft SQL Server Management Studio Helyi kiadványok és helyi előfizetések mappáiból, valamint a Replikációfigyelő Minden előfizetés lapjáról. Az Oracle-kiadványokra vonatkozó előfizetések nem szinkronizálhatók igény szerint az előfizetőtől. A replikációfigyelő elindításáról további információt a Replikációfigyelő indítása című témakörben talál.

Leküldéses előfizetés igény szerinti szinkronizálása a Management Studióban (a Publisherben)

  1. Csatlakozzon a Publisherhez a Management Studióban, majd bontsa ki a kiszolgálócsomópontot.

  2. Bontsa ki a Replikáció mappát, majd bontsa ki a Helyi kiadványok mappát.

  3. Bontsa ki azt a kiadványt, amelyhez szinkronizálni szeretné az előfizetéseket.

  4. Kattintson a jobb gombbal a szinkronizálni kívánt előfizetésre, majd kattintson a Szinkronizálás állapotának megtekintése parancsra.

  5. A Szinkronizálási állapot megtekintése – <Előfizető>:<SubscriptionDatabase> párbeszédpanelen kattintson a Start gombra. Ha a szinkronizálás befejeződött, a szinkronizálás befejeződött üzenet jelenik meg.

  6. Kattintson a Bezárás gombra.

Leküldéses előfizetés igény szerinti szinkronizálása a Management Studióban (az előfizetőnél)

  1. Csatlakozzon az előfizetőhöz a Management Studióban, majd bontsa ki a kiszolgálócsomópontot.

  2. Bontsa ki a Replikáció mappát, majd bontsa ki a Helyi előfizetések mappát.

  3. Kattintson a jobb gombbal a szinkronizálni kívánt előfizetésre, majd kattintson a Szinkronizálás állapotának megtekintése parancsra.

  4. Megjelenik egy üzenet a forgalmazóval való kapcsolat létesítéséről. Kattintson az OK gombra.

  5. A Szinkronizálási állapot megtekintése – <Előfizető>:<SubscriptionDatabase> párbeszédpanelen kattintson a Start gombra. Ha a szinkronizálás befejeződött, a szinkronizálás befejeződött üzenet jelenik meg.

  6. Kattintson a Bezárás gombra.

Leküldéses előfizetés igény szerinti szinkronizálása a Replikációfigyelőben

  1. A Replikációfigyelőben bontsa ki a bal oldali panelen a Publisher csoportot, bontsa ki a Publishert, majd kattintson egy kiadványra.

  2. Kattintson a Minden előfizetés fülre .

  3. Kattintson a jobb gombbal a szinkronizálni kívánt előfizetésre, majd kattintson a Szinkronizálás indítása parancsra.

  4. A szinkronizálási folyamat megtekintéséhez kattintson a jobb gombbal az előfizetésre, majd kattintson a Részletek megtekintése parancsra.

Replikációs ügynökök használata

A leküldéses előfizetések programozottan és igény szerint szinkronizálhatók a megfelelő replikációs ügynök végrehajtható fájljának meghívásával a parancssorból. A meghívott replikációs ügynök végrehajtható fájlja attól függ, hogy milyen típusú kiadványhoz tartozik a leküldéses előfizetés.

A Terjesztési Ügynök elindítása a tranzakciós kiadványhoz tartozó push típusú előfizetés szinkronizálásához

  1. A parancssorból vagy a forgalmazónál található batch-fájlból hajtsa végre adistrib.exe. Adja meg a következő parancssori argumentumokat:

    • -Kiadó

    • -PublisherDB

    • -Elosztó

    • -Előfizető

    • -SubscriberDB

    • -SubscriptionType = 0

    Ha SQL Server-hitelesítést használ, a következő argumentumokat is meg kell adnia:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Fontos

      Ha lehetséges, windowsos hitelesítést használjon.

Az Egyesítési ügynök indítása leküldéses előfizetés egyesítési kiadványba való szinkronizálásához

  1. A parancssorból vagy a forgalmazónál található batch-fájlból hajtsa végre areplmerg.exe. Adja meg a következő parancssori argumentumokat:

    • -Kiadó

    • -PublisherDB

    • -Közzététel

    • -Elosztó

    • -Előfizető

    • -SubscriberDB

    • -SubscriptionType = 0

    Ha SQL Server-hitelesítést használ, a következő argumentumokat is meg kell adnia:

    • -DistributorLogin

    • -DistributorPassword

    • -DistributorSecurityMode = 0

    • -PublisherLogin

    • -PublisherPassword

    • -PublisherSecurityMode = 0

    • -SubscriberLogin

    • -SubscriberPassword

    • -SubscriberSecurityMode = 0

      Fontos

      Ha lehetséges, windowsos hitelesítést használjon.

Példák (replikációs ügynökök)

Az alábbi példa elindítja a terjesztési ügynököt egy leküldéses előfizetés szinkronizálásához.

  
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  
  

Az alábbi példa elindítja az egyesítési ügynököt egy leküldéses előfizetés szinkronizálásához.

  
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  
  

Replikációkezelési objektumok (RMO) használata

A leküldéses előfizetéseket programozott módon szinkronizálhatja a replikációkezelési objektumok (RMO) és a replikációs ügynök funkcióihoz való felügyelt kódhozzáférés használatával. A leküldéses előfizetés szinkronizálásához használt osztályok attól függenek, hogy milyen típusú kiadványhoz tartozik az előfizetés.

Megjegyzés:

Ha olyan szinkronizálást szeretne elindítani, amely autonóm módon fut az alkalmazás befolyásolása nélkül, indítsa el az ügynököt aszinkron módon. Ha azonban figyelni szeretné a szinkronizálás eredményét, és visszahívásokat szeretne kapni az ügynöktől a szinkronizálási folyamat során (például ha egy folyamatjelzőt szeretne megjeleníteni), szinkronizálva kell elindítania az ügynököt. A Microsoft SQL Server 2005 Express kiadás előfizetőinek szinkron módon kell elindítania az ügynököt.

Leküldéses előfizetés szinkronizálása pillanatképhez vagy tranzakciós kiadványhoz

  1. Hozzon létre kapcsolatot a forgalmazóval az ServerConnection osztály használatával.

  2. Hozza létre az osztály egy példányát TransSubscription , és állítsa be a következő tulajdonságokat:

  3. Hívja meg a metódust LoadProperties a fennmaradó előfizetési tulajdonságok lekéréséhez. Ha ez a módszer hamis értéket ad vissza, ellenőrizze, hogy létezik-e az előfizetés.

  4. Indítsa el a terjesztési ügynököt a forgalmazónál az alábbi módok egyikével:

    • Hívja meg a 2. lépésben lévő TransSubscription példányára a SynchronizeWithJob metódust. Ez a módszer aszinkron módon indítja el a terjesztési ügynököt, és a vezérlő azonnal visszatér az alkalmazáshoz, miközben az ügynökfeladat fut. Ezt a metódust nem hívhatja meg, ha az előfizetés hamisCreateSyncAgentByDefaultértékkel lett létrehozva.

    • Szerezze be az TransSynchronizationAgent osztály egy példányát a SynchronizationAgent tulajdonságból, és hívja meg a metódust Synchronize . Ez a módszer szinkron módon indítja el az ügynököt, és a vezérlés a futó ügynökfeladatnál marad. A szinkron végrehajtás során, miközben az ügynök fut, kezelheti a Status eseményt.

Push típusú előfizetés szinkronizálása összevonási közzététellel

  1. Hozzon létre kapcsolatot a forgalmazóval az ServerConnection osztály használatával.

  2. Hozza létre az osztály egy példányát MergeSubscription , és állítsa be a következő tulajdonságokat:

  3. Hívja meg a metódust LoadProperties a fennmaradó előfizetési tulajdonságok lekéréséhez. Ha ez a módszer hamis értéket ad vissza, ellenőrizze, hogy létezik-e az előfizetés.

  4. Indítsa el az egyesítési ügynököt a forgalmazónál az alábbi módok egyikével:

    • Hívja meg a 2. lépésben lévő MergeSubscription példányára a SynchronizeWithJob metódust. Ez a metódus aszinkron módon indítja el az egyesítési ügynököt, és a vezérlő azonnal visszatér az alkalmazáshoz, miközben az ügynökfeladat fut. Ezt a metódust nem hívhatja meg, ha az előfizetés hamisCreateSyncAgentByDefaultértékkel lett létrehozva.

    • Szerezze be az MergeSynchronizationAgent osztály egy példányát a SynchronizationAgent tulajdonságból, és hívja meg a metódust Synchronize . Ez a metódus szinkron módon indítja el az egyesítési ügynököt, és a vezérlés a futó ügynökfeladattal együtt marad. A szinkron végrehajtás során kezelheti a Status eseményt, amíg az ügynök fut.

Példák (RMO)

Ez a példa egy push-előfizetést szinkronizál egy tranzakciós kiadványra, ahol az ügynök aszinkron módon indul el az ügynökfeladat segítségével.

// 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

Ez a példa egy push-előfizetést szinkronizál egy tranzakciós publikációval, ahol az ügynök szinkronban indul el.

// 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

Ez a példa egy leküldéses előfizetést szinkronizál egy egyesítési kiadvánnyal, ahol az ügynök aszinkron módon indul el az ügynöki feladat használatával.

// 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

Ez a példa szinkronizál egy push típusú előfizetést egy egyesítési publikációra, ahol az ügynök szinkron módon indul el.

// 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