gpt4 book ai didi

android - SQLite 数据库锁定读取

转载 作者:行者123 更新时间:2023-11-30 05:01:19 25 4
gpt4 key购买 nike

我们正在构建的 Xamarin.Android 应用程序存在数据库锁定问题。作为应用程序功能的概述:

  • 获取表 A 的插入内容。
  • 用户按下按钮运行计划作业,将数据从数据库同步到 API。当返回成功响应时,我们会删除表中的该行。
  • 用户仍然可以将数据添加到表 A 作为其日常工作的一部分。
  • 我们在表 A 中获得了锁。

我们构建了一个测试应用程序来尝试通过构建表 B 来复制表 A 中的数据的解决方案来模拟这一点,以便用户可以继续在表 A 上工作。但是当表 B 上的数据尝试同步时( delete 是为了模拟这个)它的数据,我们正在获取仍在 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.");
}
};

有没有人遇到过读取导致锁定这样的问题?从你的经验中有什么我可以补充的吗?这对我们来说是一个巨大的障碍,我们真的不知道为什么会这样。

最佳答案

我在原生 android 中遇到了类似的问题。我想做点什么,然后关上 table 再打开。我不断收到 android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)。我设法通过不一直打开和关闭数据库来解决这个问题。

关于android - SQLite 数据库锁定读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com