gpt4 book ai didi

multithreading - 在Apache托管的Delphi创建的ISAPI dll中获取当前上下文ID

转载 作者:行者123 更新时间:2023-12-03 18:47:12 25 4
gpt4 key购买 nike

我有一个线程安全类,用于为任何给定线程获取正确的TADOConnection。这在我的可执行文件中效果很好。归结为以下几点:

function ConnectionForCurrentThread () : TADOConnection;
var
thread : TThread;
begin
thread := TThread.CurrentThread;
result := adoConnectionFactory.ConnectionForID(thread.Handle);
end;

adoConnectionFactory最终处理一个关键部分,以返回存储在列表中的现有ADO连接,或者如果尚未为该线程句柄创建一个新的ADO连接,则进行一个新的连接。稍后,我测试它们以释放它们
if Windows.GetExitCodeThread(threadID, res) then begin
if res <> Windows.STILL_ACTIVE then begin
TADOConnection(connections.Objects[i]).Free;
connections.Delete(i);
end;
end else begin
TADOConnection(connections.Objects[i]).Free;
connections.Delete(i);
end;

这一切似乎都运作良好。当在由Apache托管的SOAP Web服务ISAPI dll中使用时,它会崩溃。两次同时调用Web服务时,Thread.Handle最终是相同的,因此它们尝试共享同一ADO连接,由于不同的线程摆弄了相同的连接,因此它们有时会引发异常。

我的问题是,我可以用什么代替CurrentThread.handle?我真的希望此函数在此函数调用中是自包含的,因此,如果我认识到它处于Web服务dll模式下,则改为执行以下操作:
function ConnectionForCurrentThread () : TADOConnection;
var
thread : TThread;
ct : TContextThing;
id : integer;
begin
if WebServiceMode then begin
ct := TContextThing.CurrentContext;
id := ct.ContextID;
end else begin
thread := TThread.CurrentThread;
id := thread.Handle;
end;
result := adoConnectionFactory.ConnectionForID(id);
end;

但是我不太确定ContextThing可能是什么。有什么建议?另一方面,如何查看给定的上下文ID不再有效,以便可以关闭其ADOConnection。

这使用的是Delphi XE和SOAP服务器应用程序(因此,WebBroker,TWebModule以及所有这些爵士乐)。
谢谢你。

最佳答案

对于ISAPI应用程序,以下应该可以工作(假设Apache正确地使用唯一的连接ID填充了ISAPI ECB)。

uses
Web.WebCntxt,
Web.Win.IsapiHttp;

if WebServiceMode then
id := TIsapiRequest(WebContext.Request).ECB^.ConnID
else
...

关于multithreading - 在Apache托管的Delphi创建的ISAPI dll中获取当前上下文ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26363747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com