Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Shardingsphere DefaultDistributedLock Lock

From Leeroopedia


Knowledge Sources
Domains Distributed_Locking
Last Updated 2026-02-10 00:00 GMT

Overview

DefaultDistributedLock implements distributed locking using ClusterPersistRepository with thread-local reentrant lock tracking for cluster-mode exclusive operations.

Description

DefaultDistributedLock implements the DistributedLock interface with a reentrant design. It maintains a ConcurrentHashMap of thread-to-LockData mappings, where LockData tracks the lock count via an AtomicInteger. On tryLock, if the current thread already holds the lock, it increments the count and returns true (reentrant). Otherwise, it uses a RetryExecutor with 100ms retry intervals to persistently attempt persistExclusiveEphemeral on the ClusterPersistRepository with the instance ID as the lock value. On unlock, it decrements the lock count; when the count reaches zero, it deletes the lock key from the repository and removes the thread mapping. An IllegalMonitorStateException is thrown if unlock is called by a thread that does not own the lock.

Usage

This is the default distributed lock implementation used when the cluster repository does not provide its own native locking mechanism. It is suitable for general-purpose exclusive operations in cluster mode.

Code Reference

Source Location

Signature

public final class DefaultDistributedLock implements DistributedLock {

    public DefaultDistributedLock(final String lockKey,
                                   final ClusterPersistRepository client,
                                   final DefaultLockTypedProperties props)

    public boolean tryLock(final long timeoutMillis)

    public void unlock()
}

Import

import org.apache.shardingsphere.mode.repository.cluster.core.lock.type.DefaultDistributedLock;

I/O Contract

Inputs

Name Type Required Description
lockKey String Yes Registry path key used as the lock identifier
client ClusterPersistRepository Yes Repository client for ephemeral node persistence
props DefaultLockTypedProperties Yes Properties containing the instance ID
timeoutMillis long Yes Maximum time to wait for lock acquisition

Outputs

Name Type Description
isLocked boolean Whether the lock was successfully acquired

Usage Examples

DefaultDistributedLock lock = new DefaultDistributedLock(
    "/locks/my_operation", clusterRepository, lockProps);

if (lock.tryLock(3000L)) {
    try {
        // Reentrant: calling tryLock again from the same thread increments the count
        lock.tryLock(1000L);
        performExclusiveWork();
        lock.unlock(); // Decrements count
    } finally {
        lock.unlock(); // Reaches zero, deletes the lock key
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment