You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-22Lines changed: 46 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,14 +15,15 @@ The usage for both is identical, with only the import being different based on t
15
15
16
16
#### Locking Scopes (maps to the `@LockOwner` parameter of `sp_getapplock`):
17
17
There are two scopes for Locks that are supported:
18
-
- Session Scope (requires expclit release; implemented as IDisposable/IAsyncDisposable to provide reliable release via C# `using` pattern)
19
-
- Transaction Scope (can be optionally released, but will automatically be released by SqlServer when Transaction is Commited/Rolled-back/Closed).
18
+
- Session Scope (will automatically be released by Sql Server when the Sql Connection is disposed/closed; or may be optionally explicitly released).
19
+
- Transaction Scope (Will automatically be released by Sql Server when Sql Transaction is Commited/Rolled-back/Closed; or can be optionally explicitly released).
20
+
21
+
Note: Explicit release can be done anytime from the `SqlServerAppLock` class returned from an acquired lock, and is also intrinsically done via IDisposable/IAsyncDisposable on the `SqlServerAppLock` class to provide reliable release when scope closes via C# `using` pattern.
20
22
21
23
#### Usage Notes:
22
-
- The generally recommended approach is to use the *Transaction* scope because it is slightly safer (e.g. more resilient agains
24
+
- The generally recommended approach is to use the *Transaction* scope because it is slightly safer (e.g. more resilient against
23
25
abandoned locks) by allowing the Locks to automatically expire with the Transaction; and is the default behavior of Sql Server.
24
-
- However the *Session* scope is reliably implemented via IDisposable/IAsyncDisposable C# interfaces leaving
25
-
the main residual risk of Database communication or Network issues, whereby if we can't communicate with the DB then we can't explicity release the lock.
26
+
- However the *Session* scope is reliably implemented as long as you always close/dispose of the connection and/or via the `SqlServerAppLock` class; which also implements IDisposable/IAsyncDisposable C# interfaces.
26
27
- The lock _acquisition timeout_ value is the value (in seconds) for which Sql Server will try and wait for Lock Acquisition. By specifying Zero
27
28
(0 seconds) then Sql Server will attempt to get the lock but immediately fail lock acquisition and return if it cannot
28
29
acquire the lock.
@@ -33,10 +34,10 @@ acquire the lock.
33
34
34
35
#### Use Cases:
35
36
- Provide a lock implementation similar to C# `lock (...) {}` but on a distributed scale across many instances of an
-Many more I'm sure... but these are the ones that I've implemented in enterprises.
40
+
- I'm sure there are many more... but these are the best examples that I've needed to implement in enterprises.
40
41
41
42
42
43
## Nuget Package
@@ -62,9 +63,34 @@ using SqlAppLockHelper.SystemDataNS;
62
63
Usage is very simple by using custom extensions of the SqlConnection or SqlTransaction. The following example shows
63
64
the recommended usage of Transaction Scope by calling `.AcquireAppLockAsync(...)` on the SqlTransaction instance:
64
65
65
-
*NOTE:* Async is recommended, but the sync implementation works exactly the same -- sans async/await.
66
+
*NOTES:*
67
+
- Async is recommended, but the sync implementation works exactly the same -- sans async/await.
68
+
- Default behavior is to throw a `SqlServerAppLockAcquisitionException` when lock acquisition fails but this can be controlled via `throwsException` parameter.
66
69
67
-
#### Using Sql Transaction (Transaction Scope will be used):
70
+
#### Using Sql Transaction (Transaction Scope will be used) - Default behavior will throw an Exception:
71
+
```csharp
72
+
//Attempt Acquisition of Lock and Handle Exception if Lock cannot be acquired...
0 commit comments