|
12 | 12 | using Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry; |
13 | 13 | using static Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry.Telemetry; |
14 | 14 | using static Microsoft.Azure.WebJobs.Extensions.Sql.SqlTriggerConstants; |
| 15 | +using static Microsoft.Azure.WebJobs.Extensions.Sql.SqlTriggerUtils; |
15 | 16 | using Microsoft.Azure.WebJobs.Host.Executors; |
16 | 17 | using Microsoft.Data.SqlClient; |
17 | 18 | using Microsoft.Extensions.Logging; |
@@ -285,6 +286,8 @@ private async Task GetTableChangesAsync(SqlConnection connection, CancellationTo |
285 | 286 | { |
286 | 287 | try |
287 | 288 | { |
| 289 | + await AcquireAppLockAsync(connection, transaction, this._logger, token); |
| 290 | + |
288 | 291 | // Update the version number stored in the global state table if necessary before using it. |
289 | 292 | using (SqlCommand updateTablesPreInvocationCommand = this.BuildUpdateTablesPreInvocation(connection, transaction)) |
290 | 293 | { |
@@ -525,6 +528,8 @@ private async Task RenewLeasesAsync(SqlConnection connection, CancellationToken |
525 | 528 | { |
526 | 529 | try |
527 | 530 | { |
| 531 | + await AcquireAppLockAsync(connection, transaction, this._logger, token); |
| 532 | + |
528 | 533 | SqlCommand renewLeasesCommand = this.BuildRenewLeasesCommand(connection, transaction); |
529 | 534 | if (renewLeasesCommand != null) |
530 | 535 | { |
@@ -634,6 +639,8 @@ private async Task ReleaseLeasesAsync(SqlConnection connection, CancellationToke |
634 | 639 | { |
635 | 640 | try |
636 | 641 | { |
| 642 | + await AcquireAppLockAsync(connection, transaction, this._logger, token); |
| 643 | + |
637 | 644 | // Release the leases held on "_rowsToRelease". |
638 | 645 | using (SqlCommand releaseLeasesCommand = this.BuildReleaseLeasesCommand(connection, transaction)) |
639 | 646 | { |
@@ -789,8 +796,6 @@ private static SqlChangeOperation GetChangeOperation(IReadOnlyDictionary<string, |
789 | 796 | private SqlCommand BuildUpdateTablesPreInvocation(SqlConnection connection, SqlTransaction transaction) |
790 | 797 | { |
791 | 798 | string updateTablesPreInvocationQuery = $@" |
792 | | - {AppLockStatements} |
793 | | -
|
794 | 799 | DECLARE @min_valid_version bigint; |
795 | 800 | SET @min_valid_version = CHANGE_TRACKING_MIN_VALID_VERSION({this._userTableId}); |
796 | 801 |
|
@@ -834,8 +839,6 @@ private SqlCommand BuildGetChangesCommand(SqlConnection connection, SqlTransacti |
834 | 839 | // up regardless since we know it should be processed - no need to check the change version. |
835 | 840 | // Once a row is successfully processed the LeaseExpirationTime column is set to NULL. |
836 | 841 | string getChangesQuery = $@" |
837 | | - {AppLockStatements} |
838 | | -
|
839 | 842 | DECLARE @last_sync_version bigint; |
840 | 843 | SELECT @last_sync_version = LastSyncVersion |
841 | 844 | FROM {GlobalStateTableName} |
@@ -882,8 +885,6 @@ private async Task<string> GetLeaseLockedOrMaxAttemptRowCountMessage(SqlConnecti |
882 | 885 | // * NULL LeaseExpirationTime OR LeaseExpirationTime <= Current Time |
883 | 886 | // * No attempts remaining (Attempt count = Max attempts) |
884 | 887 | string getLeaseLockedOrMaxAttemptRowCountQuery = $@" |
885 | | - {AppLockStatements} |
886 | | -
|
887 | 888 | DECLARE @last_sync_version bigint; |
888 | 889 | SELECT @last_sync_version = LastSyncVersion |
889 | 890 | FROM {GlobalStateTableName} |
@@ -948,8 +949,6 @@ private SqlCommand BuildAcquireLeasesCommand(SqlConnection connection, SqlTransa |
948 | 949 | const string rowDataParameter = "@rowData"; |
949 | 950 | // Create the merge query that will either update the rows that already exist or insert a new one if it doesn't exist |
950 | 951 | string query = $@" |
951 | | - {AppLockStatements} |
952 | | -
|
953 | 952 | WITH {acquireLeasesCte} AS ( SELECT * FROM OPENJSON(@rowData) WITH ({string.Join(",", cteColumnDefinitions)}) ) |
954 | 953 | MERGE INTO {this._bracketedLeasesTableName} |
955 | 954 | AS ExistingData |
@@ -989,8 +988,6 @@ private SqlCommand BuildRenewLeasesCommand(SqlConnection connection, SqlTransact |
989 | 988 | return null; |
990 | 989 | } |
991 | 990 | string renewLeasesQuery = $@" |
992 | | - {AppLockStatements} |
993 | | -
|
994 | 991 | UPDATE {this._bracketedLeasesTableName} |
995 | 992 | SET {LeasesTableLeaseExpirationTimeColumnName} = DATEADD(second, {LeaseIntervalInSeconds}, SYSDATETIME()) |
996 | 993 | WHERE {matchCondition}; |
@@ -1021,9 +1018,7 @@ private SqlCommand BuildReleaseLeasesCommand(SqlConnection connection, SqlTransa |
1021 | 1018 | const string rowDataParameter = "@rowData"; |
1022 | 1019 |
|
1023 | 1020 | string releaseLeasesQuery = |
1024 | | -$@"{AppLockStatements} |
1025 | | -
|
1026 | | -WITH {releaseLeasesCte} AS ( SELECT * FROM OPENJSON(@rowData) WITH ({string.Join(",", cteColumnDefinitions)}) ) |
| 1021 | +$@"WITH {releaseLeasesCte} AS ( SELECT * FROM OPENJSON(@rowData) WITH ({string.Join(",", cteColumnDefinitions)}) ) |
1027 | 1022 | UPDATE {this._bracketedLeasesTableName} |
1028 | 1023 | SET |
1029 | 1024 | {LeasesTableChangeVersionColumnName} = cte.{SysChangeVersionColumnName}, |
@@ -1053,8 +1048,6 @@ private SqlCommand BuildUpdateTablesPostInvocation(SqlConnection connection, Sql |
1053 | 1048 | string leasesTableJoinCondition = string.Join(" AND ", this._primaryKeyColumns.Select(col => $"c.{col.name.AsBracketQuotedString()} = l.{col.name.AsBracketQuotedString()}")); |
1054 | 1049 |
|
1055 | 1050 | string updateTablesPostInvocationQuery = $@" |
1056 | | - {AppLockStatements} |
1057 | | -
|
1058 | 1051 | DECLARE @current_last_sync_version bigint; |
1059 | 1052 | SELECT @current_last_sync_version = LastSyncVersion |
1060 | 1053 | FROM {GlobalStateTableName} |
|
0 commit comments