Introduce a new IMainDomLock and both default and sql implementations

This commit is contained in:
Shannon
2019-12-10 13:33:59 +01:00
parent 3e48022949
commit 1513a12549
16 changed files with 375 additions and 69 deletions

View File

@@ -250,6 +250,11 @@ where tbl.[name]=@0 and col.[name]=@1;", tableName, columnName)
}
public override void WriteLock(IDatabase db, params int[] lockIds)
{
WriteLock(db, 1800, lockIds);
}
public void WriteLock(IDatabase db, int millisecondsTimeout, params int[] lockIds)
{
// soon as we get Database, a transaction is started
@@ -260,7 +265,7 @@ where tbl.[name]=@0 and col.[name]=@1;", tableName, columnName)
// *not* using a unique 'WHERE IN' query here because the *order* of lockIds is important to avoid deadlocks
foreach (var lockId in lockIds)
{
db.Execute(@"SET LOCK_TIMEOUT 1800;");
db.Execute($"SET LOCK_TIMEOUT {millisecondsTimeout};");
var i = db.Execute(@"UPDATE umbracoLock WITH (REPEATABLEREAD) SET value = (CASE WHEN (value=1) THEN -1 ELSE 1 END) WHERE id=@id", new { id = lockId });
if (i == 0) // ensure we are actually locking!
throw new ArgumentException($"LockObject with id={lockId} does not exist.");