c# - Two or more server processes with one SQL Server DateTime flag. How to prevent overlap?
问题描述
I have n number of servers running the same code.
They all use the same SQL server db.
There's a process that needs to be run once an hour by any 1 server.
Is there a way to have a datetime flag in SQL server that could make sure the process is only ever run once an hour by anyone server?
My concern is :
if (DateTime.UtcNow > Settings.LastLogDataRetentionCheck.AddDays(1))
{
await dm.UpdateLastLogDataRetentionCheck(DateTime.UtcNow);
await dm.CullDeviceLogs();
}
What happens if two servers run this code at exactly the same time?
They will both pass the condition and update the DateTime, then proceed to run the expensive process CullDeviceLogs();
解决方案
推荐阅读
- javascript - Tooltip text not showing after replace()
- regex - 正则表达式帮助。捕获以“score”开头并以数字结尾的字符串
- c++ - C++ Operator overloading, returns, types and robots
- ios - Apply Beveled Radial Gradient to UIView
- ios - 用 PDFKit 实现 PencilKit
- java - 如何删除默认的搜索栏笔划?
- c++ - C++20:非类型模板参数中的非捕获 lambda
- ios - 如何在 reloadData() 之后为 UITableViewCell 中的 UILabel 设置动画?
- java - 如何分割彩色线条?
- javascript - onMouseEnter 的功能不一致,偶尔不读取元素 ID