public bool VerifyLockOwnership(string lockName, string lockOwner) { // otherwise issue the release command var connection = GetConnection(); if (_connectionString != null) { connection.Open(); } else if (connection == null) { throw new InvalidOperationException("The transaction had been disposed"); } else if (connection.State != ConnectionState.Open) { throw new InvalidOperationException("The connection is not open"); } var id = Guid.Parse(_encryptor.Decrypt(lockOwner)); using (var checkCommand = SqlHelpers.CreateCheckApplicationLockCommand(connection, 1000, lockName, id)) { var exists = (int)checkCommand.ExecuteScalar() > 0; return(exists); } }
public LockReleaseResult ReleaseLock(string @lock, string lockOwner) { var lockName = ToSafeLockName(@lock, MaxLockNameLength, s => s); // otherwise issue the release command var connection = GetConnection(); if (_connectionString != null) { connection.Open(); } else if (connection == null) { throw new InvalidOperationException("The transaction had been disposed"); } else if (connection.State != ConnectionState.Open) { throw new InvalidOperationException("The connection is not open"); } var id = Guid.Parse(_encryptor.Decrypt(lockOwner)); using (var checkCommand = SqlHelpers.CreateCheckApplicationLockCommand(connection, 1000, lockName, id)) { var exists = (int)checkCommand.ExecuteScalar() > 0; if (!exists) { return(new LockReleaseResult { Success = false, Reason = ReleaseLockFailure.OwnerNotMatching }); } SqlParameter deleteReturnValue; using ( var releaseCommand = SqlHelpers.CreateDeleteApplicationLockCommand(connection, 1000, lockName, id, out deleteReturnValue)) { releaseCommand.ExecuteNonQuery(); } var success = (int)deleteReturnValue.Value == 0; return(success ? new LockReleaseResult { Success = true, Reason = ReleaseLockFailure.Undefined } : new LockReleaseResult { Success = false, Reason = ReleaseLockFailure.ReleaseError }); } }