1

We are having an issue with a Xamarin.Android application we are building where there is database locking. As an overview of what the application does:

  • Table A get's inserts into it.
  • User presses a button to run a Scheduled Job to sync the data from the database to a an 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.
  • We get locks in Table 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().

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? 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.

Saamer
  • 4,687
  • 1
  • 13
  • 55
SmiffyKmc
  • 801
  • 1
  • 16
  • 34

1 Answers1

0

I had similar issue in native 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). I managed to solve it by not opening and closing database all time.

solaza
  • 1,251
  • 1
  • 17
  • 30
  • Hi solaza, thank you for your input. Can you explain what you mean by "not opening and closing database all time". You don't create a new connection on DB call? You don't explicitly close the connection? If so do you trust in just using the "using" statement? – SmiffyKmc Oct 02 '19 at 11:24
  • With "not opening and closing database all time" I mean that if I know I'm doing more than one operation after another I close database after last one. In my case problem was opening and closing connection too fast. I realized it in production while on debug everything worked fine and even then this wasn't happening that often. As I said I'm working on native apps. Hope I helped you at least a bit. – solaza Oct 02 '19 at 11:34
  • You have solaza, as much help as possible would be great. I read the "using statement" would close the connection if there was an issue and I was closing the connection after my other commands have finished. We could be deleting rows in the ScheduledJob while we are trying to read from the database. Creating a new DB to write job information to read from other than reading from the Table A will help. – SmiffyKmc Oct 02 '19 at 11:53