简体   繁体   English

SQLite 数据库锁定读取

[英]SQLite Database locking on reads

We are having an issue with a Xamarin.Android application we are building where there is database locking.我们在构建存在数据库锁定的 Xamarin.Android 应用程序时遇到问题。 As an overview of what the application does:作为应用程序功能的概述:

  • Table A get's inserts into it.表 A 将插入其中。
  • User presses a button to run a Scheduled Job to sync the data from the database to a an API.用户按下按钮运行计划作业,将数据库中的数据同步到 API。 When a successful response comes back, we delete that row in the table.当成功响应返回时,我们删除表中的该行。
  • The user is still able to add data to Table A as a part of their routine.作为例行程序的一部分,用户仍然可以将数据添加到表 A。
  • We get locks in Table A.我们在表 A 中获得了锁。

We build a test application to try and simulate this with a solution of building a Table B to copy the data from Table A so the user can keep working on Table A. But when the data on Table B is trying to sync (delete is there to simulate this) its data, we are getting locks on the table still on the Get().我们构建了一个测试应用程序来尝试使用构建表 B 以复制表 A 中的数据的解决方案来模拟这一点,以便用户可以继续在表 A 上工作。但是当表 B 上的数据尝试同步时(删除在那里为了模拟这个)它的数据,我们正在获取仍然在 Get() 上的表上的锁。

My Database code is here:我的数据库代码在这里:

using System;
using SQLite;
using System.Collections.Generic;
using System.Linq;

namespace LockSimulation
{
    public class SampleDatabaseB : IDatabase
    {
        public SampleDatabaseB()
        {
        try
        {
            string dbPath = System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "sampleb.db3");

            using (SQLiteConnection db = new SQLiteConnection(dbPath))
            {
                db.CreateTable<Sample>();
                db.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("DB already created");
        }

    }


    public List<Sample> Get()
    {
        List<Sample> samples = new List<Sample>();
        string dbPath = System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "sampleb.db3");

        using (var db = new SQLiteConnection(dbPath,SQLiteOpenFlags.ReadOnly))
        {

            var s = db.Table<Sample>();

            // ISSUE IS HERE> s.ToList() SHOWS THE LOCK

            samples = s.ToList();
            db.Close();

        }
        return samples;
    }

    public void Copy(List<Sample> samples)
    {
        string dbPath = System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "sampleb.db3");

        using (var db = new SQLiteConnection(dbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache))
        {
            db.InsertAll(samples);
            db.Close();
        }
    }

    public void Save(object sample)
    {
        string dbPath = System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "sampleb.db3");

        using (var db = new SQLiteConnection(dbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache))
        {
            db.Insert(sample);
            db.Close();
        }
    }
  }
}

My Scheduled Job is here:我的预定工作在这里:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.App;
using Android.App.Job;

namespace LockSimulation
{
    [Service(Name = "com.testapp.LockSimulation.Job", Permission = "android.permission.BIND_JOB_SERVICE")]
public class SyncJob : JobService
{
    SampleDatabaseB db = null;

    public override bool OnStartJob(JobParameters jobParams)
    {
        db = new SampleDatabaseB();
        Task.Run( () =>
        {
            var loopCount = jobParams.Extras.GetInt("LoopCount", 10);
            List<Sample> samples = db.Get();
            try
            {
                foreach (Sample sample in samples)
                {
                    db.Delete(sample);
                }

                if (db.Get().Count == 0)
                {
                    JobFinished(jobParams, false);
                }
                else
                {
                    JobFinished(jobParams, true);
                }
            }
            catch (SQLite.SQLiteException ex)
            {
                Console.WriteLine(ex);
            }
        });

        return true;
    }

    public override bool OnStopJob(JobParameters jobParams)
    {
        //Before the Job tries to finish, we will check the Samples DB.
        //If there is Samples on the DB, we will need to tell the process 
        //to run the job again.
        //If there is an issues in doing so, tell the process to try again.

        //if (db.Get().Count == 0)
        //{
        //    return false;
        //}
        //else
        //{
        //    return true;
        //}

        try
        {
            if (db.Get().Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            var properties = new Dictionary<string, string>
            {
                { "Syncing Job Exception", ex.Message},
                { "DB Issue", "DB could be having processing done on it. Will try again."}
            };
            Console.WriteLine(properties);
            //Crashes.TrackError(ex, properties);
            return true;
        }
    }
}
}

And my button code is here:我的按钮代码在这里:

button.Click += delegate {
            SampleDatabaseB sampleDatabaseB = new SampleDatabaseB();

            List<Sample> samplesA = sampleDatabase.Get();
            List<Sample> samplesB = sampleDatabaseB.Get();

            Console.WriteLine(String.Format("Total Samples In A Before Copy {0}", samplesA.Count));
            Console.WriteLine(String.Format("Total Samples In B Before Copy {0}", samplesB.Count));

            sampleDatabaseB.Copy(samplesA);
            sampleDatabase.DeleteAll();

            Console.WriteLine(String.Format("Total Samples In B After Copy {0}", sampleDatabaseB.Get().Count));

            Console.WriteLine(String.Format("Total Samples In A After Copy {0}", sampleDatabase.Get().Count));


            var jobBuilder = this.CreateJobBuilderUsingJobId<SyncJob>(1)
                                     .SetRequiredNetworkType(NetworkType.Any)
                                     .SetBackoffCriteria(2000, BackoffPolicy.Linear)
                                     .SetPersisted(true)
                                     .Build();

            var jobScheduler = (JobScheduler)GetSystemService(JobSchedulerService);
            var scheduleResult = jobScheduler.Schedule(jobBuilder);

            if (JobScheduler.ResultSuccess == scheduleResult)
            {
                Console.WriteLine("Samples Scheduled for Syncing Successfully");
            }
            else
            {
                Console.WriteLine("Samples Unsuccessfully Scheduled for Syncing.");
            }
        };

Has anyone had an issue like this where the Read was causing a lock?有没有人遇到过这样的问题,即 Read 导致锁定? is there anything from your experience that I could add?根据您的经验,我有什么可以补充的吗? It's a huge blocker for us and we really don't know why it's happening.这对我们来说是一个巨大的障碍,我们真的不知道为什么会这样。

I had similar issue in native android.我在原生 android 中遇到了类似的问题。 I was trying to do something, then close table and open again.我试图做某事,然后关闭桌子并再次打开。 I kept getting android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5) .我不断收到android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5) I managed to solve it by not opening and closing database all time.我设法通过不一直打开和关闭数据库来解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM