Publishing Stored Procedure execution in transactional Replication

Source: Internet
Author: User

Replication not only pushes the table article or SP article to the subscription, but also pushes the execution of the SP to subscription. The execution of the push SP refers to pushing the command of the executing SP to subscriber to execute, rather than pushing a large amount of transaction generated after the SP execution to Subscriber. Transaction is to update data rows in table article one at a.

Push the SP's execution to synchronize the subscription data, with certain restrictions on the SP:

Stored procedure replication is not appropriate for all applications. If an article was filtered horizontally, so that there be different sets of rows at the Publisher than at the subscriber, Executing the same stored procedure at both returns different results. Similarly, if an update was based on a subquery of another, nonreplicated table, executing the same stored procedure at bot H The Publisher and Subscriber returns different results.

Push SP execution Settings article properties:replicate for execution of the stored procedure or execution in a serialized of the SP One.

The SP code is to update the table article value, Dbo.dt_study is a table article, set without the row filter condition

ALTER PROCEDURE [dbo].[Usp_update_dt_study]@id int,@name varchar( -),@sex bit asBEGIN    --SET NOCOUNT on added to prevent extra result sets from    --interfering with SELECT statements.    SETNOCOUNT on; UpdateDbo.dt_studySetName=@name, Sex=@sex    whereId=@id;END


Reference MSDN Documentation: Publishing Stored Procedure execution in transactional Replication

If you had one or more stored procedures that execute at the Publisher and affect published tables, consider including Those stored procedures in your publication as stored procedure execution articles . The definition of the procedure (the CREATE procedure statement) is replicated to the subscriber when the subscription is Initialized When the procedure are executed at the Publisher, replication executes the corresponding procedure at the Subscriber. This can provide significantly better performance for cases where large batch operations is performed, because only the P Rocedure execution is replicated, bypassing the need-replicate the individual changes for each row. For example, assume to create the following stored procedure in the publication database:

CREATE PROC  as UPDATE SET = * 1.10

This procedure gives all of the employees in your company a ten percent pay increase. When you execute this stored procedure at the Publisher, it updates the salary for each employee. Without the replication of stored procedure execution, the update would is sent to subscribers as a large, multi-step Tran Saction:

 begin  tran  update  EMPLOYEES set  salary *  1.10  where  PK =   "   " update  EMPLOYEES set  salary =  salary *  1.10  =   '  

And this repeats for updates.

With the replication of stored procedure execution, replication sends only the command to execute the stored procedure at The subscriber, rather than writing all the updates to the distribution database and then sending them over the network to The Subscriber:

EXEC give_raise

Types of Stored Procedure execution articles

There is different ways in which the execution of a stored procedure can be published:serializable procedure Executi On article and procedure execution article.
  • The serializable option is recommended because it replicates the procedure execution only if the procedure is Execu Ted within the context of a serializable transaction. If the stored procedure is executed from outside a serializable transaction, changes to data in published tables are repli Cated as a series of DML statements. This behavior contributes-making data at the Subscriber consistent with data at the Publisher. This was especially useful for batch operations, such as large cleanup operations.

  • With the procedure execution option, it's possible that execution could being replicated to all subscribers regardless of wh Ether individual statements in the stored procedure were successful. Furthermore, because changes made to data by the stored procedure can occur within multiple transactions, data at the Subs Cribers might not being consistent with data at the Publisher. To address these issues, it's required that subscribers be read-only and that's use a isolation level greater than re AD uncommitted. If you use the READ uncommitted, changes to data in published tables is replicated as a series of DML statements.

The following example illustrates why it's recommended that's set up replication of procedures as serializable Procedur E articles.

BEGIN TRANSACTIONT1SELECT @var = Max(col1) fromTableAUPDATETableASETCol2= <Value>    WHERECol1= @var BEGIN TRANSACTIONT2INSERTTableAVALUES <Values>COMMIT TRANSACTIONT2

In the previous example, it's assumed, the SELECT in transaction T1 happens before the INSERT in transaction T2.

If The procedure is isn't executed within a serializable transaction (with isolation level set to serializable), transaction T2 'll is allowed to insert a new row within the range of the SELECT statement in T1 and it'll commit before T1. This also means, that it'll be is applied at the Subscriber before T1. When T1 are applied at the Subscriber, the SELECT can potentially return a different value than at the Publisher and can re Sult in a different outcome from the UPDATE.

If the procedure is executed within a serializable transaction, transaction T2 'll not being allowed to insert within the RA Nge covered by the SELECT statement in T2. It'll be blocked until T1 commits ensuring the same results at the Subscriber.

Locks'll be held longer if you execute the procedure within a serializable transaction and could result in reduced concu Rrency.

The xact_abort Setting

When replicating stored procedure execution, the setting for the session executing the stored procedure should specify XAC T_abort on. If Xact_abort is set to OFF, and a error occurs during execution of the procedure at the Publisher, the same error would o CCur at the Subscriber, causing the distribution Agent to fail. Specifying XACT_ABORT on ensures this any errors encountered during execution at the Publisher cause the entire execution To is rolled back, avoiding the distribution Agent failure.

If you require a setting of Xact_abort OFF, specify the -skiperrors parameter for the distribution Agent. This allows the agent to continue applying changes at the Subscriber even if a error is encountered.

Publishing Stored Procedure execution in transactional Replication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.