Run Firebird query in a background thread and save result set

拈花ヽ惹草 提交于 2019-12-06 06:49:51

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5594174/run-firebird-query-in-a-background-thread-and-save-result-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!