Ejemplo n.º 1
0
        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];
     }
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        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;
            }
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
 public void AcquireTableReadLock(ITable table, Transaction transaction)
 {
 }
Ejemplo n.º 8
0
        public void Rollback(Transaction transaction)
        {
            GetTransactionLog(transaction).Rollback();

            this.ReleaseResources(transaction);
        }
Ejemplo n.º 9
0
 public ITransactionLog GetTransactionLog(Transaction transaction)
 {
     return this.transactionLogs.GetOrAdd(transaction, tran => new TransactionLog());
 }
Ejemplo n.º 10
0
        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;
            }
        }
Ejemplo n.º 11
0
        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);
            }
        }
Ejemplo n.º 12
0
        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
            ////}
        }
Ejemplo n.º 13
0
 public void ReleaseTableWriteLock(ITable table, Transaction transaction)
 {
 }
Ejemplo n.º 14
0
 public void ReleaseTableReadLock(ITable table, Transaction transaction)
 {
 }
Ejemplo n.º 15
0
 public void ReleaseAllLocks(Transaction transaction)
 {
 }
Ejemplo n.º 16
0
 public void AcquireTableWriteLock(ITable table, Transaction transaction)
 {
 }
 private HashSet<Item> GetLocks(Transaction transaction)
 {
     return this.collection.GetOrAdd(transaction, new HashSet<Item>());
 }
Ejemplo n.º 18
0
        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;
            }
        }
Ejemplo n.º 19
0
 public void Commit(Transaction transaction)
 {
     this.ReleaseResources(transaction);
 }
Ejemplo n.º 20
0
 private void LockAquired(ILock l, Transaction transaction)
 {
     this.lockGraph.RemoveConnection(transaction, l);
     this.lockGraph.AddConnection(l, transaction);
 }
Ejemplo n.º 21
0
        private void WaitsFor(ILock l, Transaction transaction)
        {
            this.lockGraph.AddConnection(transaction, l);

            if (this.lockGraph.HasCycle())
            {
                // Deadlock detection
                throw new DeadlockException();
            }
        }
Ejemplo n.º 22
0
 public void AcquireRelatedTableLock(ITable table, Transaction transaction)
 {
 }