Beispiel #1
0
        /// <summary>
        /// 获得序列
        /// </summary>
        /// <param name="fullName">序列名</param>
        /// <param name="defaultSequence">默认序列</param>
        /// <returns>序列实体</returns>
        protected BaseSequenceEntity GetSequenceByLock(string fullName, int defaultSequence)
        {
            BaseSequenceEntity sequenceEntity = new BaseSequenceEntity();

            // 这里主要是为了判断是否存在
            sequenceEntity = this.GetEntityByName(fullName);
            if (sequenceEntity == null)
            {
                // 这里添加记录时加锁机制。
                // 是否已经被锁住
                this.ReturnStatusCode = StatusCode.CanNotLock.ToString();
                for (int i = 0; i < BaseSystemInfo.LockNoWaitCount; i++)
                {
                    // 被锁定的记录数
                    int lockCount = DbLogic.LockNoWait(DbHelper, BaseSequenceEntity.TableName, new KeyValuePair <string, object>(BaseSequenceEntity.FieldFullName, BaseSequenceEntity.TableName));
                    if (lockCount > 0)
                    {
                        sequenceEntity.FullName  = fullName;
                        sequenceEntity.Reduction = defaultSequence - 1;
                        sequenceEntity.Sequence  = defaultSequence;
                        sequenceEntity.Step      = DefaultStep;
                        this.AddEntity(sequenceEntity);

                        this.ReturnStatusCode = StatusCode.LockOK.ToString();
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(BaseRandom.GetRandom(1, BaseSystemInfo.LockNoWaitTickMilliSeconds));
                    }
                }
                if (this.ReturnStatusCode == StatusCode.LockOK.ToString())
                {
                    // JiRiGaLa 这个是否能省略
                    sequenceEntity = this.GetEntityByName(fullName);
                }
            }
            else
            {
                // 若记录已经存在,加锁,然后读取记录。
                // 是否已经被锁住
                this.ReturnStatusCode = StatusCode.CanNotLock.ToString();
                for (int i = 0; i < BaseSystemInfo.LockNoWaitCount; i++)
                {
                    // 被锁定的记录数
                    int lockCount = DbLogic.LockNoWait(DbHelper, BaseSequenceEntity.TableName, new KeyValuePair <string, object>(BaseSequenceEntity.FieldFullName, fullName));
                    if (lockCount > 0)
                    {
                        sequenceEntity        = this.GetEntityByName(fullName);
                        this.ReturnStatusCode = StatusCode.LockOK.ToString();
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(BaseRandom.GetRandom(1, BaseSystemInfo.LockNoWaitTickMilliSeconds));
                    }
                }
            }
            return(sequenceEntity);
        }