예제 #1
0
        public AcquireLockResult AcquireLock(LockMode mode, TimeSpan timeout)
        {
            Thread.BeginCriticalRegion();
            AcquireLockResult result;

            try
            {
                if (mode == LockMode.Shared)
                {
                    result = AcquireSharedLockCore(timeout);
                }
                else if (mode == LockMode.Update)
                {
                    result = AcquireUpdateLockCore(timeout);
                }
                else if (mode == LockMode.Exclusive)
                {
                    result = AcquireExclusiveLockCore(timeout);
                }
                else
                {
                    throw new NotSupportedException(mode.ToString());
                }
            }
            finally
            {
                Thread.EndCriticalRegion();
            }
            return(result);
        }
예제 #2
0
        public IDisposable TakeLock(TimeSpan takeLockTimeout, LockMode lockMode)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                SqlCommand sqlCommand = new SqlCommand("sp_getapplock", _sqlConnection);
                sqlCommand.CommandType    = CommandType.StoredProcedure;
                sqlCommand.CommandTimeout = (int)takeLockTimeout.TotalSeconds;

                sqlCommand.Parameters.AddWithValue("Resource", _uniqueId);
                sqlCommand.Parameters.AddWithValue("LockOwner", "Session");
                sqlCommand.Parameters.AddWithValue("LockMode", lockMode.ToString());
                sqlCommand.Parameters.AddWithValue("LockTimeout", (Int32)takeLockTimeout.TotalMilliseconds);

                SqlParameter returnValue = sqlCommand.Parameters.Add("ReturnValue", SqlDbType.Int);
                returnValue.Direction = ParameterDirection.ReturnValue;
                sqlCommand.ExecuteNonQuery();

                if ((int)returnValue.Value < 0)
                {
                    throw new Exception(String.Format("sp_getapplock failed with errorCode '{0}'", returnValue.Value));
                }

                _isLockTaken = true;

                transactionScope.Complete();
            }

            return(this);
        }
예제 #3
0
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("SelectOption").Append('{');
                sb.Append("resultDup").Append('=').Append(resultDup.ToString());
                sb.Append(", ").Append("highPriority").Append('=').Append(highPriority);
                sb.Append(", ").Append("straightJoin").Append('=').Append(straightJoin);
                sb.Append(", ").Append("resultSize").Append('=').Append(resultSize.ToString());
                sb.Append(", ").Append("sqlBufferResult").Append('=').Append(sqlBufferResult);
                sb.Append(", ").Append("queryCache").Append('=').Append(queryCache.ToString());
                sb.Append(", ").Append("sqlCalcFoundRows").Append('=').Append(sqlCalcFoundRows);
                sb.Append(", ").Append("lockMode").Append('=').Append(lockMode.ToString());
                sb.Append('}');
                return(sb.ToString());
            }
예제 #4
0
        public static GetAppLockReturnCode GetAppLock(IDbConnection connection, IDbTransaction transaction, string resourceName, LockMode lockMode,
                                                      LockOwner?lockOwner,
                                                      TimeSpan?lockTimeout, string databasePrincipal)
        {
            const string commandText = "sp_getapplock";

            var builder = new SqlParameterCollectionBuilder();

            builder.Add("Resource", resourceName);
            builder.Add("LockMode", lockMode.ToString());

            if (lockOwner != null)
            {
                builder.Add("LockOwner", lockOwner.ToString());
            }

            if (lockTimeout != null)
            {
                builder.Add("LockTimeout", (int)lockTimeout.Value.TotalMilliseconds);
            }

            if (databasePrincipal != null)
            {
                builder.Add("DbPrincipal", databasePrincipal);
            }

            var returnCodeParameter = new SqlParameter
            {
                //ParameterName = "returnCode",
                //SqlDbType = SqlDbType.Int,
                Direction = ParameterDirection.ReturnValue
            };

            builder.Add(returnCodeParameter);
            var parameters = builder.ToReadOnlyCollection();

            var createCommandRequest = new CreateCommandRequest(commandText, parameters, CommandType.StoredProcedure, null, transaction);

            connection.CreateCommandExecutor().ExecuteNonQuery(createCommandRequest);

            var returnCode = (GetAppLockReturnCode)(int)returnCodeParameter.Value;

            return(returnCode);
        }
		private IUniqueEntityLoader GetAppropriateLoader(LockMode lockMode, ISessionImplementor session)
		{
			IDictionary<string, IFilter> enabledFilters = session.EnabledFilters;
			if (queryLoader != null)
			{
				return queryLoader;
			}
			else if (enabledFilters == null || enabledFilters.Count == 0)
			{
				if (!string.IsNullOrEmpty(session.FetchProfile) && LockMode.Upgrade.GreaterThan(lockMode))
				{
					return loaders[session.FetchProfile];
				}
				else
				{
					return loaders[lockMode.ToString()];
				}
			}
			else
			{
				return CreateEntityLoader(lockMode, enabledFilters);
			}
		}