[英]SQLite Database locking on reads
我們在構建存在數據庫鎖定的 Xamarin.Android 應用程序時遇到問題。 作為應用程序功能的概述:
我們構建了一個測試應用程序來嘗試使用構建表 B 以復制表 A 中的數據的解決方案來模擬這一點,以便用戶可以繼續在表 A 上工作。但是當表 B 上的數據嘗試同步時(刪除在那里為了模擬這個)它的數據,我們正在獲取仍然在 Get() 上的表上的鎖。
我的數據庫代碼在這里:
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();
}
}
}
}
我的預定工作在這里:
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;
}
}
}
}
我的按鈕代碼在這里:
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.");
}
};
有沒有人遇到過這樣的問題,即 Read 導致鎖定? 根據您的經驗,我有什么可以補充的嗎? 這對我們來說是一個巨大的障礙,我們真的不知道為什么會這樣。
我在原生 android 中遇到了類似的問題。 我試圖做某事,然后關閉桌子並再次打開。 我不斷收到android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
。 我設法通過不一直打開和關閉數據庫來解決它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.