delphi - Microsoft AlwaysOn 故障转移解决方案和 Delphi

标签 delphi delphi-xe3 alwayson adoconnection

我正在尝试制作一个 Delphi 应用程序来与 AlwaysOn 解决方案一起使用。我在 Google 上发现必须在连接字符串中使用 MultiSubnetFailover=True

应用程序在 Delphi XE3 中编译并使用 TADOConnection

如果我在连接字符串中使用 Provider=SQLOLEDB,应用程序会启动,但看起来 MultiSubnetFailover=True 没有效果。

如果我使用 Provider=SQLNCLI11 (我在 Google 上发现 OLEDB 不支持 AlwaysOn 解决方案,我必须使用 SQL Native 客户端),我在尝试打开连接时会得到无效属性。

连接字符串是:

Provider=SQLOLEDB.1;Password="password here";Persist Security Info=True;User ID=sa;Initial Catalog="DB here";Data Source="SQL Instance here";MultiSubnetFailover=True

我是否必须在 Delphi 上升级到较新版本才能使用此故障转移解决方案,或者连接字符串中缺少某些内容?

最佳答案

我目前正在将 XE2 与 SQL Server AlwaysOn 一起使用。如果您阅读文档,您将看到 AlwaysOn 弹性事件将导致您的数据库连接失败,您需要启动一个新的连接。

If a SqlClient application is connected to an AlwaysOn database that fails over, the original connection is broken and the application must open a new connection to continue work after the failover.

我通过简单的权宜之计处理了这个问题,即使用我自己的版本覆盖 TAdoQuery 组件,该版本在连接失败后重试连接。这可能不是正确的方法,但它确实有效。它的作用是覆盖为打开而调用的方法(如果查询返回结果集)或执行 SQL(否则),如果由于连接丢失错误而失败,则再次尝试(但仅一次)。我已经针对 AlwaysOn 切换进行了大量测试,它对于我们的配置可靠地工作。它还会对任何其他连接丢失事件使用react,从而处理查询失败的其他一些原因。如果您使用 TAdoQuery 以外的组件,则需要为该组件创建类似的覆盖。

这可能可以通过其他方式来解决,但一旦我找到了可行的方法,我就不再寻找替代方案。您可能需要整理 use 语句,因为它显然包含一些不需要的内容。 (光是看这段代码就让我想离开并重构代码重复)

unit sptADOQuery;

interface

uses
  Windows, Messages, SysUtils, Classes, Db, ADODB;

type
  TsptADOQuery = class(TADOQuery)
  protected
    procedure SetActive(Value: Boolean); override;
  public
    function ExecSQL: Integer;   // static override
  published
  end;

procedure Register;

implementation

uses ComObj;

procedure Register;
begin
  RegisterComponents('dbGo', [TsptADOQuery]);
end;

procedure TsptADOQuery.SetActive(Value: Boolean);
begin
  try
    inherited SetActive(Value);
  except
    on e: EOleException do
    begin
      if (EOleException(e).ErrorCode = HRESULT($80004005)) then
      begin
        if Assigned(Connection) then
        begin
          Connection.Close;
          Connection.Open;
        end;
        inherited SetActive(Value);   // try again
      end
      else raise;
    end
    else raise;
  end;
end;

function TsptADOQuery.ExecSQL: Integer;
begin
  try
    Result := inherited ExecSQL;
  except
    on e: EOleException do
    begin
      if (EOleException(e).ErrorCode = HRESULT($80004005)) then
      begin
        if Assigned(Connection) then
        begin
          Connection.Close;
          Connection.Open;
        end;
        Result := inherited ExecSQL;   // try again
      end
      else raise;
    end
    else raise;
  end;
end;

end.

关于delphi - Microsoft AlwaysOn 故障转移解决方案和 Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479459/

相关文章:

delphi - 多部分/表单数据请求的 Indy MIME 解码返回尾随 CR/LF

delphi - 在 Delphi 中高效填充组合框

delphi - 生成浮点范围内的随机数

Delphi XE3 文件未找到 GIFImage.dcu

string - StringOf 是否复制传递给它的数据?

delphi - Delphi 应用程序的 Firebird 错误 "username and password are not defined"

c++ - 从 Delphi 7 中的 C++ DLL 接收字符串数组

sql-server - 使用 AlwaysOn 集群进行加密

面向 Azure 互联网的多区域负载均衡器