Skip to content

Commit 5a3bbdd

Browse files
authored
Fix Example Code sampels
Fixes example code samples to show default behavior with Exception handling.
1 parent 14ed4df commit 5a3bbdd

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,34 @@ using SqlAppLockHelper.SystemDataNS;
6363
Usage is very simple by using custom extensions of the SqlConnection or SqlTransaction. The following example shows
6464
the recommended usage of Transaction Scope by calling `.AcquireAppLockAsync(...)` on the SqlTransaction instance:
6565

66-
*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.
6769

68-
#### 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...
73+
try
74+
{
75+
await using var sqlConn = new SqlConnection(sqlConnectionString);
76+
await sqlConn.OpenAsync();
77+
78+
await using var sqlTrans = (SqlTransaction)await sqlConn.BeginTransactionAsync();
79+
80+
//Using any SqlTransaction (cast DbTransaction to SqlTransaction if needed), this will
81+
// attempt to acquire a distributed mutex/lock, and will wait up to 5 seconds before timing out.
82+
await using var appLock = await sqlTrans.AcquireAppLockAsync("MyAppBulkLoadingDistributedLock", 5);
83+
84+
//.... Custom logic that should only occur when a lock is held....
85+
86+
}
87+
catch (SqlServerAppLockAcquisitionException appLockException)
88+
{
89+
//.... A lock could not be acquired so handle as needed....
90+
}
91+
```
92+
93+
#### Using Sql Transaction (Transaction Scope will be used) - Without Exception Handling:
6994
```csharp
7095
await using var sqlConn = new SqlConnection(sqlConnectionString);
7196
await sqlConn.OpenAsync();
@@ -74,34 +99,34 @@ the recommended usage of Transaction Scope by calling `.AcquireAppLockAsync(...)
7499

75100
//Using any SqlTransaction (cast DbTransaction to SqlTransaction if needed), this will
76101
// attempt to acquire a distributed mutex/lock, and will wait up to 5 seconds before timing out.
77-
//Note: Default behavior is to throw and exception if the Lock cannot be acquired
78-
// (e.g. is already held by another process) but this can be overridden by parameter
79-
// to return the state in the appLock result.
80-
await using var appLock = await sqlTrans.AcquireAppLockAsync("MyAppBulkLoadingDistributedLock", 5);
102+
//Note: Default behavior is to throw and exception but this is controlled via throwsException param
103+
// and can then be managed via the returned the SqlServerAppLock result.
104+
await using var appLock = await sqlTrans.AcquireAppLockAsync(
105+
"MyAppBulkLoadingDistributedLock", 5, throwsException: false);
81106

82107
if(appLock.IsAcquired)
83108
{
84-
//.... Custom Lock that should only occur when a lock is held....
109+
//.... Custom logic that should only occur when a lock is held....
85110
}
86111

87112
```
88-
#### Using Sql Connection (Session Scope will be used):
113+
#### Using Sql Connection (Session Scope will be used) - Without Exception Handling:
89114
_*NOTE: *Application Lock should ALWAYS be explicity Disposed of to ensure Lock is released**_
90115
```csharp
91116
await using var sqlConn = new SqlConnection(sqlConnectionString);
92117
await sqlConn.OpenAsync();
93118

94119
//Using any SqlTransaction (cast DbTransaction to SqlTransaction if needed), this will
95120
// attempt to acquire a distributed mutex/lock, and will wait up to 5 seconds before timing out.
96-
//Note: Default behavior is to throw and exception if the Lock cannot be acquired
97-
// (e.g. is already held by another process) but this can be overridden by parameter
98-
// to return the state in the appLock result.
121+
//Note: Default behavior is to throw and exception but this is controlled via throwsException param
122+
// and can then be managed via the returned the SqlServerAppLock result.
99123
//Note: The IDisposable/IAsyncDisposable implementation ensures that the Lock is released!
100-
await using var appLock = await sqlConn.AcquireAppLockAsync("MyAppBulkLoadingDistributedLock", 5);
124+
await using var appLock = await sqlConn.AcquireAppLockAsync(
125+
"MyAppBulkLoadingDistributedLock", 5throwsException: false);
101126

102127
if(appLock.IsAcquired)
103128
{
104-
//.... Custom Lock that should only occur when a lock is held....
129+
//.... Custom logic that should only occur when a lock is held....
105130
}
106131

107132
```

0 commit comments

Comments
 (0)