private int InternalSaveAll <TStorageModel, TModel>( SQLiteParallelConnection db, ICollection <TModel> models, bool overwriteExisting = true) where TModel : BaseModel, new() where TStorageModel : BaseStorageModel <TModel>, new() { using (new PerformanceTimer(typeof(TStorageModel).Name + ".batch.insert", true)) { int rowCount = 0; try { db.BeginTransaction(); foreach (var model in models) { TStorageModel storageModel = StorageModelInitializers.FromModel <TStorageModel, TModel>(model); if (overwriteExisting) { rowCount += db.InsertOrReplace(storageModel); } else { rowCount += db.Insert(storageModel); } } db.Commit(); } catch (Exception) { db.Rollback(); rowCount = -1; } return(rowCount); } }
/// <summary> /// Caches the specified database path. /// </summary> /// <param name="databasePath">The database path.</param> /// <param name="connection">The connection.</param> public static void Cache(string databasePath, SQLiteParallelConnection connection) { if (!IsEnable) { return; } var Cache = connection.IsReadonly ? ReadConnectionsCache : WriteConnectionsCache; var bag = Cache.GetOrAdd(databasePath, new ConcurrentBag <SQLiteParallelConnection>()); bag.Add(connection); /* * AppLogger.Default.WriteVerbose( * "[DB] {1} connection cached count {0}", * bag.Count, * connection.IsReadonly ? "Read" : "Write"); */ }
private async Task InvokeWithRetry(Action <SQLiteParallelConnection> callback, SQLiteParallelConnection db) { int retryAttempts = 3; while (retryAttempts >= 0) { SQLiteException exception = null; try { callback(db); } catch (SQLiteException e) { if (retryAttempts <= 0 || (e.Result != SQLite3.Result.CannotOpen && e.Result != SQLite3.Result.LockErr && e.Result != SQLite3.Result.Warning)) { throw; } exception = e; } if (exception == null) { return; } await Task.Delay(500 *retryAttempts); retryAttempts--; } }