internal static TransactionContext EnsureTransaction(ref Transaction transaction, IDatabase database) { TransactionContext result = null; if (transaction != null) { if (transaction.Aborted) { throw new System.Transactions.TransactionAbortedException(); } // Just a mock result result = new TransactionContext(); } else { // Create a local transaction for the current operation System.Transactions.CommittableTransaction localTransaction = CreateDefaultTransaction(); transaction = Create(localTransaction); result = new TransactionContext(localTransaction); } transaction.Subscribe(database.DatabaseEngine.TransactionHandler); // database.DatabaseEngine.TransactionHandler.EnsureSubscription(transaction); return result; }
public IList<Item> GetAllForRelease(Transaction transaction) { HashSet<Item> entries = null; if (this.collection.TryRemove(transaction, out entries)) { return entries.ToArray(); } else { return new Item[0]; } }
private void ReleaseResources(Transaction transaction) { TransactionLog log; this.transactionLogs.TryRemove(transaction, out log); if (log != null) { log.Release(); } this.database.DatabaseEngine.ConcurrencyManager.ReleaseAllLocks(transaction); }
public void AcquireTableReadLock(ITable table, Transaction transaction) { ILock tableLock = this.tableLocks[table]; var lockInfo = this.lockInventory.GetLockInformation(tableLock, transaction); if (!lockInfo.IsReadLockHeld) { if (!lockInfo.IsWriteLockHeld) { // TODO: OnWaiting tableLock.EnterRead(); // TODO: OnAcquired } lockInfo.IsReadLockHeld = true; } }
// TODO: Reimagine public Item GetLockInformation(ILock l, Transaction transaction) { var set = this.GetLocks(transaction); // TODO: Perf var lockInfo = set.FirstOrDefault(x => x.Lock.Equals(l)); if (lockInfo != null) { return lockInfo; } else { var newLockInfo = new Item(l); set.Add(newLockInfo); return newLockInfo; } }
private static object CreateAndInsertEntity(ITable table, IList<MemberBinding> memberBindings, Transaction transaction) { LambdaExpression expression = Expression.Lambda( Expression.MemberInit( Expression.New(table.EntityType), memberBindings)); Delegate factory = expression.Compile(); object newEntity = factory.DynamicInvoke(); DatabaseReflectionHelper.InsertEntity(table, newEntity, transaction); return newEntity; }
public void AcquireTableReadLock(ITable table, Transaction transaction) { }
public void Rollback(Transaction transaction) { GetTransactionLog(transaction).Rollback(); this.ReleaseResources(transaction); }
public ITransactionLog GetTransactionLog(Transaction transaction) { return this.transactionLogs.GetOrAdd(transaction, tran => new TransactionLog()); }
public void ReleaseTableReadLock(ITable table, Transaction transaction) { if (transaction.IsolationLevel == IsolationLevels.RepetableRead) { return; } ILock tableLock = this.tableLocks[table]; var lockInfo = this.lockInventory.GetLockInformation(tableLock, transaction); // Releated tables are locked until the transaction ends if (!lockInfo.IsRelatedTable && lockInfo.IsReadLockHeld) { if (!lockInfo.IsWriteLockHeld) { tableLock.ExitRead(); // TODO: OnReleased } lockInfo.IsReadLockHeld = false; } }
public void ReleaseAllLocks(Transaction transaction) { foreach (var item in this.lockInventory.GetAllForRelease(transaction)) { if (item.IsWriteLockHeld) { item.Lock.ExitWrite(); } else if (item.IsReadLockHeld) { item.Lock.ExitRead(); } // TODO: Graph ////lockGraph.RemoveConnection(item.Lock, transaction); } }
public void AcquireTableWriteLock(ITable table, Transaction transaction) { IList<ITable> tables = this.database.Tables.GetAllTables(); ILock tableLock = this.tableLocks[table]; var lockInfo = this.lockInventory.GetLockInformation(tableLock, transaction); if (!lockInfo.IsWriteLockHeld) { // TODO: OnWaiting if (lockInfo.IsReadLockHeld) { tableLock.Upgrade(); } else { tableLock.EnterWrite(); } // TODO: OnReleased lockInfo.IsWriteLockHeld = true; } ////switch (this.deadlockManagement) ////{ //// #region Deadlock detection //// case DeadlockManagementStrategies.DeadlockDetection: //// if (!tableLock.IsWriteLockHeld) //// { //// this.WaitsFor(tableLock); //// tableLock.EnterWriteLock(); //// this.LockAquired(tableLock); //// } //// break; //// #endregion //// #region Deadlock prevention //// case DeadlockManagementStrategies.DeadlockPrevention: //// throw new NotSupportedException(); //// if (!tableLock.IsWriteLockHeld) //// { //// for (int i = 0; i < tables.Count; i++) //// { //// if (tables[i] == table) //// { //// for (int l = i + 1; l < tables.Count; l++) //// { //// ILock otherLock = this.tableLocks[tables[l]]; //// if (!otherLock.IsWriteLockHeld) //// { //// this.WaitsFor(otherLock); //// otherLock.EnterWriteLock(); //// this.LockAquired(otherLock); //// } //// } //// this.WaitsFor(tableLock); //// tableLock.EnterWriteLock(); //// this.LockAquired(tableLock); //// break; //// } //// } //// } //// break; //// #endregion ////} }
public void ReleaseTableWriteLock(ITable table, Transaction transaction) { }
public void ReleaseTableReadLock(ITable table, Transaction transaction) { }
public void ReleaseAllLocks(Transaction transaction) { }
public void AcquireTableWriteLock(ITable table, Transaction transaction) { }
private HashSet<Item> GetLocks(Transaction transaction) { return this.collection.GetOrAdd(transaction, new HashSet<Item>()); }
public void ReleaseTableWriteLock(ITable table, Transaction transaction) { if (transaction.IsolationLevel == IsolationLevels.ReadCommited || transaction.IsolationLevel == IsolationLevels.RepetableRead) { return; } ILock tableLock = this.tableLocks[table]; var lockInfo = this.lockInventory.GetLockInformation(tableLock, transaction); if (lockInfo.IsWriteLockHeld) { if (lockInfo.IsReadLockHeld) { tableLock.Downgrade(); } else { tableLock.ExitWrite(); // TODO: OnReleased } lockInfo.IsWriteLockHeld = false; } }
public void Commit(Transaction transaction) { this.ReleaseResources(transaction); }
private void LockAquired(ILock l, Transaction transaction) { this.lockGraph.RemoveConnection(transaction, l); this.lockGraph.AddConnection(l, transaction); }
private void WaitsFor(ILock l, Transaction transaction) { this.lockGraph.AddConnection(transaction, l); if (this.lockGraph.HasCycle()) { // Deadlock detection throw new DeadlockException(); } }
public void AcquireRelatedTableLock(ITable table, Transaction transaction) { }