Class SchemaSyncLock
A cross-node advisory lock around a schema-sync / migration pass, implemented with MySQL's
connection-scoped GET_LOCK. Because the lock is released automatically when the holding
connection closes, this type keeps a single dedicated connection open for its whole lifetime and
releases the lock (and the connection) on DisposeAsync().
public sealed class SchemaSyncLock : IAsyncDisposable
- Inheritance
-
SchemaSyncLock
- Implements
- Inherited Members
Remarks
Multiple application nodes booting at once all contend for the same named lock; the winner runs the DDL pass while the others wait, then find the schema already reconciled (matching CRCs) and do nothing. Acquire via AcquireAsync(ConnectionManager, string, int, string?, ILogger?, CancellationToken); always check Acquired.
Fields
LockName
The well-known lock name. Shared by declarative sync and the migration runner.
public const string LockName = "clmysql2_schema_sync"
Field Value
Properties
Acquired
True when the advisory lock was actually obtained within the timeout.
public bool Acquired { get; }
Property Value
Methods
AcquireAsync(ConnectionManager, string, int, string?, ILogger?, CancellationToken)
Opens a dedicated connection and attempts GET_LOCK(name, timeout). The returned
instance must be disposed to release the lock — even when Acquired is false
(the connection still needs closing).
public static Task<SchemaSyncLock> AcquireAsync(ConnectionManager connectionManager, string connectionId = "Default", int timeoutSeconds = 30, string? lockName = null, ILogger? logger = null, CancellationToken ct = default)
Parameters
connectionManagerConnectionManagerConnection source.
connectionIdstringConnection ID to lock on.
timeoutSecondsintHow long to wait for the lock before giving up. Default 30s.
lockNamestringAdvisory lock name. Defaults to LockName.
loggerILoggerOptional logger.
ctCancellationTokenCancellation token.
Returns
DisposeAsync()
Releases the advisory lock (if held) and closes the dedicated connection.
public ValueTask DisposeAsync()