繁体   English   中英

在一段时间后重试功能的最佳方法,然后重试

[英]Best approach to timeout a function after a certain period of time and retry

只是一点点背景。 我们正在从将散列在数据库中的用户密码存储到直接针对Active Directory进行身份验证。 不幸的是,我们有太多内部应用程序,以至于此时无法更新每个应用程序。

因此,我认为我可以只为SQL构建一个.NET CLR程序集,并修改现有的身份验证存储过程,以针对我们的域控制器(而不是表)验证用户密码,而不是要求所有开发人员更改其代码。

这在我们的初始测试中是成功的,除了由于某种原因(关闭,重新启动,网络等)无法与域控制器通信时。 发生这种情况时,我们会超时。 我添加了一个try / catch和while循环,以仅跳到下一个域控制器并尝试对其进行身份验证,但是在尝试对下一个服务器进行身份验证之前,它仍然需要20秒钟以上的首次尝试失败。 因为这是SQL程序集/函数,并且在存储过程中被调用,所以20秒太长了。

什么是修改我的代码并使对服务器的身份验证尝试失败并在3秒钟后尝试下一个服务器的最佳方法? 以某种方式穿线?

Imports System
Imports System.Data
Imports System.Collections
Imports System.Text
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlTypes
Imports System.Runtime.InteropServices
Imports System.DirectoryServices.AccountManagement

Partial Public Class UserDefinedFunctions

    <SqlFunction(Name:="ValidCredentials")>
    Public Shared Function ValidCredentials(ByVal Domain As SqlString, ByVal Username As SqlString, ByVal Password As SqlString) As SqlBoolean
        'Validates the username and password are correct against the domain.

        Dim ServerNum As Integer = 1
        Dim Success As Boolean = False
        Dim ValidCreds As SqlBoolean = False

        'Loop through all 4 domain controllers to attempt authentication against each of them.
        While Success = False And ServerNum <= 4

            Try

                Dim pc As PrincipalContext
                If Domain = "employees" Then
                    pc = New PrincipalContext(ContextType.Domain, "DC-E-0" & ServerNum.ToString & ".employees.ourdomain.loc", "EMPLOYEES\BindAcct", "PASS")
                Else
                    pc = New PrincipalContext(ContextType.Domain, "DC-S-0" & ServerNum.ToString & ".students.ourdomain.loc", "STUDENTS\BindAcct", "PASS")
                End If

                ValidCreds = pc.ValidateCredentials(Username, Password)
                Success = True
                Return ValidCreds

            Catch ex As Exception

                ValidCreds = False
                Success = False
                ServerNum += 1

            End Try

        End While

        Return ValidCreds

    End Function

End Class

您可以使用以下代码行在sql server 2008或更高版本上为函数设置超时值。 只需将其放在块的顶部即可:

SET LOCK_TIMEOUT 3000;  
GO

超时值以milliseconds

根据MS文档

当等待锁的时间超过超时值时,将返回错误。

这将导致您的功能花费少于3秒或失败。 然后,您应该捕获异常并在需要时回滚。

您可以使用单独的线程执行此操作:

设置操作超时

代码在C#中,但是转换起来应该很简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM