【问题标题】:Is Delphi's TADOConnection thread-safe?Delphi 的 TADOConnection 是线程安全的吗?
【发布时间】:2009-08-07 06:37:59
【问题描述】:

我正在编写一个 Delphi 7 应用程序,它需要同时从许多不同的线程访问同一个 SQL Server 数据库。

我可以使用单个共享的 TADOConnection,还是每个线程都必须创建自己的?

【问题讨论】:

    标签: delphi thread-safety ado delphi-7 adoconnection


    【解决方案1】:

    Blorgbeard,您必须创建、初始化并打开一个单独的 每个线程的 TAdoconnection 实例。

    ADO 是一种基于 COM 的技术。它使用单元线程对象,不要忘记调用 CoInitialize(nil)。

    procedure TMyThread.Execute;
    begin
       CoInitialize(nil);
       try
         try
           // create a connection here
         except
         end;
       finally
         CoUnInitialize;
       end;
    end;
    

    【讨论】:

      【解决方案2】:

      不,不是。 ADO 是一种基于 COM 的技术。它使用单元线程对象,因此您不能跨线程边界使用 ADO 连接。每个线程都需要自己的连接。

      【讨论】:

        猜你喜欢
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 2010-10-14
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        • 2010-09-09
        相关资源
        最近更新 更多