CouchbaseUnlock

Release a distributed lock acquired with couchbaseLock() in manual mode.

Syntax

couchbaseUnlock(cacheName, name, cas)

Parameters

Parameter
Type
Required
Description

cacheName

String

Yes

Name of the Couchbase cache used for locking

name

String

Yes

Unique name of the lock to release

cas

Long

Yes

CAS value returned from couchbaseLock()

Returns

Returns boolean:

  • true - Lock successfully released

  • false - Lock not found or already released (no error thrown)

Examples

Basic Manual Lock/Unlock

With Error Handling

Multiple Operations

Conditional Unlock

Long-Running Operation with Re-acquisition

Inventory Management

Payment Processing

Notes

  • Safe to call: Returns false if lock not found (no exception)

  • CAS required: Must provide CAS from lock acquisition

  • Auto-expiry: Locks expire after specified duration anyway

  • No-op if expired: If lock already expired, returns false

  • Always call: Use in finally block to ensure cleanup

When to Use

Use couchbaseUnlock() when you need:

  • Fine-grained control over lock lifecycle

  • Early release before lock expiration

  • Conditional locking based on runtime logic

  • Long operations with periodic lock release/re-acquisition

For simpler use cases, prefer callback mode in couchbaseLock() which handles unlock automatically.

Error Handling

couchbaseUnlock() does NOT throw exceptions:

Best Practices

✅ DO

❌ DON'T

Callback Mode Alternative

Instead of manual lock/unlock, consider callback mode:

Callback mode is simpler and safer for most use cases.

See Also

Last updated

Was this helpful?