Esempio n. 1
0
        /// <summary>
        /// 加锁。返回新加的锁,或者原来锁的状态。成功与否,检查AMSCheckLockResult的Available属性
        /// </summary>
        /// <param name="lockData"></param>
        /// <returns></returns>
        public AMSCheckLockResult AddLock(AMSLock lockData)
        {
            AMSCheckLockResult result = null;
            lockData.LockTime = DateTime.MinValue;

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                //插入是否成功,判断锁是否已经存在
                AMSLock lockInDB = Insert(lockData);

                if (lockInDB == null)
                {
                    //更新是否成功,如果不成功,表示锁被占用。
                    bool updated = false;

                    lockInDB = Update(lockData, false, out updated);

                    if (updated == false)
                        result = BuildNotAvailableResult(lockInDB);
                    else
                        result = BuildAvailableResult(lockInDB, true);
                }
                else
                    result = BuildAvailableResult(lockInDB, false);

                scope.Complete();
            }

            return result;
        }
Esempio n. 2
0
        /// <summary>
        /// 准备事件相关的锁
        /// </summary>
        /// <returns></returns>
        public static AMSLock PrepareEventLock(AMSEvent eventData)
        {
            AMSLock lockData = new AMSLock();

            lockData.LockID = eventData.ID;
            lockData.LockType = AMSLockType.EventLock;
            lockData.Description = string.Format("Add lock for event(ID: {0}, Name: {1})", eventData.ID, eventData.Name);

            return lockData;
        }
Esempio n. 3
0
        public void AddNewLockTest()
        {
            AMSLock lockData = new AMSLock();
            lockData.LockID = UuidHelper.NewUuidString();

            AMSCheckLockResult result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            Assert.IsTrue(result.Available);
            Assert.IsTrue(result.Lock.LockTime > DateTime.MinValue);
        }
Esempio n. 4
0
        public void AddExistedNewLockTest()
        {
            AMSLock lockData = new AMSLock();
            lockData.LockID = UuidHelper.NewUuidString();

            AMSCheckLockResult result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            //重复加锁
            result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            Assert.IsFalse(result.Available);
            Assert.AreEqual(AMSCheckLockStatus.Locked, result.LockStatus);
        }
Esempio n. 5
0
        public void AddExistedExpiredNewLockTest()
        {
            AMSLock lockData = new AMSLock();
            lockData.EffectiveTime = TimeSpan.FromSeconds(1);
            lockData.LockID = UuidHelper.NewUuidString();

            AMSCheckLockResult result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            Thread.Sleep(1100);

            //重复加锁
            result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            Assert.IsTrue(result.Available);
            Assert.AreEqual(AMSCheckLockStatus.LockExpired, result.LockStatus);
            Assert.IsTrue(result.Lock.LockTime > DateTime.MinValue);
        }
Esempio n. 6
0
        public void ExtendExistedExpiredLockTest()
        {
            AMSLock lockData = new AMSLock();
            lockData.LockID = UuidHelper.NewUuidString();
            lockData.EffectiveTime = TimeSpan.FromSeconds(1);

            AMSCheckLockResult result = AMSLockSqlAdapter.Instance.AddLock(lockData);

            Thread.Sleep(1100);

            //延长
            AMSCheckLockResult extendedResult = AMSLockSqlAdapter.Instance.ExtendLockTime(lockData);

            Assert.IsTrue(extendedResult.Lock.LockTime > result.Lock.LockTime);

            Console.WriteLine("Original time: {0:yyyy-MM-dd HH:mm:ss.ffff}, extended time: {1:yyyy-MM-dd HH:mm:ss.ffff}",
                result.Lock.LockTime, extendedResult.Lock.LockTime);
        }
Esempio n. 7
0
        public void DeleteLock(AMSLock lockData)
        {
            lockData.NullCheck("lockData");

            DeleteLock(lockData.LockID);
        }
Esempio n. 8
0
        private static string PrepareUpdateData(AMSLock lockData, bool forceOverride)
        {
            ORMappingItemCollection mappingInfo = GetMappingInfo();

            UpdateSqlClauseBuilder uBuilder = ORMapping.GetUpdateSqlClauseBuilder(lockData, mappingInfo);
            WhereSqlClauseBuilder wBuilder = ORMapping.GetWhereSqlClauseBuilderByPrimaryKey(lockData, mappingInfo);

            if (forceOverride == false)
                wBuilder.AppendItem("LockTime", "DATEADD(SECOND, -EffectiveTime, GETUTCDATE())", "<", true);

            StringBuilder sql = new StringBuilder();

            sql.AppendFormat("UPDATE {0} SET {1} WHERE {2}",
                mappingInfo.TableName,
                uBuilder.ToSqlString(TSqlBuilder.Instance),
                wBuilder.ToSqlString(TSqlBuilder.Instance));

            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);

            sql.Append("SELECT @@ROWCOUNT");

            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);

            sql.AppendFormat("SELECT * FROM {0} WHERE LockID = {1}",
                GetMappingInfo().TableName,
                TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockData.LockID));

            return sql.ToString();
        }
Esempio n. 9
0
        private static AMSLock Update(AMSLock lockData, bool forceOverride, out bool updated)
        {
            DataSet ds = DbHelper.RunSqlReturnDS(PrepareUpdateData(lockData, forceOverride), GetConnectionName());

            (ds.Tables[1].Rows.Count > 0).FalseThrow("不能找到ID为{0}的锁信息", lockData.LockID);

            updated = (int)ds.Tables[0].Rows[0][0] > 0;

            AMSLock result = new AMSLock();

            ORMapping.DataRowToObject(ds.Tables[1].Rows[0], GetMappingInfo(), result);

            return result;
        }
Esempio n. 10
0
        private static AMSLock Insert(AMSLock lockData)
        {
            StringBuilder sql = new StringBuilder();

            sql.Append(ORMapping.GetInsertSql(lockData, TSqlBuilder.Instance));
            sql.Append(TSqlBuilder.Instance.DBStatementSeperator);
            sql.AppendFormat("SELECT * FROM {0} WHERE LockID = {1}",
                GetMappingInfo().TableName,
                TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockData.LockID));

            AMSLock result = null;

            try
            {
                DataTable table = DbHelper.RunSqlReturnDS(sql.ToString(), GetConnectionName()).Tables[0];

                result = new AMSLock();

                ORMapping.DataRowToObject(table.Rows[0], GetMappingInfo(), result);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number != 2627)
                    throw;
            }

            return result;
        }
Esempio n. 11
0
        private static AMSLock GetLockInfo(string lockID)
        {
            string sql = string.Format("SELECT * FROM {0} WHERE LockID = {1}",
                GetMappingInfo().TableName, TSqlBuilder.Instance.CheckUnicodeQuotationMark(lockID));

            DataTable table = DbHelper.RunSqlReturnDS(sql, GetConnectionName()).Tables[0];

            (table.Rows.Count > 0).FalseThrow("不能根据\"{0}\"找到对应的锁信息", lockID);

            AMSLock result = new AMSLock();

            ORMapping.DataRowToObject(table.Rows[0], GetMappingInfo(), result);

            return result;
        }
Esempio n. 12
0
        private static AMSCheckLockResult BuildNotAvailableResult(AMSLock lockData)
        {
            AMSCheckLockResult result = new AMSCheckLockResult();

            result.Lock = lockData;
            result.LockStatus = AMSCheckLockStatus.Locked;

            return result;
        }
Esempio n. 13
0
        private static AMSCheckLockResult BuildAvailableResult(AMSLock lockData, bool overrideLock)
        {
            AMSCheckLockResult result = new AMSCheckLockResult();

            result.Lock = lockData;

            if (overrideLock)
                result.LockStatus = AMSCheckLockStatus.LockExpired;
            else
                result.LockStatus = AMSCheckLockStatus.NotLocked;

            return result;
        }
Esempio n. 14
0
        /// <summary>
        /// 延长锁的时间
        /// </summary>
        /// <param name="lockData"></param>
        /// <returns></returns>
        public AMSCheckLockResult ExtendLockTime(AMSLock lockData)
        {
            AMSCheckLockResult result = null;
            lockData.LockTime = DateTime.MinValue;

            using (TransactionScope scope = TransactionScopeFactory.Create())
            {
                bool updated = false;

                AMSLock lockInDB = lockInDB = Update(lockData, true, out updated);

                if (updated == false)
                    result = BuildNotAvailableResult(lockData);
                else
                    result = BuildAvailableResult(lockInDB, true);

                scope.Complete();
            }

            return result;
        }