簡體   English   中英

SQLite 拋出忙異常並鎖定

[英]SQLite throws busy exception and locked

我有一個使用 xamarin 創建的xamarin應用程序。 這是崩潰的代碼,不是第一次,而是第二次!

        public void InsertOrUpdateInventoryWithoutEntries(Inventory inventory)
        {
        _logger.Debug("Enter");
        try
        {
            var db = _dataBaseConnection.GetSqLiteConnection();
            using (db)
            {
                var existingDbInventory = db.Find<DbInventory>(dbInventory => dbInventory.Code == 
                 inventory.Code);
                if (existingDbInventory != null)
                {
                    if (!existingDbInventory.Finished ) // do not update if finished.
                    {
                        existingDbInventory.Description = inventory.Description;

                        existingDbInventory.OpenTime = inventory.OpenTime;
                        existingDbInventory.Finished = inventory.Finished ;
                        existingDbInventory.UseInventoryList = inventory.UseInventoryList;
                        existingDbInventory.PostedToServer = inventory.PostedToServer;
                        existingDbInventory.InventoryListIsDownloaded = 
                                                      inventory.InventoryListIsDownloaded;
                        UpdateInventory(existingDbInventory,db);

                    }
                }
                else
                {
                    db.Insert(DbInventory.FromInventory(inventory));
                }
                db.Close();
            }
        }
        catch
        (SQLiteException ex)
        {
            _logger.Error(ex);
            throw;
        }
    }

        private void UpdateInventory(DbInventory inventory, SQLiteConnection db)
    {
        _logger.Debug("Enter");
        try
        {
           var result = db.Update(inventory);

        }
        catch (SQLiteException ex)
        {
            _logger.Error(ex);
            throw;
        }
    }
        public bool InsertOrUpdateInventoryEntry(InventoryEntry inventoryEntryModel, 
        SQLiteConnection db=null)
    {
        _logger.Debug("Enter");
        bool disposeFlg = false;

        //detta då sqllite inte klarar av samtidiga anrop så bra, så man skall bara använda en 
         connection i taget !
        if (db == null)
        {
            db = _dataBaseConnection.GetSqLiteConnection();
            disposeFlg = true;
        }

        var existingDbInvetoryRow = db.Find<DbInventoryEntry>(dbInventoryRow => 
             dbInventoryRow.ItemId == inventoryEntryModel.ItemId && dbInventoryRow.InventoryCode == 

            inventoryEntryModel.InventoryCode);
            if (existingDbInvetoryRow != null)
            {
                existingDbInvetoryRow.Cost = inventoryEntryModel.Cost;
                existingDbInvetoryRow.Quantity = inventoryEntryModel.Quantity;

                db.Update(existingDbInvetoryRow);
            }
            else
            {
                db.Insert(DbInventoryEntry.FromInventoryEntry(inventoryEntryModel));
            }

            if (disposeFlg)
            {
                db.Close();
                db.Dispose();
            }

            return true;
    }
       private bool InsertInventoryRows(IEnumerable<InventoryEntry> inventoryEntryModels)
    {
        _logger.Debug("Enter");
        var dbRows = inventoryEntryModels.Select(entry => 
        (DbInventoryEntry.FromInventoryEntry(entry)));

        var db = _dataBaseConnection.GetSqLiteConnection();

        using (db)
        {
            db.InsertAll(dbRows);

            db.Close();
        }
        return true;
    }

我得到的錯誤是:

SQLite.SQLiteException:“數據庫已鎖定”或 SQLite.SQLiteException:“數據庫正忙”

感謝 Jason,我找到了解決方案 - github.com/praeclarum/sqlite-net/issues/700

我建議您為您的應用程序保留一個 SQLiteConnection 並將其緩存以利用類型映射緩存策略。 用 Create | 打開它讀寫 | FullMutex 標志將確保所有操作都是多線程序列化的。 每當您的應用關閉時,不要忘記 Dispose 連接

這完美地工作並加速了應用程序! 謝謝傑森!

我所做的是處理一個我一直保持打開狀態的 static 連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM