title: "database is locked" for SQLite
body: <p>I cannot get rid of "database is locked" error... even when I used mutex to control access to database access.</p>
  
  <p>This is an event handler that is thrown asynchronously:</p>
  
  <pre><code>    void driver_Transaccion(object sender, AttendanceReader.MarcacionEventArgs e)
      {
          if (_transaccion != null)
          {
              try
              {
                  using (GlobalMutex.GetMutex())
                  {
                      using (var db = new DataWare.monitorEntities())
                      {
                            /* some instructions */
                            /* ..... */
                            db.SaveChanges(); /* A database lock occurs here */
                      }
                  }
               }
               catch (Exception ex)
               {
                  ErrorLog.Save(ex);
               }
          }
      }
  </code></pre>
  
  <p>GlobalMutex.GetMutex() is defined this way:</p>
  
  <pre><code>public class GlobalMutex : IDisposable
  {
      private Mutex _mutex;
      private bool _acquired;
  
      public GlobalMutex()
      {
          _mutex = new Mutex(false, "RELOJ_ELO_20181004");
          _acquired = _mutex.WaitOne();
      }
  
      public static IDisposable GetMutex()
      {
          return new GlobalMutex();
      }
  
      public void Dispose()
      {
          if (_acquired)
              _mutex.ReleaseMutex();
          _mutex.Dispose();
          _mutex = null;
      }
  }
  </code></pre>
  
  <p>On the other hand, there is another application that also write to the database.</p>
  
  <p>That external application captures mutex the same way.</p>
  
  <p>Maybe db.SaveChanges does not close database file before the other process tries to write ot it. Is SQLite database slow to do that? Is there an effective way to solve this?</p>
  
  <p>Regards
  Jaime</p>