INF: Setting up Bidirectional ReplicationLast reviewed: December 19, 1997Article ID: Q173075 |
The information in this article applies to:
SUMMARYThis article details the steps to set up bidirectional transaction-based replication using a simple conflict resolution mechanism (also known as reconciliation), based on record ownership. It also discusses some situations that are handled by the custom stored procedures as well as recommendations on issues to be handled by the application.
MORE INFORMATION
Replication in SQL Server 6.xReplication in SQL Server 6.x is intended to distribute logically read-only copies of data to multiple servers to offload report processing load out of an online transaction processing (OLTP) server. SQL Server replication also assumes that the data at the subscriber is always in sync with the data at the publisher. This constrains the data that is obtained as a result of subscribing to a publication to be static at the subscriber. SQL Server replication is intended to satisfy this class of applications that are designed for near real-time, static data at the subscriber. Another class of applications that can use SQL Server replication is the one that depends on partitioned data. In this case, the servers own a disjointed set of data and each server replicates the data it owns to the other server. Effectively, the tables are published with a restriction clause to achieve horizontal partitioning and do not update data that is owned by the other server. This partitioning scheme implements two completely disjointed replication streams, thereby enhancing data availability. However, some applications may require completely distributed data. Though SQL Server replication is not intended for such an update anywhere scenario, it provides the basic framework for implementing a two-way or bidirectional replication setup, using custom stored procedures and filter procedures. However, there are some constraints on the schema and the transactions. It relies on the procedure logic or manual intervention to detect and resolve conflicts. This setup using bidirectional replication is intended to be used only with applications that yield themselves well to these constraints. Though this article uses an sample setup of bidirectional replication between two servers, it can easily be expanded to multiple servers. Also, the concepts discussed here apply to both SQL Server 6.0 and 6.5, though the features available are limited in SQL Server 6.0. For example, text and image columns cannot participate in transaction-based replication. Hence, appropriate modifications need to be done to the publication definition or custom stored procedures to handle the different cases.
Assumptions
ConstraintsThese constraints are based on the assumptions in the previous section.
Create custom stored procedures on both the servers involved. The following sample uses a simple reconciliation logic. The application can do data modification only on owned records (replicate_flag=1) or it will take ownership of the record prior to modification. Custom procedures will not update or delete data that is not a replica (replicate_flag=0). In such cases, update and delete commands will be ignored.
Create the Procedures in the Subscribing Database
/* custom procedure for insert */create proc sp_twoway_i @PKcol int, @Data int, @repl_flag smallint as begin set nocount on /* handle condition where delete part of update is filtered */ if exists (select * from twoway where PKcol = @PKcol) begin update twoway set Data = @Data, replicate_flag=0 where PKcol=@PKcol end else begin insert into twoway values (@PKcol,@Data,0) endend go
/* custom procedure for update */create proc sp_twoway_u @PKcolNew int, @Data int, @replicate_flag smallint, @PKcol int as begin set nocount on declare @flag smallint /* Update data only if this is subscribed data */ /* replicate_flag will be 0 for subscribed data */ if exists (select * from twoway where PKcol=@PKcol) begin select @flag = replicate_flag from twoway where PKcol=@PKcol if (@flag=0) begin update twoway set PKcol=@PKcolNew, Data=@Data,replicate_flag=0 where PKcol=@PKcol end end else /* handle cases where delete logreader generates */ /* update for delete/insert */ begin insert twoway values (@PKcolNew,@Data,0) endend go
/* custom procedure for delete */create proc sp_twoway_d @PKcol int as begin set nocount on /* Replication cannot update data owned by the site */ /* data owned by the site will have replicate_flag = 1 */ if exists (select * from twoway where PKcol = @PKcol and replicate_flag = 0) begin delete twoway where PKcol=@PKcol endend go Now that the table and the custom procedures have been created, set up replication on the table using a horizontal partition and select the "Do Nothing" option on the Auto-Generate Sync Scripts dialog box using the following steps:
replicate_flag = 1 on the Filters tab.4. On the Scripts tab, under Data Replication Mechanism, click Custom option for Insert/Update/Delete and specify the appropriate procedure name. For example, for insert, the entry similar to the following: call sp_twoway_i5. Now, click Generate. In the Auto-Generate Sync Scripts dialog box, under If Existing Table Detected in Subscriber, select the Do Nothing option. This is required because the table will exist at the subscriber and will be published. On synchronization, the default Drop Table option will be unable to drop a published table.6. To finish the setup, click Auto-Generate, click OK, and then click Add. The above steps must be performed on both servers. This will create the publications on both servers. From each server, subscribe to the other server's publication. If the tables do not contain any data, you may choose either the Data Synchronization Automatically Applied or the No Data Synchronization option in the Subscription Options dialog box. If the tables will contain data prior to publishing, on each server, the replication flag must be set to 1 and the primary key values must be unique. In this case, choose the Data Synchronization Manually Applied by Operator option and synchronize the tables as explained previously. This will complete the setup of bidirectional replication with a very simple reconciliation scheme. This sample procedure should be modified to include any logic your specific application/design may require.
|
Additional reference words: 2-way repl bi-directional bi-dir update
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |