我正在尝试制作一个 Delphi 应用程序来与 AlwaysOn 解决方案一起使用。我在谷歌上发现我必须使用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 弹性事件将导致您的数据库连接失败,您需要启动一个新的连接。
如果 SqlClient 应用程序连接到 AlwaysOn 数据库
故障转移,原始连接断开,应用程序必须
故障转移后打开新连接以继续工作。
我通过使用我自己的版本覆盖 TAdoQuery 组件的简单权宜之计来处理这个问题,该版本在连接失败后重试连接。这可能不是正确的方法,但它确实有效。它的作用是覆盖为打开而调用的方法(如果查询返回结果集)或执行 SQL(否则),如果由于连接丢失错误而失败,则再次尝试(但仅一次)。我已经针对 AlwaysOn 切换进行了大量测试,它对于我们的配置可靠地工作。它还会对任何其他连接丢失事件做出反应,从而处理查询失败的一些其他原因。如果您使用 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)