BlueCollar.SQLiteRepository.GetScheduleEnqueueingLock C# (CSharp) Method

GetScheduleEnqueueingLock() public method

Attempts to obtain the enqueueing lock for the given schedule ID.
public GetScheduleEnqueueingLock ( long scheduleId, System.DateTime forceIfOlderThan, IDbTransaction transaction ) : bool
scheduleId long The ID of the schedule to obtain the schedule enqueueing lock for.
forceIfOlderThan System.DateTime A date to compare the enqueue lock's last updated date with. If /// the lock is older than the given date, then it will be forced and acquired by the caller.
transaction IDbTransaction The transaction to use, if applicable.
return bool
        public bool GetScheduleEnqueueingLock(long scheduleId, DateTime forceIfOlderThan, IDbTransaction transaction)
        {
            const string SelectSql =
            @"SELECT [Enqueueing], [EnqueueingUpdatedOn]
            FROM [BlueCollarSchedule]
            WHERE
            [Id] = @Id;";

            const string UpdateSql =
            @"UPDATE [BlueCollarSchedule]
            SET
            [Enqueueing] = @Enqueueing,
            [EnqueueingUpdatedOn] = @EnqueueingUpdatedOn
            WHERE
            [Id] = @Id;";

            bool obtained = false, commitRollback = false;

            if (transaction == null)
            {
                transaction = this.BeginTransaction(IsolationLevel.Serializable);
                commitRollback = true;
            }

            try
            {
                EnqueueingRecord enqueueing = this.connection.Query<EnqueueingRecord>(
                     SelectSql,
                     new { Id = scheduleId },
                     transaction,
                     true,
                     null,
                     null).First();

                if (!enqueueing.Enqueueing || enqueueing.EnqueueingUpdatedOn == null || enqueueing.EnqueueingUpdatedOn <= forceIfOlderThan)
                {
                    obtained = true;

                    this.connection.Execute(
                        UpdateSql,
                        new { Id = scheduleId, Enqueueing = true, EnqueueingUpdatedOn = DateTime.UtcNow },
                        transaction,
                        null,
                        null);
                }

                if (commitRollback)
                {
                    transaction.Commit();
                }
            }
            catch
            {
                if (commitRollback)
                {
                    transaction.Rollback();
                }

                throw;
            }

            return obtained;
        }