title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | helpviewer_keywords | monikerRange | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Synchronize a Push Subscription |
Learn how to synchronize a push subscription in SQL Server by using SQL Server Management Studio, replication agents, or Replication Management Objects. |
MashaMSFT |
mathoma |
09/25/2024 |
sql |
replication |
how-to |
|
|
=azuresqldb-current||>=sql-server-2016 |
[!INCLUDEsql-asdb] This topic describes how to synchronize a push subscription in [!INCLUDEssnoversion] by using [!INCLUDEssManStudioFull], replication agents, or Replication Management Objects (RMO).
[!INCLUDEazure-sql-db-replication-supportability-note]
Subscriptions are synchronized by the Distribution Agent (for snapshot and transactional replication) or the Merge Agent (for merge replication). Agents can run continuously, run on demand, or run on a schedule. For more information about specifying synchronization schedules, see Specify Synchronization Schedules.
Synchronize a subscription on demand from the Local Publications and Local Subscriptions folders in [!INCLUDEmsCoName] [!INCLUDEssManStudioFull] and the All Subscriptions tab in Replication Monitor. Subscriptions to Oracle publications cannot be synchronized on demand from the Subscriber. For information about starting Replication Monitor, see Start the Replication Monitor.
-
Connect to the Publisher in [!INCLUDEssManStudio], and then expand the server node.
-
Expand the Replication folder, and then expand the Local Publications folder.
-
Expand the publication for which you want to synchronize subscriptions.
-
Right-click the subscription you want to synchronize, and then click View Synchronization Status.
-
In the View Synchronization Status - <Subscriber>:<SubscriptionDatabase> dialog box, click Start. When synchronization is complete, the message Synchronization completed is displayed.
-
Click Close.
-
Connect to the Subscriber in [!INCLUDEssManStudio], and then expand the server node.
-
Expand the Replication folder, and then expand the Local Subscriptions folder.
-
Right-click the subscription you want to synchronize, and then click View Synchronization Status.
-
A message is displayed about establishing a connection to the Distributor. Click OK.
-
In the View Synchronization Status - <Subscriber>:<SubscriptionDatabase> dialog box, click Start. When synchronization is complete, the message Synchronization completed is displayed.
-
Click Close.
-
In Replication Monitor, expand a Publisher group in the left pane, expand a Publisher, and then click a publication.
-
Click the All Subscriptions tab.
-
Right-click the subscription you want to synchronize, and then click Start Synchronizing.
-
To view synchronization progress, right-click the subscription, and then click View Details.
Push subscriptions can be synchronized programmatically and on-demand by invoking the appropriate replication agent executable file from the command prompt. The replication agent executable file that is invoked will depend on the type of publication to which the push subscription belongs.
-
From the command prompt or in a batch file at the Distributor, execute distrib.exe. Specify the following command-line arguments:
-
-Publisher
-
-PublisherDB
-
-Distributor
-
-Subscriber
-
-SubscriberDB
-
-SubscriptionType = 0
If you are using SQL Server Authentication, you must also specify the following arguments:
-
-DistributorLogin
-
-DistributorPassword
-
-DistributorSecurityMode = 0
-
-PublisherLogin
-
-PublisherPassword
-
-PublisherSecurityMode = 0
-
-SubscriberLogin
-
-SubscriberPassword
-
-SubscriberSecurityMode = 0
[!IMPORTANT]
[!INCLUDEssNoteWinAuthentication]
-
-
From the command prompt or in a batch file at the Distributor, execute replmerg.exe. Specify the following command-line arguments:
-
-Publisher
-
-PublisherDB
-
-Publication
-
-Distributor
-
-Subscriber
-
-SubscriberDB
-
-SubscriptionType = 0
If you are using SQL Server Authentication, you must also specify the following arguments:
-
-DistributorLogin
-
-DistributorPassword
-
-DistributorSecurityMode = 0
-
-PublisherLogin
-
-PublisherPassword
-
-PublisherSecurityMode = 0
-
-SubscriberLogin
-
-SubscriberPassword
-
-SubscriberSecurityMode = 0
[!IMPORTANT]
[!INCLUDEssNoteWinAuthentication]
-
The following example starts the Distribution Agent to synchronize a push subscription.
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
The following example starts the Merge Agent to synchronize a push subscription.
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
You can synchronize push subscriptions programmatically by using Replication Management Objects (RMO) and managed code access to replication agent functionalities. The classes that you use to synchronize a push subscription depend on the type of publication to which the subscription belongs.
Note
If you want to start a synchronization that runs autonomously without affecting your application, start the agent asynchronously. However, if you want to monitor the outcome of the synchronization and receive callbacks from the agent during the synchronization process (for example, if you want to display a progress bar), you should start the agent synchronously. For [!INCLUDEmsCoName] [!INCLUDEssexpress-2005-md] Subscribers, you must start the agent synchronously.
-
Create a connection to the Distributor by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.TransSubscription class and set the following properties:
-
The publication database name for xref:Microsoft.SqlServer.Replication.Subscription.DatabaseName%2A.
-
The name of the publication to which the subscription belongs for xref:Microsoft.SqlServer.Replication.Subscription.PublicationName%2A.
-
The name of the subscription database for xref:Microsoft.SqlServer.Replication.Subscription.SubscriptionDBName%2A.
-
The name of the Subscriber for xref:Microsoft.SqlServer.Replication.Subscription.SubscriberName%2A.
-
The connection created in step 1 for xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationObject.LoadProperties%2A method to get the remaining subscription properties. If this method returns false, verify that the subscription exists.
-
Start the Distribution Agent at the Distributor in one of the following ways:
-
Call the xref:Microsoft.SqlServer.Replication.TransSubscription.SynchronizeWithJob%2A method on the instance of xref:Microsoft.SqlServer.Replication.TransSubscription from step 2. This method starts the Distribution Agent asynchronously, and control immediately returns to your application while the agent job is running. You cannot call this method if the subscription was created with a value of false for xref:Microsoft.SqlServer.Replication.Subscription.CreateSyncAgentByDefault%2A.
-
Obtain an instance of the xref:Microsoft.SqlServer.Replication.TransSynchronizationAgent class from the xref:Microsoft.SqlServer.Replication.TransSubscription.SynchronizationAgent%2A property, and call the xref:Microsoft.SqlServer.Replication.TransSynchronizationAgent.Synchronize%2A method. This method starts the agent synchronously, and control remains with the running agent job. During synchronous execution you can handle the xref:Microsoft.SqlServer.Replication.TransSynchronizationAgent.Status event while the agent is running.
-
-
Create a connection to the Distributor by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.MergeSubscription class, and set the following properties:
-
The publication database name for xref:Microsoft.SqlServer.Replication.Subscription.DatabaseName%2A.
-
The name of the publication to which the subscription belongs for xref:Microsoft.SqlServer.Replication.Subscription.PublicationName%2A.
-
The name of the subscription database for xref:Microsoft.SqlServer.Replication.Subscription.SubscriptionDBName%2A.
-
The name of the Subscriber for xref:Microsoft.SqlServer.Replication.Subscription.SubscriberName%2A.
-
The connection created in step 1 for xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationObject.LoadProperties%2A method to get the remaining subscription properties. If this method returns false, verify that the subscription exists.
-
Start the Merge Agent at the Distributor in one of the following ways:
-
Call the xref:Microsoft.SqlServer.Replication.MergeSubscription.SynchronizeWithJob%2A method on the instance of xref:Microsoft.SqlServer.Replication.MergeSubscription from step 2. This method starts the Merge Agent asynchronously, and control immediately returns to your application while the agent job is running. You cannot call this method if the subscription was created with a value of false for xref:Microsoft.SqlServer.Replication.Subscription.CreateSyncAgentByDefault%2A.
-
Obtain an instance of the xref:Microsoft.SqlServer.Replication.MergeSynchronizationAgent class from the xref:Microsoft.SqlServer.Replication.MergeSubscription.SynchronizationAgent%2A property, and call the xref:Microsoft.SqlServer.Replication.MergeSynchronizationAgent.Synchronize%2A method. This method starts the Merge Agent synchronously, and control remains with the running agent job. During synchronous execution, you can handle the xref:Microsoft.SqlServer.Replication.MergeSynchronizationAgent.Status event while the agent is running.
-
This example synchronizes a push subscription to a transactional publication, where the agent is started asynchronously using the agent job.
[!code-csHowTo#rmo_SyncTranPushSub_WithJob]
[!code-vbHowTo#rmo_vb_SyncTranPushSub_WithJob]
This example synchronizes a push subscription to a transactional publication, where the agent is started synchronously.
[!code-csHowTo#rmo_SyncTranPushSub]
[!code-vbHowTo#rmo_vb_SyncTranPushSub]
This example synchronizes a push subscription to a merge publication, where the agent is started asynchronously using the agent job.
[!code-csHowTo#rmo_SyncMergePushSub_WithJob]
[!code-vbHowTo#rmo_vb_SyncMergePushSub_WithJob]
This example synchronizes a push subscription to a merge publication, where the agent is started synchronously.
[!code-csHowTo#rmo_SyncMergePushSub]
[!code-vbHowTo#rmo_vb_SyncMergePushSub]