1

我正在开发一个有 50 多个表并且可以在没有互联网连接的情况下工作的应用程序,因此在后台应用程序可以与 API 同步并获取所有信息并在本地进行 CRUD 操作。

有时,当应用程序与 API 同步时,当我在应用程序上进行另一项操作时,我会收到错误“数据库已锁定”。

所以我需要帮助来解决这个问题,我知道有很多关于这个问题的帖子,但是基于我对我的数据库的实施,这似乎不足以解决我的问题。

Nugets:Xamarin.Forms 3.0.0 482510 sqlite-net-pcl 1.5.166-beta

我使用一个类 DataService.cs,该类与DataContext.csCRUD 连接并与数据库和方法 CRUD 建立连接。DataService 中的所有方法与 DataContext 的连接方式相同:

    //This is a resume of DataService.cs
        public class DataService 
            {
                public T Insert<T>(T model)
                {
                    try
                    {
                        using (var da = new DataContext())
                        {
                            da.Insert(model);
                            return model;
                        }
                    }
                    catch (Exception error)
                    {
                        error.ToString();
                        return model;
                    }
                }
        }

DataContext.cs我们有与本地数据库的连接以及与本地数据库的所有方法。所有方法都有collisionLock(避免与数据库冲突)和cnn.Dispose()(关闭与数据库的连接并避免错误Fatal signal 11 (SIGSEGV));

数据上下文.cs

    public interface IBusinessEntity
        {
            int ID { get; set; }
        }


         //This is a resume of DataContext.cs
            public class DataContext : IDisposable
            {
                #region Attributes
                public SQLiteConnection cnn;
                private static object collisionLock = new object();
                #endregion


                public DataContext()
                {
                    cnn = DependencyService.Get<IConfiguracion>().GetConnection();
        ...
        } 
                #endregion

                #region MetodosGenericosZulu
                public void Insert<T>(T model)
                {
                    try
                    {
                        // Use locks to avoid database collisions
                        lock (collisionLock)
                        {
                            cnn.Insert(model);
                            cnn.Dispose();
                        }
                    }
                    catch (Exception error)
                    {
                        Application.Current.MainPage.DisplayAlert(
                            "Error",
                            "Un error a ocurrido con la DB (Insert): " + error.Message.ToString(),
                            "Ok");
                    }
                }

                public void Update<T>(T model)
                {
                    try
                    {
                        lock (collisionLock)
                        {
                            cnn.Update(model);
                            cnn.Dispose();
                        }
                    }
                    catch (Exception error)
                    {
                        Application.Current.MainPage.DisplayAlert(
                                            "Error",
                                            "Un error a ocurrido con la DB (Actualizar): " + error.Message.ToString(),
                                            "Ok");
                    }
                }

                ...
        }
        }

Android项目的实现。

    public class Configuracion : IConfiguracion
        {
            public Configuracion(){ }

            public SQLite.SQLiteConnection GetConnection()
            {
                    var sqliteFileName = "FN_Desarrollo.db3";
                    string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                    var path = Path.Combine(documentsPath, sqliteFileName);
                  var  conn = new SQLite.SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache);

                return conn;
            }
        }

所以我需要你们的帮助来解决问题(数据库锁定)并检查我使用 SQLite 的实现是否正常。

我听到了所有的建议。

提前致谢。

4

1 回答 1

0

我使用一个连接完成了与本地数据库的所有操作。

在 Sqlite 文档中,他们建议打开连接一次,永远不要关闭它。

public class DataContext
    {
        public static SQLiteConnection cnn;

        public DataContext()
        {
            if (cnn == null)
            {
               Create your tables.
               cnn.CreateTable<...>();
               ...
            }
        }
   }
于 2020-07-08T14:01:32.100 回答