Race Condition - Resolved Through ADO.Net SQL Transaction

In our site there is a Shared (aka static) class that clears and then modifies a commonly used Table in a SQL Server Database.  The issue was that we would occasionally throughout the day get errors saying that "such and such record" could not inserted because it already exists.  We put in logging in between the statements to try and debug the thing since we could not seem to duplicate the error locally.  We noticed that in one example a single user had somehow two processes that were in lock-step through the function.  Both would clear the table, then both would attempt to insert the same data to it.  When the second process would attempt to insert it would blow up.

Basically the resource is shared and needs to be locked so it is not modified by multiple users at the same time. http://stackoverflow.com/questions/34510/what-is-a-race-condition

This article was very useful in creating the transaction that resolved my issue.  Ideally I would have preferred to have used a transaction in sql, but we clear it then do a bulk insert.
http://www.codeproject.com/Articles/522039/A-Beginners-Tutorial-for-Understanding-Transaction

Comments

Popular posts from this blog

Asp.net Publishing Broke Site - "App_WebReferences is not allowed because the application is precompiled"

Telerik - Custom Group Footers In RadGrid