Skip to content

Commit a5a7282

Browse files
authored
Merge pull request #395 from hangfire-postgres/features/386-change-lock-exception-type
Changed distributed lock exception type
2 parents 5ddbe91 + 6978f39 commit a5a7282

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/Hangfire.PostgreSql/PostgreSqlDistributedLock.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal static void Release(IDbConnection connection, string resource, PostgreS
9191

9292
if (!LockHandler.TryRemoveLock(resource, connection, options, false))
9393
{
94-
throw new PostgreSqlDistributedLockException($"Could not release a lock on the resource '{resource}'. Lock does not exist.");
94+
throw new PostgreSqlDistributedLockException(resource);
9595
}
9696
}
9797

@@ -102,14 +102,12 @@ public static void Lock(string resource, TimeSpan timeout, IDbConnection connect
102102
Stopwatch lockAcquiringTime = Stopwatch.StartNew();
103103

104104
bool tryAcquireLock = true;
105-
Exception lastException = null;
106105
Func<IDbConnection, string, string, bool> tryLock = options.UseNativeDatabaseTransactions
107106
? TransactionLockHandler.TryLock
108107
: UpdateCountLockHandler.TryLock;
109108

110109
while (tryAcquireLock)
111110
{
112-
lastException = null;
113111
if (connection.State != ConnectionState.Open)
114112
{
115113
connection.Open();
@@ -126,7 +124,6 @@ public static void Lock(string resource, TimeSpan timeout, IDbConnection connect
126124
}
127125
catch (Exception ex)
128126
{
129-
lastException = ex;
130127
Log(resource, "Failed to acquire lock", ex);
131128
}
132129

@@ -153,7 +150,7 @@ public static void Lock(string resource, TimeSpan timeout, IDbConnection connect
153150
}
154151
}
155152

156-
throw new PostgreSqlDistributedLockException($@"Could not place a lock on the resource '{resource}': Lock timeout.", lastException);
153+
throw new PostgreSqlDistributedLockException(resource);
157154
}
158155

159156
public static bool TryRemoveLock(string resource, IDbConnection connection, PostgreSqlStorageOptions options, bool onlyExpired)

src/Hangfire.PostgreSql/PostgreSqlDistributedLockException.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
// Special thanks goes to him.
2121

2222
using System;
23+
using Hangfire.Storage;
2324

2425
namespace Hangfire.PostgreSql
2526
{
2627
[Serializable]
27-
public class PostgreSqlDistributedLockException : Exception
28+
public class PostgreSqlDistributedLockException : DistributedLockTimeoutException
2829
{
29-
public PostgreSqlDistributedLockException(string message) : base(message)
30-
{
31-
}
32-
33-
public PostgreSqlDistributedLockException(string message, Exception innerException) : base(message, innerException)
30+
public PostgreSqlDistributedLockException(string resource) : base(resource)
3431
{
3532
}
3633
}

0 commit comments

Comments
 (0)