title: Best condition to defend against race condition entering SQL commands using Monitor statements
body: <p>I have a problem of two requests entering the same code simulataneously. I implemented the following solution that I read in the following article: <a href="https://www.developerfusion.com/article/84514/deep-c-8211-avoiding-race-conditions/" rel="nofollow noreferrer">https://www.developerfusion.com/article/84514/deep-c-8211-avoiding-race-conditions/</a></p>
  
  <p>Code:</p>
  
  <pre><code>public class TransactionManager
  {
      static readonly object MyCountLock = new object();
  
      public void ExectuteSQLStatments() 
      {
          try
          {
              Monitor.Enter(MyCountLock);
  
              // SQL statements that must be executed by one thread (or entering SQL).
          }
          finally
          {
              Monitor.Exit(MyCountLock);
          }
      }
  }
  </code></pre>
  
  <p>I am not sure that it is the best way to protect the execution of the statements by only one thread entering the 'Monitor' statement. That code was affected recently by race condition recently, and I want to prevent that condition from happening again.</p>