public void CommitTransaction() { var lst = new List <string>(); using (var cmd = new SqliteCommand(String.Format("SELECT DISTINCT [TableName] FROM {0}", TranStatusTable), ActiveConnection)) { using (SqliteDataReader r = cmd.ExecuteReader()) { while (r.Read()) { lst.Add(r.GetString(0)); } } } SqliteTransaction tran = ActiveConnection.BeginTransaction(); try { foreach (String tableName in lst) { using (var cmd = new SqliteCommand(String.Format("DELETE FROM __{0}", tableName), tran.Connection, tran)) cmd.ExecuteNonQuery(); } using (var cmd = new SqliteCommand(String.Format("DELETE FROM {0}", TranStatusTable), tran.Connection, tran)) cmd.ExecuteNonQuery(); tran.Commit(); } catch { tran.Rollback(); throw; } }
public void SyncComplete(bool success) { if (success) { using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { CreateSchema(); foreach (string tableName in _tablesByIdx) { if (tableName.StartsWith("_Catalog_") || tableName.StartsWith("_Document_")) { Exec(string.Format("UPDATE {0} SET IsDirty = 0 WHERE IsDirty = 1", tableName), cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); }); } } Cache.ClearNew(); tran.Commit(); } catch { tran.Rollback(); throw; } } } UpdateDbStatus(_userId, success); }
public void SaveAnchor(byte[] anchor) { using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { Exec(string.Format("DELETE FROM {0}", AnchorTable), cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); if (anchor != null) { string data = Convert.ToBase64String(anchor); cmd.CommandText = String.Format("INSERT INTO {0}([Data]) VALUES(@Data)", AnchorTable); cmd.Parameters.AddWithValue("@Data", data); cmd.ExecuteNonQuery(); } }); tran.Commit(); } catch { tran.Rollback(); throw; } } }
/// <summary> /// Starts the transaction. /// </summary> /// <param name="name">The name.</param> /// <param name="isolation">The isolation.</param> private static void StartTransaction(string name, IsolationLevel isolation) { if (TransactionCounter <= 0) { TransactionCounter = 0; ActiveConnection = Connection(name); ActiveTransaction = ActiveConnection.BeginTransaction(isolation); } TransactionCounter++; }
public void Save(IEntity obj, bool inTran) { using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { ProcessAllInternal(new[] { obj }, ProcessMode.LocalChanges, tran, inTran); tran.Commit(); } catch { tran.Rollback(); } } }
public void Delete(IDbRef obj, bool inTran) { using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { if (!inTran) { string tableName = obj.TableName; Exec(string.Format("DELETE FROM __{0} WHERE [Id]=@Id", tableName), cmd => { cmd.Transaction = tran; cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.ExecuteNonQuery(); }); string q = string.Format("DELETE FROM {0} WHERE [Id]=@Id AND [TableName]=@TableName", TranStatusTable); Exec(q, cmd => { cmd.Transaction = tran; cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.Parameters.AddWithValue("@TableName", tableName); cmd.ExecuteNonQuery(); }); } else { lock (_dbsync) CopyTranObject(tran, obj.TableName, obj.ToString(), Operation.Delete); } Exec(string.Format("UPDATE [_{0}] SET IsTombstone = 1 WHERE [Id]=@Id", obj.TableName), cmd => { cmd.Transaction = tran; cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.ExecuteNonQuery(); }); tran.Commit(); } catch { tran.Rollback(); } } }
public void ProcessData(IEnumerable <IEntity> data, ProcessMode mode, SqliteTransaction tran = null) { bool inTran = tran != null; var toRemoveFromCache = new List <Guid>(); try { foreach (IEnumerable <IEntity> lst in GetBlock(data.GetEnumerator())) { if (!inTran) { tran = ActiveConnection.BeginTransaction(); } ProcessAllInternal(lst, mode, tran, mode == ProcessMode.LocalChanges); if (!inTran) { tran.Commit(); } if (mode == ProcessMode.ServerChanges) { foreach (ISqliteEntity e in lst) { toRemoveFromCache.Add(e.EntityId); } } GC.Collect(); } Cache.Clear(toRemoveFromCache); } catch { if (tran != null && !inTran) { tran.Rollback(); } throw; } }
/// <summary> /// 지정된 이름의 Database에 대해 Transaction을 시작합니다. /// </summary> /// <param name="connectionStringName">connection string name</param> /// <param name="isolationLevel">격리수준</param> private static void StartTransaction(string connectionStringName, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) { connectionStringName.ShouldNotBeWhiteSpace("connectionStringName"); if (IsDebugEnabled) { log.Debug("Start Transaction. database connectionStringName=[{0}], isolationLevel=[{1}]", connectionStringName, isolationLevel); } if (TransactionCount <= 0) { TransactionCount = 0; ActiveConnection = Connection(connectionStringName); ActiveTransaction = ActiveConnection.BeginTransaction(isolationLevel); if (IsDebugEnabled) { log.Debug("Start transaction is success for database connectionStringName=[{0}]", connectionStringName); } } TransactionCount++; }
public void Delete(IDbRef obj, bool inTran) { using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { using (var cmd = new SqliteCommand(String.Format("UPDATE [_{0}] SET IsTombstone = 1 WHERE [Id]=@Id", obj.TableName), tran.Connection, tran)) { cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.ExecuteNonQuery(); } if (!inTran) { String tableName = obj.TableName; using (var cmd = new SqliteCommand(String.Format("DELETE FROM __{0} WHERE [Id]=@Id", tableName), tran.Connection, tran)) { cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.ExecuteNonQuery(); } using (var cmd = new SqliteCommand(String.Format("DELETE FROM {0} WHERE [Id]=@Id AND [TableName]=@TableName", TranStatusTable), tran.Connection, tran)) { cmd.Parameters.AddWithValue("@Id", obj.ToString()); cmd.Parameters.AddWithValue("@TableName", tableName); cmd.ExecuteNonQuery(); } } tran.Commit(); } catch { tran.Rollback(); } } }
public void RollbackTransaction() { var toRemoveFromCache = new List <Guid>(); var modified = new Dictionary <string, List <string> >(); var inserted = new Dictionary <string, List <string> >(); Exec(string.Format("SELECT [Id], [TableName], [Status] FROM {0} ORDER BY [TableName]", TranStatusTable), cmd => { using (SqliteDataReader r = cmd.ExecuteReader()) { while (r.Read()) { String id = r.GetString(0); String tableName = r.GetString(1); Operation status = (Operation)r.GetInt16(2); Dictionary <String, List <String> > dict = status != Operation.Insert ? modified : inserted; List <String> ids; if (!dict.TryGetValue(tableName, out ids)) { ids = new List <string>(); dict.Add(tableName, ids); } ids.Add(id); } } }); using (SqliteTransaction tran = ActiveConnection.BeginTransaction()) { try { foreach (KeyValuePair <String, List <String> > pair in modified) { String ids = ""; foreach (String s in pair.Value) { ids = ids + (String.IsNullOrEmpty(ids) ? String.Format("'{0}'", s) : String.Format(",'{0}'", s)); toRemoveFromCache.Add(DbRef.FromString(s).Id); } Exec(string.Format("INSERT OR REPLACE INTO _{0} SELECT * FROM __{0} WHERE [Id] IN ({1})", pair.Key, ids) , cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); }); Exec(string.Format("DELETE FROM __{0}", pair.Key), cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); }); } foreach (KeyValuePair <String, List <String> > pair in inserted) { string ids = ""; foreach (string s in pair.Value) { ids = ids + (String.IsNullOrEmpty(ids) ? String.Format("'{0}'", s) : String.Format(",'{0}'", s)); toRemoveFromCache.Add(DbRef.FromString(s).Id); } Exec(string.Format("DELETE FROM _{0} WHERE [Id] IN ({1})", pair.Key, ids), cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); }); } Exec(string.Format("DELETE FROM {0}", TranStatusTable), cmd => { cmd.Transaction = tran; cmd.ExecuteNonQuery(); }); tran.Commit(); _cache.Clear(toRemoveFromCache); } catch { tran.Rollback(); throw; } } }
public void Begin(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) { ActiveConnection.Open(); ActiveTransaction = ActiveConnection.BeginTransaction(isolationLevel); }