Table of Contents

Class PostgreSqlTransactionLock

Namespace
NuvTools.Data.EntityFrameworkCore.PostgreSQL.Context
Assembly
NuvTools.Data.EntityFrameworkCore.PostgreSQL.dll

PostgreSQL implementation of IDbContextTransactionLock, on top of pg_advisory_xact_lock: blocks until the lock is granted and releases it when the transaction ends.

public sealed class PostgreSqlTransactionLock : IDbContextTransactionLock
Inheritance
PostgreSqlTransactionLock
Implements
Inherited Members

Remarks

PostgreSQL keys advisory locks by a 64-bit integer, so the caller's name is hashed into one. The hash is SHA-256 rather than GetHashCode(), which is randomized per process — two replicas would derive different keys for the same name and neither would block the other.

Methods

AcquireAsync(DbContext, string, CancellationToken)

Acquires the exclusive lock named name, blocking until it is granted, and holds it until the current transaction of context ends.

public Task AcquireAsync(DbContext context, string name, CancellationToken cancellationToken = default)

Parameters

context DbContext

The context whose transaction and connection the lock belongs to.

name string

Lock name, as given by the caller.

cancellationToken CancellationToken

A cancellation token to observe while waiting for the task to complete.

Returns

Task

A task that completes once the lock is held.