Solved

Merge Replication Conflicts

Posted on 2010-11-22
4
470 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 142

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 142

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now