Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework concurrency refresh and update
    primarykey
    data
    text
    <p>I have written a .NET + EF application. Everything works fine on a single thread. On multiple threads - it's another story.</p> <p>In my EF object I have an integer counter. This property is marked as "Concurrency Mode = Fixed". Basically, what I'm trying to do is <strong>update this counter on several threads</strong>. Like this operation:</p> <pre><code>this.MyCounter -= 1; </code></pre> <p>Because it's concurrency mode has been changed to "Fixed", when I'm tring to update a property that's already change - an <code>OptimisticConcurrencyException</code> is thrown.</p> <p>In order to solve this concurrency problems, I'm using this code:</p> <pre><code>while (true) { try { this.UsageAmount -= 1; // Change the local EF object value and call SaveChanges(). break; } catch (OptimisticConcurrencyException) { Logger.Output(LoggerLevel.Trace, this, "concurrency conflict detected."); EntityContainer.Instance.Entities.Refresh(RefreshMode.StoreWins, this.InnerObject); } } </code></pre> <p>The result of this code is an infinite (or maybe its just looks like) loop. Every call of <code>this.UsageAmount -= 1</code> throw an <code>OptimisticConcurrencyException</code>, which causes the loop to run again.</p> <p>My <code>EntityContainer.Instance.Entities</code> is a singleton class that provides an EF context PER THREAD. This means that every thread has a unique context. The code:</p> <pre><code>public sealed class EntityContainer { #region Singlethon Implemantation private static Dictionary&lt;Thread, EntityContainer&gt; _instance = new Dictionary&lt;Thread,EntityContainer&gt; (); private static object syncRoot = new Object(); public static EntityContainer Instance { get { if (!_instance.ContainsKey(Thread.CurrentThread)) { lock (syncRoot) { if (!_instance.ContainsKey(Thread.CurrentThread)) _instance.Add(Thread.CurrentThread, new EntityContainer()); } } return _instance[Thread.CurrentThread]; } } private EntityContainer() { Entities = new anticopyEntities2(); } #endregion anticopyEntities2 _entities; public anticopyEntities2 Entities { get { //return new anticopyEntities2(); return _entities; } private set { _entities = value; } } } </code></pre> <p>BTW, after calling the <code>Entities.Refresh</code> methods - it looks like it's working (object state is Unchanged and the propery value is exactly what exists in the database).</p> <p>How can I solve this concurrency problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload