pub struct DatabaseLock { /* private fields */ }Expand description
Exclusive advisory lock on a database directory
This lock ensures single-process access to a SochDB database. The lock is automatically released when this struct is dropped.
§Implementation
Uses POSIX flock() on Unix systems and LockFileEx() on Windows.
The lock file also contains the PID of the lock holder for debugging
and stale lock detection.
§Safety
Advisory locks are cooperative - they only work if all processes attempting to access the database use this locking mechanism.
Implementations§
Source§impl DatabaseLock
impl DatabaseLock
Sourcepub fn acquire<P: AsRef<Path>>(db_path: P) -> Result<Self, LockError>
pub fn acquire<P: AsRef<Path>>(db_path: P) -> Result<Self, LockError>
Acquire exclusive lock on a database directory
Uses the default timeout (5 seconds) to allow concurrent processes to wait for the lock, similar to SQLite’s busy timeout behavior. This enables multi-process patterns like ProcessPoolExecutor where each worker serializes database access through lock contention.
§Arguments
db_path- Path to the database directory
§Returns
Returns Ok(DatabaseLock) if lock acquired successfully.
Returns Err(LockError::Timeout) if lock not acquired within 5 seconds.
Returns Err(LockError::DatabaseLocked) if another process holds the lock
and stale lock detection fails.
§Example
let lock = DatabaseLock::acquire("/path/to/db")?;
// Lock is held until `lock` is droppedSourcepub fn acquire_no_wait<P: AsRef<Path>>(db_path: P) -> Result<Self, LockError>
pub fn acquire_no_wait<P: AsRef<Path>>(db_path: P) -> Result<Self, LockError>
Acquire exclusive lock without waiting (fail immediately if locked)
Use this only when you want to detect if another process has the database open, without blocking.
Sourcepub fn acquire_with_timeout<P: AsRef<Path>>(
db_path: P,
timeout: Duration,
) -> Result<Self, LockError>
pub fn acquire_with_timeout<P: AsRef<Path>>( db_path: P, timeout: Duration, ) -> Result<Self, LockError>
Acquire exclusive lock with timeout
Will retry lock acquisition until timeout expires.
§Arguments
db_path- Path to the database directorytimeout- Maximum time to wait for lock
Sourcepub fn acquire_with_config<P: AsRef<Path>>(
db_path: P,
config: &LockConfig,
) -> Result<Self, LockError>
pub fn acquire_with_config<P: AsRef<Path>>( db_path: P, config: &LockConfig, ) -> Result<Self, LockError>
Acquire exclusive lock with full configuration
Trait Implementations§
Source§impl Drop for DatabaseLock
impl Drop for DatabaseLock
Auto Trait Implementations§
impl Freeze for DatabaseLock
impl RefUnwindSafe for DatabaseLock
impl Send for DatabaseLock
impl Sync for DatabaseLock
impl Unpin for DatabaseLock
impl UnsafeUnpin for DatabaseLock
impl UnwindSafe for DatabaseLock
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more