sqlalchemy_dlock.lock.mssql module

class sqlalchemy_dlock.lock.mssql.MssqlAsyncSadLock(connection_or_session, key, **kwargs)[source]

Bases: MssqlSadLockMixin, BaseAsyncSadLock[str, AsyncConnection | AsyncSession | async_scoped_session]

Async IO version of MssqlSadLock

Parameters:
  • key (Any) – SQL Server requires the resource name as a string (max 255 chars)

  • convert – Custom function to convert key to string

  • sharedshared - Whether to use Shared lock mode

  • updateupdate - Whether to use Update lock mode Note: Shared and Update are mutually exclusive. If both are True, Update takes precedence.

  • connection_or_session (AsyncConnection | AsyncSession | async_scoped_session)

async do_acquire(block=True, timeout=None, *args, **kwargs)[source]
Return type:

bool

Parameters:
async do_release()[source]
class sqlalchemy_dlock.lock.mssql.MssqlSadLock(connection_or_session, key, **kwargs)[source]

Bases: MssqlSadLockMixin, BaseSadLock[str, Connection | Session | scoped_session]

Distributed lock implemented by SQL Server application lock (sp_getapplock)

Parameters:
  • key (Any) – SQL Server requires the resource name as a string (max 255 chars)

  • convert – Custom function to convert key to string

  • sharedshared - Whether to use Shared lock mode

  • updateupdate - Whether to use Update lock mode Note: Shared and Update are mutually exclusive. If both are True, Update takes precedence.

  • connection_or_session (Connection | Session | scoped_session)

do_acquire(block=True, timeout=None, *args, **kwargs)[source]

Acquire the lock using sp_getapplock.

sp_getapplock returns: - >= 0: Success (lock owner) - -1: Timeout - -2: Canceled - -3: Parameter error - -999: Generic error

Return type:

bool

Parameters:
do_release()[source]

Release the lock using sp_releaseapplock.

Returns: - >= 0: Success - -999: Generic error

class sqlalchemy_dlock.lock.mssql.MssqlSadLockMixin(*, key, convert=None, shared=False, update=False, **kwargs)[source]

Bases: AbstractLockMixin[KTV, str]

Mixin class for SQL Server application lock

Parameters:
  • key (TypeVar(KTV, bound= Any)) – SQL Server requires the resource name as a string (max 255 chars)

  • convert (Callable[[TypeVar(KTV, bound= Any)], str] | None) – Custom function to convert key to string

  • shared (bool) – shared - Whether to use Shared lock mode

  • update (bool) – update - Whether to use Update lock mode Note: Shared and Update are mutually exclusive. If both are True, Update takes precedence.

MSSQL_LOCK_RESOURCE_MAX_LENGTH = 255
classmethod convert(k)[source]

Default key converter for SQL Server application lock

Return type:

str

Parameters:

k (bytes | bytearray | memoryview | str | int | float)

get_actual_key()[source]

The actual key used in SQL Server application lock

Return type:

str

property lock_mode: Literal['Shared', 'Update', 'Exclusive']

The lock mode being used (for informational purposes)

property shared: bool

Is the lock mode Shared

property update: bool

Is the lock mode Update