A newer version of this documentation is available.

View Latest

Concurrent Document Mutations

    +

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    var maxRetries = 10;
    for (var i = 0; i < maxRetries; ++i) {
        // Get the current document contents
        console.log("get " + i);
        var result = await collection.get(key);
        cas = result.cas;
        console.log("CAS:", cas.toString('utf8'));
        if (i == 0) {
            console.log("upsert");
            let result = await collection.upsert(key, document);
            console.log("CAS:", result.cas.toString('utf8'));
        }
    
        try {
            // Attempt to replace the document using CAS
            console.log("replace");
            await collection.replace(key, document, { cas: result.cas });
            cas = result.cas;
        } catch (e) {
            // Check if the error thrown is a cas mismatch, if it is, we retry
            if (e instanceof couchbase.CasMismatchError || e instanceof couchbase.DocumentExistsError) {
                console.log(e.toString());
                continue;
            }
            throw e;
        }
        // If no errors occured during the replace, we can exit our retry loop
        break;
    }

    Sometimes more logic is needed when performing updates, for example, if a property is mutually exclusive with another property; only one or the other can exist, but not both.

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    Unresolved include directive in modules/howtos/pages/concurrent-document-mutations.adoc - include::7.0@sdk:shared:partial$cas.adoc[]

    var result = await collection.getAndLock(key, 1000);
    document = result.value;
    cas = result.cas;
    // if we decided not to update, unlock
    //    await collection.unlock(key, result.cas);
    // else ...
    // Attempt to replace the document using CAS
    await collection.replace(key, document, { cas: result.cas });

    The handler will unlock the item either via an explicit unlock operation (unlock) or implicitly via modifying the item with the correct CAS.

    If the item has already been locked, the server will respond with CasMismatch which means that the operation could not be executed temporarily, but may succeed later on.