SQL Server“检查数据库表是否有进程在表上运行”
我想做的是,通知用户我的程序“数据库正在进行更新,因此结果可能不正确”。因此,我必须检查我的程序使用的表上是否有某些进程(例如写入或删除)。
编辑:我更新表的方式是使用 MS Access,我从 MS Excell 复制大约 10.000 行,然后使用 MS Access 将其粘贴到数据库中。因此,当复制粘贴正在进行时,我希望能够从我的基于网络的程序中看到它。
谢谢。
What I want to do is, inform the users about my program that "there is an update going on in database, so the results may not be correct." Thus I have to check if there is some process (like writing or deleting) on my table which my program uses.
Edit: The way that I update my table is I use MS Access, I copy from MS Excell about 10.000 rows and paste it to the database using MS Access. So when the copy paste is in progress, I want to be able see it from my web based program.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解您想要在这里实现的目标。
只要您使用合适的隔离级别,SQL Server 就会为您管理数据的完整性和一致性。
因此,如果您的计数/报告查询尝试访问当前正在更新的记录,则在更新操作完成之前它将无法获取共享锁(当前正在更新的记录上),从而确保仅提交数据被报道。因此,该报告截至其完成时是正确的。
如需进一步阅读,请参阅:事务隔离级别
I'm not certain I understand what you are trying to achieve here.
SQL Server manages the integrity and consistency of your data for you, provided you are using a suitable Isolation Level.
So, if your count/reporting query attempts to access a record that is currently being updated, it will not be able to acquire a shared lock (on a record currently being updated) until after the update operation has completed, thereby ensuring only committed data is reported. The report is therefore correct as of the time it completes.
For further reading see: Transaction Isolation Levels
更新,回复:
原理是相同的,在粘贴过程开始时,添加一个数据库行,就像我的第一个答案中一样。或者,您可以在 Web 应用程序中设置一个全局变量,例如
updateInProgress = true
。但是,这个副本需要多长时间?如果少于 5 分钟,那么可能不值得做任何其他事情。
如果您只是想让用户知道有“新鲜”数据,您可以查询最近的日期时间。
什么样的更新?在通常意义上,SQL Server 通过默认事务锁定来保持结果的准确性。也许您的意思是长时间的数据导入工作?或者,天堂不允许,一些密集的、光标驱动的过程?
然后您可以执行以下操作:
在数据库中创建一个表。称之为 LongJobLog。它可能有 3 列:ID、开始日期时间和结束日期时间。
在每个长作业开始时,向该表添加一个新的开始时间,请务必将新 ID 保存到变量中。
在每个长作业开始时,向该表
当作业结束或出错时,用结束时间更新 ID 的行。
当
您的应用程序将查询此表以查找结束时间仍为空的最近行(按开始时间)。如果存在此类行,则正在进行“更新”。
您可能需要一个 cron 作业来清除作业崩溃的条目。
或者,此启动停止信息可能在 SQL 日志中可用。不过,解决这个问题可能需要一些工作。
Update, re:
The principle is the same, at the start of your paste process, add a DB row, like in my first answer. Or, you could set a global variable in your web application, something like
updateInProgress = true
.But, how long does this copy take? If it's less than about 5 minutes, then it's probably not worth doing anything else.
If you just want to let the user know that there is "fresh" data, you could query for recent datetimes.
What kind of update?In the usual sense, SQL Server keeps the results accurate with default transaction locking.Maybe you mean a long data-import job? Or, Heaven forbid, some intensive, cursor-driven process?
Then you could do something like:
Create a table in the database. Call it, say LongJobLog. It might have 3 columns: an ID, a start datetime, and an end datetime.
At the beginning of each long job, add a new start time to this table, be sure to save the new ID to a variable.
When the job ends, or errors out, update the ID's row with the endtime.
Your application would query this table for recent rows (by the start time) that still had a null end time. If such rows existed, then an "update" is in progress.
You'll probably need a cron job to clear out entries where jobs crashed.
Alternatively, this start-stop information is probably available in the SQL logs. It might take a bit of work to sort it out, though.
在某个地方创建一个共享计数器(在进程中,或者如果在服务器上,在文件或共享内存中等)。你的更新是这样的
然后如果计数器大于0,你就会显示消息。
(但这是你真正想要的吗?请参阅评论)
Make a shared counter somewhere (in the process, or if on a server, in a file or shared memory, etc.). Do your updates like this
Then you show the message if the counter is larger than 0.
(But is this what you really want? See comment)