Delphi的TADOConnection线程安全吗? [英] Is Delphi's TADOConnection thread-safe?

查看:224
本文介绍了Delphi的TADOConnection线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

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



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

 过程TMyThread.Execute ; 
begin
CoInitialize(nil);
尝试
尝试
//在这里创建一个连接
除了
结束;
finally
CoUnInitialize;
结束
结束


I'm writing a Delphi 7 application which needs to access the same SQL Server database from many different threads simultaneously.

Can I use a single shared TADOConnection, or must each thread create their own?

解决方案

Blorgbeard, you must create, initialize and open a separate TAdoconnection instance for each of your threads.

ADO is a COM-based technology. It uses apartment-threaded objects ,don't forget to call CoInitialize(nil).

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

这篇关于Delphi的TADOConnection线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆