可以在线程中使用 TADOConnection [英] Ok to use TADOConnection in threads

查看:27
本文介绍了可以在线程中使用 TADOConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 TCPip 服务器应用程序.该应用程序有一个全局 TADOConnection.这个全局 ado 连接既用于主线程查询,也用于线程进程.

I have created an TCPip server application. The application has one global TADOConnection. This global ado connection is used both for main thread queries and also within threaded processes.

这样好吗?ADOConnection 是否具有同时处理多个查询的内置机制?

Is this ok? Does the ADOConnection have built in mechanisms to handle multiple queries at the same time?

我的应用程序可以在测试环境中找到(2-5 个连接).但是部署在生产环境中,当链接到 ADOConnection 的 TADOQuery 设置为打开时,我遇到了无法解释的"访问冲突.

My application works find in testing environments (2-5 connections). But deployed in a production environment I am getting "unexplainable" access violations at the point the TADOQuery linked to the ADOConnection are set to opened.

我应该使用 ADOConnection 还是所有查询都应该自己建立与数据库的连接(这可能会占用更多资源)?

Should I be using ADOConnection or should all queries just make the connection to the database on their own (which is probably a bit more resource costly)?

推荐答案

每个线程都需要有自己的连接对象.以下链接提供了更多信息:http://delphi.about.com/od/kbthread/a/query_threading.htm

Each thread needs to have its own connection object. The following link provides more information: http://delphi.about.com/od/kbthread/a/query_threading.htm

文章中的一些要点:

1] 在使用任何 dbGo 对象之前,必须手动调用 CoInitialize 和 CoUninitialize.未能调用 CoInitialize 将导致未调用 CoInitialize"异常.CoInitialize 方法在当前线程上初始化 COM 库.ADO 是 COM.

1] CoInitialize and CoUninitialize must be called manually before using any of the dbGo objects. Failing to call CoInitialize will result in the "CoInitialize was not called" exception. The CoInitialize method initializes the COM library on the current thread. ADO is COM.

2] 您不能使用主线程(应用程序)中的 TADOConnection 对象.每个线程都需要创建自己的数据库连接.

2] You cannot use the TADOConnection object from the main thread (application). Every thread needs to create its own database connection.

这篇关于可以在线程中使用 TADOConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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