< Back

Use-DbRetry

Mon Jan 13, 2020 6:28 pm

NAME Use-DbRetry



SYNOPSIS

Retry SQL operations.





SYNTAX

Use-DbRetry [-Script] <ScriptBlock> [[-RetryCount] <Object>] [[-RetrySeconds] <Object>] [<CommonParameters>]





DESCRIPTION

Retry SQL operations. These are only in limited scenarios:

* SQL Server deadlocks and timeouts.

* SQL Server policy evaluation errors (caused by race conditions).





PARAMETERS

-Script <ScriptBlock>



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-RetryCount <Object>



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-RetrySeconds <Object>



Required? false

Position? 3

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

A scriptblock.





OUTPUTS

Anything output by the scriptblock. But failure information is also written to the Verbose stream.





NOTES





https://docs.microsoft.com/en-us/azure/ ... ity-issues



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>$serverInstance = ".\\SQL2016"



New-DbConnection $serverInstance master | New-DbCommand "If Object_Id('dbo.Moo', 'U') Is Not Null Drop Table

dbo.Moo; Create Table dbo.Moo (A Int Identity (1, 1) Primary Key, B Nvarchar(Max));" | Get-DbData

$dbData = New-DbConnection $serverInstance master | New-DbCommand "Select * From dbo.Moo" | Enter-DbTransaction

-PassThru | Get-DbData -As DataTables

$dbData.Alter(@{ A = 1; B = "B" })

try {

$dbData2 = New-DbConnection $serverInstance master | New-DbCommand "Select * From dbo.Moo" -CommandTimeout 2 |

ForEach-Object {

Use-DbRetry { Get-DbData $_ } -Verbose

}

} catch {

"Exception was caught: $_"

}

Exit-DbTransaction $dbData -Rollback



This drops and recreates a dbo.Moo table (no output), begins a transaction and then upserts a record (returning 1

for 1 record modified).



It then starts a second connection with a short timeout and attempts to select data from the table again. With

verbose output this shows a series of timeouts and retry attempts, before throwing an exception which we catch

(outputs exception text).



The transaction is then rolled back (no output).











RELATED LINKS