使用多线程时如何使用 Delphi 设计时 FireDac TFDQuery?
How to use Delphi design-time FireDac TFDQuery when using multiple threads?
我想使用组件编辑器设计 TFDQuery
,即在设计时设置 SQL 字符串、选项等,然后在线程中使用查询。
我的问题是线程的每个 运行 实例都需要它自己的查询实例 - 否则它将不是线程安全的。
我是否应该在线程启动时克隆查询 运行,即在线程的 Execute 方法中,然后设置它的连接 - 如果是,怎么办?或者有更好的方法吗?
谢谢
如documentation所述:
...after a thread opens a query and until its processing is finished,
the application cannot use this query and the connection objects in
another thread. Similarly, after a thread starts a transaction and
until the transaction is finished, the application cannot use this
transaction and the connection objects in another thread.
还有:
The standard simplification is to create and use for each thread a
dedicated connection object working with the database.
所以最简单的解决方案是为每个工作线程建立一个单独的连接。如果您在同一个数据模块上使用 FireDAC(连接、事务和查询)组件,那么最简单的解决方案可能是为每个工作线程简单地创建整个数据模块的单独实例。
我想使用组件编辑器设计 TFDQuery
,即在设计时设置 SQL 字符串、选项等,然后在线程中使用查询。
我的问题是线程的每个 运行 实例都需要它自己的查询实例 - 否则它将不是线程安全的。
我是否应该在线程启动时克隆查询 运行,即在线程的 Execute 方法中,然后设置它的连接 - 如果是,怎么办?或者有更好的方法吗?
谢谢
如documentation所述:
...after a thread opens a query and until its processing is finished, the application cannot use this query and the connection objects in another thread. Similarly, after a thread starts a transaction and until the transaction is finished, the application cannot use this transaction and the connection objects in another thread.
还有:
The standard simplification is to create and use for each thread a dedicated connection object working with the database.
所以最简单的解决方案是为每个工作线程建立一个单独的连接。如果您在同一个数据模块上使用 FireDAC(连接、事务和查询)组件,那么最简单的解决方案可能是为每个工作线程简单地创建整个数据模块的单独实例。