Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Merge Replication Conflicts

Posted on 2010-11-22
4
477 Views
Last Modified: 2012-05-10
I am using LINQ to SQL in my code of a form that does import some sales data, also I surround the code with a transaction object. Importing the sales subtracts logically the product quantity from the inventory table.
However, if I import on one replica the sales and on the other also do some actions that affect the inventory record of the same product, the merge replication chooses a winner and that renders the record with wrong on hand qty, because the loser (the imported sales) does not roll back all the sales imported!
example:
replica1, productA, import sales of 10 pieces, on hand qty was 20, then it ends being 10
replica2,productA enter goods of 5 pieces, on hand qty was 20, then it ends being 25
after replication and winning of replica2, the inventory is 25, and the sales records are not rolled back! which makes the data corrupted.
0
Comment
Question by:TheSilverBullet
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34190726
what are your replication intervals? they might be often enough to avoid such merge replication conflicts, aka updates on both sides.

anyhow: the merge replication IS transactional, but it cannot resolve a transaction on both sides "at the same time" (= in the same replication interval) to update the same record, at it will have to choose a winner.
you have to know that replication does not replay the SQL, but sends the "old/new" data (aka what is in the transaction log: there is no SQL in there. only data info)

another path to solve this might be to split the table into 2, or have 2 columns for the qty:
qty_sold
qty_bought

so, the process selling will only update 1 of the 2 columns, and the process "buying" (aka adding to the store) update the other. a computed column (qty) could do the difference ...

0
 

Author Comment

by:TheSilverBullet
ID: 34194247
Thank you so much for important contribution. what I have is
Transaction
{
update sales table;
update inventory table by subtracting sold qty;
}

on the other replica at the same moment:
Transaction
{
update purchase table;
update inventory table by adding purchased qty;

}

here for the same product there is a conflict on its inventory table record (I am replicating every 60 seconds, but one could go offline in the merge replication for a while), what the agent does is selecting the second as a winner. I thought -since it is transactional in nature- it would rollback the whole first transaction if any line within it get conflicted with another higher priority transaction.
"replication does not replay the SQL" that clarifies it.
I will try to split, sounds a good suggestion.
0
 

Author Comment

by:TheSilverBullet
ID: 34194294
splitting into two columns, still could raise the same issue: what if two purchases done at the same time on two different replicas? in that case it would affect the Purchase Part of the splitting as well and conflict it.
Is there any way to access the conflict viewer data thru SQL queries, only for readonly. that could help to make me program a tool to auto resolve the conflict.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34194563
then I only see the following solution:
the inventory table should be updated on a central store, and this when the purchase and sales tables are updated (including by replication) on that same "central" store.

so, your application should not update the "local replica", but only the purchase/sales table. that change being replicated to the "central" store (aka the one where the inventory is hold), that replication triggers the update of the inventory (which in turn gets replicated to the other replicas )

this however means that the time between the purchase/sales record and the update in the same database for the inventory takes (at least) 2 replication cycles.

you could, of course, hold each time also local sales numbers, to ensure that at least "local sales" will be taken into account when considering if an inventory is "empty" (aka close to being empty) ...
this is indeed a big design issue...
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question