@@ -6,7 +6,7 @@ namespace Microsoft.AspNet.SessionState
6
6
using Resources ;
7
7
using System ;
8
8
using System . Data . SqlClient ;
9
- using System . Threading ;
9
+ using System . Runtime . CompilerServices ;
10
10
using System . Threading . Tasks ;
11
11
using System . Web ;
12
12
using System . Web . SessionState ;
@@ -47,10 +47,10 @@ Created datetime NOT NULL DEFAULT GETUTCDATE(),
47
47
)
48
48
CREATE NONCLUSTERED INDEX Index_Expires ON { SqlSessionStateRepositoryUtil . TableName } (Expires)
49
49
END" ;
50
- #endregion
50
+ #endregion
51
51
52
52
#region TempInsertUninitializedItem
53
- private static readonly string TempInsertUninitializedItemSql = $@ "
53
+ private static readonly string TempInsertUninitializedItemSql = $@ "
54
54
DECLARE @now AS datetime
55
55
DECLARE @nowLocal AS datetime
56
56
SET @now = GETUTCDATE()
@@ -75,10 +75,10 @@ DECLARE @nowLocal AS datetime
75
75
@nowLocal,
76
76
1,
77
77
1)" ;
78
- #endregion
78
+ #endregion
79
79
80
80
#region GetStateItemExclusive
81
- private static readonly string GetStateItemExclusiveSql = $@ "
81
+ private static readonly string GetStateItemExclusiveSql = $@ "
82
82
BEGIN TRAN
83
83
DECLARE @textptr AS varbinary(16)
84
84
DECLARE @length AS int
@@ -275,7 +275,7 @@ DEALLOCATE ExpiredSessionCursor
275
275
END
276
276
277
277
DROP TABLE #tblExpiredSessions" ;
278
- #endregion
278
+ #endregion
279
279
#endregion
280
280
281
281
public SqlSessionStateRepository ( string connectionString , int commandTimeout ,
@@ -310,17 +310,23 @@ internal int CommandTimeout
310
310
}
311
311
#endregion
312
312
313
- private bool CanRetry ( RetryCheckParameter parameter )
313
+ private Task < bool > CanRetryAsync ( RetryCheckParameter parameter )
314
314
{
315
315
if ( _retryIntervalMilSec <= 0 ||
316
316
! SqlSessionStateRepositoryUtil . IsFatalSqlException ( parameter . Exception ) ||
317
317
parameter . RetryCount >= _maxRetryNum )
318
318
{
319
- return false ;
319
+ return Task . FromResult ( false ) ;
320
320
}
321
321
322
+ return WaitToRetryAsync ( parameter , _retryIntervalMilSec ) ;
323
+ }
324
+
325
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
326
+ private static async Task < bool > WaitToRetryAsync ( RetryCheckParameter parameter , int retryIntervalMilSec )
327
+ {
322
328
// sleep the specified time and allow retry
323
- Thread . Sleep ( _retryIntervalMilSec ) ;
329
+ await Task . Delay ( retryIntervalMilSec ) ;
324
330
parameter . RetryCount ++ ;
325
331
326
332
return true ;
@@ -334,7 +340,7 @@ public void CreateSessionStateTable()
334
340
try
335
341
{
336
342
var cmd = _commandHelper . CreateNewSessionTableCmd ( CreateSessionTableSql ) ;
337
- var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) . ConfigureAwait ( false ) ;
343
+ var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) . ConfigureAwait ( false ) ;
338
344
task . GetAwaiter ( ) . GetResult ( ) ;
339
345
}
340
346
catch ( Exception ex )
@@ -349,7 +355,7 @@ public void DeleteExpiredSessions()
349
355
using ( var connection = new SqlConnection ( _connectString ) )
350
356
{
351
357
var cmd = _commandHelper . CreateDeleteExpiredSessionsCmd ( DeleteExpiredSessionsSql ) ;
352
- var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) . ConfigureAwait ( false ) ;
358
+ var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) . ConfigureAwait ( false ) ;
353
359
task . GetAwaiter ( ) . GetResult ( ) ;
354
360
}
355
361
}
@@ -375,7 +381,7 @@ public async Task<SessionItem> GetSessionStateItemAsync(string id, bool exclusiv
375
381
376
382
using ( var connection = new SqlConnection ( _connectString ) )
377
383
{
378
- using ( var reader = await SqlSessionStateRepositoryUtil . SqlExecuteReaderWithRetryAsync ( connection , cmd , CanRetry ) )
384
+ using ( var reader = await SqlSessionStateRepositoryUtil . SqlExecuteReaderWithRetryAsync ( connection , cmd , CanRetryAsync ) )
379
385
{
380
386
if ( await reader . ReadAsync ( ) )
381
387
{
@@ -427,7 +433,7 @@ public async Task CreateOrUpdateSessionStateItemAsync(bool newItem, string id, b
427
433
428
434
using ( var connection = new SqlConnection ( _connectString ) )
429
435
{
430
- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry , newItem ) ;
436
+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync , newItem ) ;
431
437
}
432
438
}
433
439
@@ -436,7 +442,7 @@ public async Task ResetSessionItemTimeoutAsync(string id)
436
442
var cmd = _commandHelper . CreateResetItemTimeoutCmd ( ResetItemTimeoutSql , id ) ;
437
443
using ( var connection = new SqlConnection ( _connectString ) )
438
444
{
439
- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
445
+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
440
446
}
441
447
}
442
448
@@ -445,7 +451,7 @@ public async Task RemoveSessionItemAsync(string id, object lockId)
445
451
var cmd = _commandHelper . CreateRemoveStateItemCmd ( RemoveStateItemSql , id , lockId ) ;
446
452
using ( var connection = new SqlConnection ( _connectString ) )
447
453
{
448
- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
454
+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
449
455
}
450
456
}
451
457
@@ -454,7 +460,7 @@ public async Task ReleaseSessionItemAsync(string id, object lockId)
454
460
var cmd = _commandHelper . CreateReleaseItemExclusiveCmd ( ReleaseItemExclusiveSql , id , lockId ) ;
455
461
using ( var connection = new SqlConnection ( _connectString ) )
456
462
{
457
- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
463
+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
458
464
}
459
465
}
460
466
@@ -463,10 +469,10 @@ public async Task CreateUninitializedSessionItemAsync(string id, int length, byt
463
469
var cmd = _commandHelper . CreateTempInsertUninitializedItemCmd ( TempInsertUninitializedItemSql , id , length , buf , timeout ) ;
464
470
using ( var connection = new SqlConnection ( _connectString ) )
465
471
{
466
- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry , true ) ;
472
+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync , true ) ;
467
473
}
468
474
}
469
475
#endregion
470
-
471
- }
476
+
477
+ }
472
478
}
0 commit comments