Export (0) Print
Expand All

How to: Reinitialize a Subscription (RMO Programming)

Individual subscriptions can be marked for reinitialization so that during the next synchronization, a new snapshot is applied. Subscriptions can be reinitialized programmatically by using Replication Management Objects (RMO). The classes you use depend on the type of publication to which the subscription belongs and the type of subscription (that is, a push or pull subscription).

  1. Create a connection to the Subscriber by using the ServerConnection class.

  2. Create an instance of the TransPullSubscription class, and set PublicationName, DatabaseName, PublisherName, PublicationDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(en-US,SQL.90).gifNote:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the pull subscription does not exist.

  4. Call the Reinitialize method. This method marks the subscription for reinitialization.

  5. Synchronize the pull subscription. For more information, see How to: Synchronize a Pull Subscription (RMO Programming).

  1. Create a connection to the Publisher by using the ServerConnection class.

  2. Create an instance of the TransSubscription class, and set PublicationName, DatabaseName, SubscriberName, SubscriptionDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(en-US,SQL.90).gifNote:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the push subscription does not exist.

  4. Call the Reinitialize method. This method marks the subscription for reinitialization.

  5. Synchronize the push subscription. For more information, see How to: Synchronize a Push Subscription (RMO Programming).

  1. Create a connection to the Subscriber by using the ServerConnection class.

  2. Create an instance of the MergePullSubscription class, and set PublicationName, DatabaseName, PublisherName, PublicationDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(en-US,SQL.90).gifNote:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the pull subscription does not exist.

  4. Call the Reinitialize method. Pass a value of true to upload changes at the Subscriber before reinitialization or a value of false to reinitialize and lose any pending changes at the Subscriber. This method marks the subscription for reinitialization.

    ms146923.note(en-US,SQL.90).gifNote:
    Changes cannot be uploaded if the subscription is expired. For more information, see A Merge Subscription Has Expired and Changes Must Be Uploaded.

  5. Synchronize the pull subscription. For more information, see How to: Synchronize a Pull Subscription (RMO Programming).

  1. Create a connection to the Publisher by using the ServerConnection class.

  2. Create an instance of the MergeSubscription class, and set PublicationName, DatabaseName, SubscriberName, SubscriptionDBName, and the connection from step 1 for ConnectionContext.

  3. Call the LoadProperties method to get the properties of the object.

    ms146923.note(en-US,SQL.90).gifNote:
    If this method returns false, either the subscription properties in step 2 were defined incorrectly or the push subscription does not exist.

  4. Call the Reinitialize method. Pass a value of true to upload changes at the Subscriber before reinitialization or a value of false to reinitialize and lose any pending changes at the Subscriber. This method marks the subscription for reinitialization.

    ms146923.note(en-US,SQL.90).gifNote:
    Changes cannot be uploaded if the subscription is expired. For more information, see A Merge Subscription Has Expired and Changes Must Be Uploaded.

  5. Synchronize the push subscription. For more information, see How to: Synchronize a Push Subscription (RMO Programming).

This example reinitializes a pull subscription to a transactional publication.

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksProductTran";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

TransPullSubscription subscription;

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

    // Define subscription properties.
    subscription = new TransPullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription and the job exists, mark the subscription
    // for reinitialization and start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        subscription.Reinitialize();
        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)
{
    // Do appropriate error handling here.
    throw new ApplicationException("The subscription could not be reinitialized.", ex);
}
finally
{
    conn.Disconnect();
}

This example reinitializes a pull subscription to a merge publication after first uploading pending changes at the Subscriber.

// Define server, publication, and database names.
String subscriberName = subscriberInstance;
String publisherName = publisherInstance;
String publicationName = "AdvWorksSalesOrdersMerge";
String publicationDbName = "AdventureWorks";
String subscriptionDbName = "AdventureWorksReplica";

// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);

MergePullSubscription subscription;

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

    // Define subscription properties.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = conn;
    subscription.DatabaseName = subscriptionDbName;
    subscription.PublisherName = publisherName;
    subscription.PublicationDBName = publicationDbName;
    subscription.PublicationName = publicationName;

    // If the pull subscription and the job exists, mark the subscription
    // for reinitialization after upload and start the agent job.
    if (subscription.LoadProperties() && subscription.AgentJobId != null)
    {
        subscription.Reinitialize(true);
        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)
{
    // Do appropriate error handling here.
    throw new ApplicationException("The subscription could not be synchronized.", ex);
}
finally
{
    conn.Disconnect();
}

Community Additions

ADD
Show:
© 2014 Microsoft