代码之家  ›  专栏  ›  技术社区  ›  jstuardo

SQLite的“数据库已锁定”

  •  0
  • jstuardo  · 技术社区  · 6 年前

    我无法摆脱“数据库已锁定”错误。。。即使我使用互斥锁来控制对数据库访问的访问。

        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);
                 }
            }
        }
    

    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;
        }
    }
    

    詹姆

    0 回复  |  直到 6 年前