?
Solved

Merge Replication Conflicts

Posted on 2010-11-22
4
Medium Priority
?
483 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
[X]
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
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 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