使用线程时的数据库可访问性 [英] Database accessibility while working with thread

查看:79
本文介绍了使用线程时的数据库可访问性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写邮件发送脚本。在主线程中,数据来自数据库并发送邮件,并且随着邮件发送开始,在数据库中进行的更改很少,例如sending_status变为发送等。



I已经使用了计时器线程,每隔5分钟调用一个函数并更新数据库。



我怀疑当主线程运行时它会打开连接,如果是计时器线程开始执行它会产生错误,因为主线程已经打开了连接,计时器线程将尝试打开与数据库的连接。



什么可以解决这个?

I am writing mail sending script. In main thread the data is brought from database and mail is sent and as the mail sending starts few changes are made in database like sending_status become 'Sending' etc.

I have used timer thread where every 5 mins a function is called and database is updated.

My doubt is that when the main thread is running it will open connection and if the timer thread starts execution it will produce an error because the connection is already opened by main thread and timer thread will try to open connection with database.

What can be solution to this?

推荐答案

我建​​议你使用 SQLDependency类 [ ^ ]而是每隔5分钟更新一次数据库。





这是一个例子:使用SqlDependency进行数据更改事件 [ ^ ]
I'd suggest you to use SQLDependency class[^] instead updating database every 5 minutes in thread.


Here is an example: Using SqlDependency for data change events[^]


我建​​议使用轮询架构,它消除了一大堆类似的问题。请参阅:使用计时器轮询数据库 [ ^ ]。



-SA
I suggested the polling schema which eliminates a whole bunch of similar problems. Please see: Polling Database with Timer[^].

—SA


这篇关于使用线程时的数据库可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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