Table of Contents

Class SchemaSyncLock

Namespace
CL.MySQL2.Services
Assembly
CL.MySQL2.dll

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

string

Properties

Acquired

True when the advisory lock was actually obtained within the timeout.

public bool Acquired { get; }

Property Value

bool

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

connectionManager ConnectionManager

Connection source.

connectionId string

Connection ID to lock on.

timeoutSeconds int

How long to wait for the lock before giving up. Default 30s.

lockName string

Advisory lock name. Defaults to LockName.

logger ILogger

Optional logger.

ct CancellationToken

Cancellation token.

Returns

Task<SchemaSyncLock>

DisposeAsync()

Releases the advisory lock (if held) and closes the dedicated connection.

public ValueTask DisposeAsync()

Returns

ValueTask