首页 > 解决方案 > 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();

标签: c#sqlsql-server

解决方案


推荐阅读