在线程中使用 TADOQuery
我正在编写应用程序,它连接到数据库并重复(1 分钟间隔)从数据库读取数据。它类似于 RSS 提要阅读器,但具有本地数据库。如果数据读取失败,我会尝试重新建立连接。我设计它时将 TADOConnection 和 TADOQuery 放置在表单上(因此没有动态创建)。我的目标是从用户的角度保持应用程序“活跃”,因此我将连接和读取部分放入单个线程中。问题是,如何做到最好?
我的设计如下:
- 创建
- 应用程序启动时,TADOConnection和TADOQuery与表单打开连接一起在单独的线程(TADOConnection)中
- ,如果连接建立,则挂起连接线程,启动表单上的计时器,该计时器定期恢复另一个线程用于数据读取
- 如果读取线程成功,则没有任何反应并且表单计时器继续运行,如果失败,则线程停止计时器并恢复连接线程
动态创建 TADOConnection 或 TADOQuery 更好还是无关紧要?是否最好在线程中使用关键部分或其他东西(我同时只能对组件进行一次访问,并且只有一个线程)?
感谢您的建议
I'm writing the application, which connects to the DB and repetitively (1 minute interval) reads the data from a database. It's something like RSS feed reader, but with local DB. If the data reading fails, I try to reestablish the connection. I've designed it with TADOConnection and TADOQuery placed on the form (so with no dynamic creation). My aim is to keep the application "alive" from the user's point of view, so I placed the connection and the reading part into a single thread. The question is, how to do it best way ?
My design looks like this:
- application start, the TADOConnection and TADOQuery are created along with the form
- open connection in a separate thread (TADOConnection)
- if the connection is established, suspend the connection thread, start the timer on the form, which periodically resumes another thread for data reading
- if the reading thread succeeds, nothing happens and form timer keeps going, if it fails, the thread stops the timer and resume connection thread
Is it better to create TADOConnection or TADOQuery dynamically or it doesn't matter ? Is it better to use e.g. critical section in the threads or something (I have only one access to the component at the same time and only one thread) ?
Thanks for your suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题相当主观,可能还没有主观到足以结束,但无论如何都是主观的。这就是我选择动态创建 ADO 对象的原因:
This question is fairly subjective, probably not subjective enough to get closed but subjective any way. Here's why I'd go for dynamically created ADO objects:
运行线程中的所有内容。在线程中有一个周期性计时器,用于打开数据库连接,读取数据,将其“发布”回主线程,然后断开连接。线程在等待时间时需要“睡眠”,例如在 Windows 上,即使是由计时器发出信号的。 DB 组件是线程本地且私有的,可以在线程执行开始时(在应用程序启动时)在线程内部创建,并在线程执行完成时(在应用程序关闭时)释放。无论数据库连接是否暂时可用,这都将始终有效,并且主线程甚至不必与“数据库线程”进行通信。这是我一直使用的架构,而且绝对是防弹的。
Run everything in the thread. Have a periodic timer in the thread that opens the DB connection, reads the data, "posts" it back to the main thread, and then disconnects. The thread needs to "sleep" while waiting for the time, e.g. on a Windows even that is signalled by the timer. The DB components, which are local and private to the thread, can be created inside the thread when thread executions starts (on application startup), and freed when thread execution finishes (on application shutdown). This will always work, regardless of whether the DB conncetion is temporarily available or not, and the main thread does not even have to communicate with the "DB thread". It is an architcture that I use all the time and is absolulutely bullet-proof.