Example #1
0
 internal Lock(LockingQueue queue, LockingMode mode, AccessType accessType)
 {
     Queue      = queue;
     AccessType = accessType;
     Mode       = mode;
     Queue.Acquire(this);
 }
Example #2
0
 internal Lock(LockingQueue queue, LockingMode mode, AccessType accessType)
 {
     Queue = queue;
     AccessType = accessType;
     Mode = mode;
     Queue.Acquire(this);
 }
Example #3
0
 internal LockEvent(LockEventType eventType, IEnumerable<ObjectName> references, LockingMode mode, AccessType accessType)
 {
     EventType = eventType;
     References = references;
     Mode = mode;
     AccessType = accessType;
 }
Example #4
0
        public LockHandle Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode)
        {
            // Set up the local constants.

            int lockCount = toRead.Length + toWrite.Length;
            LockHandle handle = new LockHandle(lockCount);

            lock (this) {
                Lock @lock;
                LockingQueue queue;

                // Add Read and Write locks to cache and to the handle.
                for (int i = toWrite.Length - 1; i >= 0; --i) {
                    var toWriteLock = toWrite[i];
                    queue = GetQueueFor(toWriteLock);

                    // slightly confusing: this will add Lock to given table queue
                    @lock = new Lock(queue, mode, AccessType.Write);
                    @lock.Acquire();
                    handle.AddLock(@lock);
                }

                for (int i = toRead.Length - 1; i >= 0; --i) {
                    var toReadLock = toRead[i];
                    queue = GetQueueFor(toReadLock);

                    // slightly confusing: this will add Lock to given table queue
                    @lock = new Lock(queue, mode, AccessType.Read);
                    @lock.Acquire();
                    handle.AddLock(@lock);
                }
            }

            return handle;
        }
Example #5
0
 public static void Lock(this ISession session, IEnumerable <ObjectName> objectNames, AccessType accessType,
                         LockingMode mode, int timeout)
 {
     if (session.Transaction != null)
     {
         session.Transaction.Lock(objectNames, accessType, mode, timeout);
     }
 }
Example #6
0
 public Lock NewLock(LockingMode mode, AccessType accessType)
 {
     lock (this) {
         var @lock = new Lock(this, mode, accessType);
         Acquire(@lock);
         @lock.OnAcquired();
         return @lock;
     }
 }
Example #7
0
        public void LockingModeAsync_GivenInvalidLockingModeValue_ThrowsArgumentException()
        {
            var connection = CreateConnectionFactory();
            var dbPragma   = CreateDatabasePragma(connection, MainSchema);

            const LockingMode newValue = (LockingMode)55;

            Assert.That(() => dbPragma.LockingModeAsync(newValue), Throws.ArgumentException);
        }
 public Lock NewLock(LockingMode mode, AccessType accessType)
 {
     lock (this) {
         var @lock = new Lock(this, mode, accessType);
         Acquire(@lock);
         @lock.OnAcquired();
         return(@lock);
     }
 }
Example #9
0
        public LockTableStatement(ObjectName tableName, LockingMode mode, int timeout)
        {
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (timeout < Timeout.Infinite)
                throw new ArgumentException("Invalid wait timeout specified", "timeout");

            TableName = tableName;
            Mode = mode;
            WaitTimeout = timeout;
        }
        internal Lock Lock(LockingMode mode, AccessType accessType)
        {
            lock (this) {
                var @lock = new Lock(this, mode, accessType);
                Locked.Locked(@lock);
                locks.Add(@lock);
                @lock.Acquire();

                return(@lock);
            }
        }
Example #11
0
        public LockHandle Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode)
        {
            lock (this) {
                int        lockCount = toRead.Length + toWrite.Length;
                LockHandle handle    = new LockHandle(lockCount);

                AddToHandle(handle, toWrite, AccessType.Write, mode);
                AddToHandle(handle, toRead, AccessType.Read, mode);

                return(handle);
            }
        }
Example #12
0
        public async Task LockingModeAsync_GetAndSet_InvokesProperly()
        {
            var connection = CreateConnectionFactory();
            var dbPragma   = CreateDatabasePragma(connection, MainSchema);

            _ = await dbPragma.LockingModeAsync().ConfigureAwait(false); // should be normal

            const LockingMode newValue = LockingMode.Exclusive;
            await dbPragma.LockingModeAsync(newValue).ConfigureAwait(false);

            _ = await dbPragma.LockingModeAsync().ConfigureAwait(false);

            Assert.Pass(); // not checking value as it's a once-only effect
        }
Example #13
0
        public LockTableStatement(ObjectName tableName, LockingMode mode, int timeout)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            if (timeout < Timeout.Infinite)
            {
                throw new ArgumentException("Invalid wait timeout specified", "timeout");
            }

            TableName   = tableName;
            Mode        = mode;
            WaitTimeout = timeout;
        }
Example #14
0
        public void CombinedTest()
        {
            lockingMode    = LockingMode.None;
            isolationLevel = IsolationLevel.ReadCommitted;
            Run("Counter isn't isolated.");

            isolationLevel = IsolationLevel.RepeatableRead;
            Run("There are deadlocks or version conflicts, but counter is isolated.");

            lockingMode = LockingMode.EntityLock;
            Run("Still can be a deadlock, since lock happen after read.");

            lockingMode = LockingMode.QueryLock;
            Run("No deadlocks, counter isolation.");
        }
Example #15
0
        public LockHandle Lock(ILockable[] lockables, AccessType accessType, LockingMode mode)
        {
            lock (this) {
                int count = 0;
                if ((accessType & AccessType.Read) != 0)
                    count += lockables.Length;
                if ((accessType & AccessType.Write) != 0)
                    count += lockables.Length;

                var handle = new LockHandle(count);

                if ((accessType & AccessType.Read) != 0)
                    AddToHandle(handle, lockables, AccessType.Read, mode);

                if ((accessType & AccessType.Write) != 0)
                    AddToHandle(handle, lockables, AccessType.Write, mode);

                return handle;
            }
        }
Example #16
0
        public void Lock(IEnumerable <IDbObject> objects, AccessType accessType, LockingMode mode)
        {
            lock (Database) {
                var lockables = objects.OfType <ILockable>().ToArray();
                if (lockables.Length == 0)
                {
                    return;
                }

                // Before we can lock the objects, we must wait for them
                //  to be available...
                CheckAccess(lockables, accessType);

                var handle = Database.Locker.Lock(lockables, accessType, mode);

                if (lockHandles == null)
                {
                    lockHandles = new List <LockHandle>();
                }

                lockHandles.Add(handle);
            }
        }
Example #17
0
        private void AddToHandle(LockHandle handle, ILockable[] lockables, AccessType accessType, LockingMode mode)
        {
            if (lockables == null)
            {
                return;
            }

            for (int i = lockables.Length - 1; i >= 0; --i)
            {
                var lockable = lockables[i];
                var queue    = GetQueueFor(lockable);

                handle.AddLock(queue.NewLock(mode, accessType));
            }
        }
 public static LockHandle LockWrite(this ITransaction transaction, IEnumerable<ObjectName> tableNames, LockingMode mode)
 {
     var tables = tableNames.Select(transaction.GetTable).OfType<ILockable>().ToArray();
     return transaction.Database.DatabaseContext.Locker.Lock(tables, new ILockable[0], mode);
 }
Example #19
0
 public void Lock(IEnumerable<IDbObject> objects, AccessType accessType, LockingMode mode)
 {
     // A system session does not hold any lock
 }
Example #20
0
 internal Lock(LockingQueue queue, LockingMode mode, AccessType accessType)
 {
     Queue      = queue;
     AccessType = accessType;
     Mode       = mode;
 }
Example #21
0
        public void Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode)
        {
            lock (Database) {
                if (lockHandles == null)
                    lockHandles = new List<LockHandle>();

                var handle = Database.DatabaseContext.Locker.Lock(toWrite, toRead, mode);
                if (handle != null)
                    lockHandles.Add(handle);
            }
        }
Example #22
0
 public static void Lock(this ITransaction transaction, IEnumerable<ObjectName> tableNames, AccessType accessType, LockingMode mode, int timeout)
 {
     var lockables = tableNames.Select(transaction.FindObject);
     transaction.Lock(lockables.ToArray(), mode, timeout);
 }
Example #23
0
 public void Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode)
 {
 }
Example #24
0
 public LockHandle LockWrite(ILockable[] lockables, LockingMode mode)
 {
     return Lock(lockables, AccessType.Write, mode);
 }
Example #25
0
 public LockHandle Lock(ILockable lockable, AccessType accessType, LockingMode mode)
 {
     return Lock(new[] {lockable}, accessType, mode);
 }
Example #26
0
        public void Lock(IEnumerable<IDbObject> objects, AccessType accessType, LockingMode mode)
        {
            lock (Database) {
                var lockables = objects.OfType<ILockable>().ToArray();
                if (lockables.Length == 0)
                    return;

                // Before we can lock the objects, we must wait for them
                //  to be available...
                CheckAccess(lockables, accessType);

                var handle = Database.Locker.Lock(lockables, accessType, mode);

                if (lockHandles == null)
                    lockHandles = new List<LockHandle>();

                lockHandles.Add(handle);
            }
        }
Example #27
0
        public static void Lock(this ISession session, IEnumerable<ObjectName> objectNames, AccessType accessType,
			LockingMode mode, int timeout)
        {
            if (session.Transaction != null)
                session.Transaction.Lock(objectNames, accessType, mode, timeout);
        }
Example #28
0
        public LockHandle Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode)
        {
            lock (this) {
                int lockCount = toRead.Length + toWrite.Length;
                LockHandle handle = new LockHandle(lockCount);

                AddToHandle(handle, toWrite, AccessType.Write, mode);
                AddToHandle(handle, toRead, AccessType.Read, mode);

                return handle;
            }
        }
        protected virtual IQueryResponse[] ExecuteQuery(string text, IEnumerable <SqlQueryParameter> parameters)
        {
            // Log this Query if Query logging is enabled
            if (Logger.IsInterestedIn(LogLevel.Debug))
            {
                // Output the instruction to the _queries log.
                Logger.DebugFormat(this, "[CLIENT] [{0}] - Query: {1}", Session.User.UserName, text);
            }

            // Write debug message (Info level)
            if (Logger.IsInterestedIn(LogLevel.Debug))
            {
                Logger.DebugFormat(this, "Query From User: {0}", Session.User.UserName);
                Logger.DebugFormat(this, "Query: {0}", text.Trim());
            }

            // Get the locking mechanism.
            LockingMechanism locker   = Session.Connection.LockingMechanism;
            LockingMode      lockMode = LockingMode.None;

            IQueryResponse[] response = null;

            try {
                try {
                    // For simplicity - all database locking is now exclusive inside
                    // a transaction.  This means it is not possible to execute
                    // queries concurrently inside a transaction.  However, we are
                    // still able to execute queries concurrently from different
                    // connections.
                    //
                    // It's debatable whether we even need to perform this Lock anymore
                    // because we could change the contract of this method so that
                    // it is not thread safe.  This would require that the callee ensures
                    // more than one thread can not execute queries on the connection.
                    lockMode = LockingMode.Exclusive;
                    locker.SetMode(lockMode);

                    // Execute the Query (behaviour for this comes from super).
                    response = CoreExecuteQuery(text, parameters);

                    // Return the result.
                    return(response);
                } finally {
                    try {
                        // This is executed no matter what happens.  Very important we
                        // unlock the tables.
                        if (lockMode != LockingMode.None)
                        {
                            locker.FinishMode(lockMode);
                        }
                    } catch (Exception e) {
                        // If this throws an exception, we should output it to the debug
                        // log and screen.
                        Logger.Error(this, "Exception finishing locks");
                        Logger.Error(this, e);
                        // Note, we can't throw an error here because we may already be in
                        // an exception that happened in the above 'try' block.
                    }
                }
            } finally {
                // This always happens after tables are unlocked.
                // Also guarenteed to happen even if something fails.

                // If we are in auto-commit mode then commit the Query here.
                // Do we auto-commit?
                if (Session.Connection.AutoCommit)
                {
                    // Yes, so grab an exclusive Lock and auto-commit.
                    try {
                        // Lock into exclusive mode.
                        locker.SetMode(LockingMode.Exclusive);
                        // If an error occured then roll-back
                        if (response == null)
                        {
                            // Rollback.
                            Session.Connection.Rollback();
                        }
                        else
                        {
                            try {
                                // Otherwise commit.
                                Session.Connection.Commit();
                            } catch (Exception e) {
                                foreach (IQueryResponse queryResponse in response)
                                {
                                    // Dispose this response if the commit failed.
                                    DisposeResult(queryResponse.ResultId);
                                }

                                // And throw the SQL Exception
                                throw;
                            }
                        }
                    } finally {
                        locker.FinishMode(LockingMode.Exclusive);
                    }
                }
            }
        }
Example #30
0
 public LockTableStatement(ObjectName tableName, LockingMode mode)
     : this(tableName, mode, Timeout.Infinite)
 {
 }
Example #31
0
        private void AddToHandle(LockHandle handle, ILockable[] lockables, AccessType accessType, LockingMode mode)
        {
            if (lockables == null)
                return;

            for (int i = lockables.Length - 1; i >= 0; --i) {
                var lockable = lockables[i];
                var queue = GetQueueFor(lockable);

                handle.AddLock(queue.NewLock(mode, accessType));
            }
        }
Example #32
0
 public LockHandle LockWrite(ILockable lockable, LockingMode mode)
 {
     return(Lock(lockable, AccessType.Write, mode));
 }
 public static void LockTable(this IRequest request, ObjectName tableName, LockingMode mode, int timeout)
 {
     request.ExecuteStatement(new LockTableStatement(tableName, mode, timeout));
 }
Example #34
0
 public LockHandle LockRead(ILockable[] lockables, LockingMode mode)
 {
     return Lock(lockables, AccessType.Read, mode);
 }
        public static void Lock(this ITransaction transaction, IEnumerable <ObjectName> tableNames, AccessType accessType, LockingMode mode, int timeout)
        {
            var lockables = tableNames.Select(transaction.FindObject);

            transaction.Lock(lockables.ToArray(), mode, timeout);
        }
 public static void LockTable(this IRequest request, ObjectName tableName, LockingMode mode)
 {
     LockTable(request, tableName, mode, Timeout.Infinite);
 }
Example #37
0
 public LockTableStatement(ObjectName tableName, LockingMode mode)
     : this(tableName, mode, Timeout.Infinite)
 {
 }
Example #38
0
        public static LockHandle LockTables(this ITransaction transaction, IEnumerable <ObjectName> tableNames, AccessType accessType, LockingMode mode)
        {
            var tables = tableNames.Select(transaction.GetTable).OfType <ILockable>();

            return(transaction.Database.Locker.Lock(tables.ToArray(), accessType, mode));
        }
 public static LockHandle LockTables(this ITransaction transaction, IEnumerable<ObjectName> tableNames, AccessType accessType, LockingMode mode)
 {
     var tables = tableNames.Select(transaction.GetTable).OfType<ILockable>();
     return transaction.Database.Locker.Lock(tables.ToArray(), accessType, mode);
 }
 public static void Lock(this IUserSession session, LockingMode mode)
 {
     var lockable = new ILockable[] { session.Transaction };
     session.Lock(lockable, lockable, LockingMode.Exclusive);
 }
Example #41
0
        private void AddToHandle(LockHandle handle, ILockable[] lockables, AccessType accessType, LockingMode mode)
        {
            if (lockables == null)
                return;

            for (int i = lockables.Length - 1; i >= 0; --i) {
                var lockable = lockables[i];
                var queue = GetQueueFor(lockable);

                // slightly confusing: this will add Lock to given table queue
                var @lock = new Lock(queue, mode, accessType);
                @lock.Acquire();
                handle.AddLock(@lock);
            }
        }
Example #42
0
 public Lock(LockingQueue queue, LockingMode mode, AccessType accessType)
 {
     this.queue = queue;
     this.mode  = mode;
     AccessType = accessType;
 }
Example #43
0
        private void AddToHandle(LockHandle handle, ILockable[] lockables, AccessType accessType, LockingMode mode)
        {
            if (lockables == null)
            {
                return;
            }

            for (int i = lockables.Length - 1; i >= 0; --i)
            {
                var lockable = lockables[i];
                var queue    = GetQueueFor(lockable);

                // slightly confusing: this will add Lock to given table queue
                var @lock = new Lock(queue, mode, accessType);
                @lock.Acquire();
                handle.AddLock(@lock);
            }
        }
Example #44
0
 public LockHandle Lock(ILockable lockable, AccessType accessType, LockingMode mode)
 => Lock(new[] { lockable }, accessType, mode);
Example #45
0
 public LockHandle Lock(ILockable lockable, AccessType accessType, LockingMode mode)
 {
     return(Lock(new[] { lockable }, accessType, mode));
 }
Example #46
0
 public void Lock(IEnumerable <IDbObject> objects, AccessType accessType, LockingMode mode)
 {
     // A system session does not hold any lock
 }
Example #47
0
 public LockHandle LockRead(ILockable[] lockables, LockingMode mode)
 {
     return(Lock(lockables, AccessType.Read, mode));
 }
Example #48
0
 public void LockTable(ObjectName tableName, LockingMode mode, int waitTimeout)
 {
     Session.Lock(new [] { tableName }, AccessType.ReadWrite, mode, waitTimeout);
 }
Example #49
0
 internal Lock(LockingQueue queue, LockingMode mode, AccessType accessType)
 {
     Queue = queue;
     AccessType = accessType;
     Mode = mode;
 }
Example #50
0
 internal LockEvent(LockEventType eventType, IEnumerable <ObjectName> references, LockingMode mode, AccessType accessType)
 {
     EventType  = eventType;
     References = references;
     Mode       = mode;
     AccessType = accessType;
 }
Example #51
0
        public void Lock(IEnumerable<IDbObject> objects, LockingMode mode, int timeout)
        {
            lock (Database) {
                var lockables = objects.OfType<ILockable>().ToArray();
                if (lockables.Length == 0)
                    return;

                // Before we can lock the objects, we must wait for them
                //  to be available...
                if (lockables.Any(x => Database.Locker.IsLocked(x)))
                    Database.Locker.CheckAccess(lockables, AccessType.ReadWrite, timeout);

                var handle = Database.Locker.Lock(lockables, AccessType.ReadWrite, mode);

                if (lockHandles == null)
                    lockHandles = new List<LockHandle>();

                lockHandles.Add(handle);

                var lockedNames = objects.Where(x => x is ILockable).Select(x => x.ObjectInfo.FullName);
                Context.OnEvent(new LockEvent(LockEventType.Lock, lockedNames, LockingMode.Exclusive, AccessType.ReadWrite));
            }
        }