sqlalchemy_dlock.lock.oracle module

Oracle database lock implementation using DBMS_LOCK

class sqlalchemy_dlock.lock.oracle.OracleAsyncSadLock(connection_or_session, key, **kwargs)[source]

Bases: OracleSadLockMixin, BaseAsyncSadLock[int, AsyncConnection | AsyncSession | async_scoped_session]

Async IO version of OracleSadLock

Parameters:
  • key (Any) – Oracle lock identifier - When int: used directly as lock ID (must be 0-1073741823) - When str/bytes: hashed to integer via blake2b - Other types are converted using default or custom converter

  • convert – Custom function to convert key to int

  • lock_mode – Lock mode to use - “X” (default): Exclusive mode - full exclusive access - “S”: Shared mode - multiple readers - “SS”: Sub-Shared - for aggregate objects - “SX”: Sub-Exclusive (Row Exclusive) - “SSX”: Shared Sub-Exclusive (Share Row Exclusive) - “NL”: Null mode - no actual locking

  • release_on_commit – Whether to release lock on commit/rollback - False (default): Lock held until explicit release or session ends - True: Lock released when transaction ends

  • connection_or_session (AsyncConnection | AsyncSession | async_scoped_session)

Lock Mode Compatibility Matrix: (Held Mode vs Get Mode: S=Success, F=Fail) HeldGet | NL | SS | SX | S | SSX | X ———|----|—-|----|—-|-----|— NL | S | S | S | S | S | S SS | S | S | S | S | S | F SX | S | S | S | F | F | F S | S | S | F | S | F | F SSX | S | S | F | F | F | F X | S | F | F | F | F | F

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

bool

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

Bases: OracleSadLockMixin, BaseSadLock[int, Connection | Session | scoped_session]

Distributed lock implemented by Oracle DBMS_LOCK

Tip

Oracle user locks are identified with the prefix “UL” and can be viewed in Enterprise Manager lock monitor or appropriate V$ views.

Locks are automatically released when a session terminates.

String keys are converted to integer IDs using blake2b hash, similar to PostgreSQL’s advisory lock implementation.

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

Acquire the lock using DBMS_LOCK.REQUEST.

Returns:

Success 1: Timeout 2: Deadlock 3: Parameter error 4: Already own lock 5: Illegal lock ID

Return type:

bool

Parameters:
do_release()[source]

Release the lock using DBMS_LOCK.RELEASE.

Returns:

Success 3: Parameter error 4: Don’t own lock 5: Illegal lock ID

Return type:

0

class sqlalchemy_dlock.lock.oracle.OracleSadLockMixin(*, key, convert=None, lock_mode='X', release_on_commit=False, **kwargs)[source]

Bases: AbstractLockMixin[KTV, int]

Mixin class for Oracle DBMS_LOCK

Parameters:
  • key (TypeVar(KTV, bound= Any)) – Oracle lock identifier - When int: used directly as lock ID (must be 0-1073741823) - When str/bytes: hashed to integer via blake2b - Other types are converted using default or custom converter

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

  • lock_mode (Literal['NL', 'SS', 'SX', 'S', 'SSX', 'X']) – Lock mode to use - “X” (default): Exclusive mode - full exclusive access - “S”: Shared mode - multiple readers - “SS”: Sub-Shared - for aggregate objects - “SX”: Sub-Exclusive (Row Exclusive) - “SSX”: Shared Sub-Exclusive (Share Row Exclusive) - “NL”: Null mode - no actual locking

  • release_on_commit (bool) – Whether to release lock on commit/rollback - False (default): Lock held until explicit release or session ends - True: Lock released when transaction ends

Lock Mode Compatibility Matrix: (Held Mode vs Get Mode: S=Success, F=Fail) HeldGet | NL | SS | SX | S | SSX | X ———|----|—-|----|—-|-----|— NL | S | S | S | S | S | S SS | S | S | S | S | S | F SX | S | S | S | F | F | F S | S | S | F | S | F | F SSX | S | S | F | F | F | F X | S | F | F | F | F | F

NL_MODE = 1
SSX_MODE = 5
SS_MODE = 2
SX_MODE = 3
S_MODE = 4
X_MODE = 6
classmethod convert(k)[source]

Default key converter for Oracle DBMS_LOCK

Similar to PostgreSQL: strings/bytes are hashed using blake2b.

Return type:

int

Parameters:

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

classmethod ensure_valid_id(i)[source]

Ensure the integer is in Oracle’s lock ID range (0 to 1073741823)

Parameters:

i (int) – Integer to validate

Return type:

int

Returns:

Valid lock ID in range [0, 1073741823]

Raises:

TypeError – If input is not an integer

get_actual_key()[source]

The actual key used in Oracle DBMS_LOCK

Return type:

int

property lock_mode: Literal['NL', 'SS', 'SX', 'S', 'SSX', 'X']

The lock mode being used

property lock_mode_int: int

The lock mode as integer (for DBMS_LOCK)

property release_on_commit: bool

Whether the lock is released on commit/rollback