在后台线程中运行 Firebird 查询并保存结果集
我想将带有参数的查询的执行放入delphi-2009中的线程安全类中。
我在谷歌中导航,但没有找到我想要的东西。
谢谢
i want to put the execution of a query with parameters into a thread-safe class in delphi-2009.
I navigate in google but i don't found just what I wanted.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现大多数数据库 API 仅在连接级别是线程安全的。 Firebird 可能有所不同,但在数 (8+) 年前使用 InterBase,它不是线程安全的。更新: 我已经验证 Firebird 仅在线程安全连接级别。
这意味着通常您需要避免同时从多个线程使用单个连接。针对给定连接执行查询适用。
避免在不同线程中对同一连接运行两个查询。
但是,话虽如此,如果您有两个连接,则可以同时运行两个查询。
您问题的本质似乎是如何以安全的方式将数据传递给线程,尽管您的具体需求是数据库,但这以通用方式适用,无论线程中包含什么。
传递数据的最简单方法是在创建时,您可以创建挂起的线程,在
TThread
后代上设置各种属性。然后恢复线程的执行。在线程中执行工作的实际代码需要从 Execute() 方法调用。
如果需要在线程之间共享数据,则必须包装在允许同步的各种结构之一中,例如关键部分、互斥体或信号量。
Delphi 在 SyncObjs.pas 单元中为每一个都提供了库包装器。
I have found that most database API's are only thread safe at the connection level. Firebird may be different, but using InterBase several (8+) years ago, it was not thread safe. Update: I have verified Firebird is only thread safe at the connection level.
This means that typically you need to avoid using a single connection from more than one thread at the same time. The execution of a query against a given connection applies.
Avoid having two queries in different threads running against the same connection.
However, having said that if you have two connections you can have two queries running at the same time.
The nature of your question seems to be how to pass data in a safe manner to a thread, and although you specific need is database, this applies in a generic manner regardless of what is contained in a thread.
The easiest way to pass data is on creation, you can create the thread suspended, set various properties on your
TThread
descendant. Then resume the execution of the thread.Your actual code that does the work in the thread needs to be called from the
Execute()
method.If you need to share data between threads you must wrap in one of the various structures that allow to synchronization, such as a Critical Section, Mutex, or Semaphore.
Delphi has library wrappers for each of these in the SyncObjs.pas unit.