在后台线程中运行Firebird查询并保存结果集 [英] Run Firebird query in a background thread and save result set

查看:192
本文介绍了在后台线程中运行Firebird查询并保存结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在delphi-2009中将带有参数的查询执行到线程安全的类。



我在谷歌浏览,但是我没有找到



谢谢

解决方案

我发现大多数数据库API在连接级只有线程安全。 Firebird可能有所不同,但使用InterBase几个(8+)年前,它不是线程安全的。更新:我已经验证过Firebird只有线程安全连接级别。



这意味着您通常需要避免同时使用多个线程的单个连接。针对给定连接的查询的执行适用。
避免在针对相同连接运行的不同线程中有两个查询。



但是,如果你有两个连接,可以在两个连接上运行两个查询同样的时间。



您的问题的性质似乎是如何以安全的方式将数据传递给线程,虽然您具体需要数据库,但这适用于通用的方式,无论线程中包含什么。



传递数据的最简单的方法是创建,您可以创建挂起的线程,设置您的 TThread 后裔。然后恢复线程的执行。
您在线程中工作的实际代码需要从 Execute()方法中调用。



如果您需要在线程之间共享数据,则必须将其包含在允许同步的各种结构之中,例如Critical Section,Mutex或Semaphore。



Delphi在SyncObjs.pas单元中为每个文件夹提供了库包装。


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

解决方案

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.

这篇关于在后台线程中运行Firebird查询并保存结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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