Error Handling

    +
    Handling transaction errors with Couchbase.

    Unresolved include directive in modules/concept-docs/pages/transactions-error-handling.adoc - include::7.1.2@sdk:shared:partial$acid-transactions.adoc[]

    Transaction Errors

    Unresolved include directive in modules/concept-docs/pages/transactions-error-handling.adoc - include::7.1.2@sdk:shared:partial$acid-transactions.adoc[]

    Unresolved include directive in modules/concept-docs/pages/transactions-error-handling.adoc - include::7.1.2@sdk:shared:partial$acid-transactions.adoc[]

    var cluster = Cluster.connect("localhost", ClusterOptions.clusterOptions("username", "password")
            .environment(env -> env.transactionsConfig(TransactionsConfig.timeout(Duration.ofSeconds(120)))));

    Alternatively it can be configured at the per-transaction level:

    cluster.transactions().run((ctx) -> {
        // Your transaction logic
    }, transactionOptions().timeout(Duration.ofSeconds(60)));

    Unresolved include directive in modules/concept-docs/pages/transactions-error-handling.adoc - include::7.1.2@sdk:shared:partial$acid-transactions.adoc[]

    Full Error Handling Example

    Pulling all of the above together, this is the suggested best practice for error handling:

    try {
        TransactionResult result = cluster.transactions().run((ctx) -> {
            // ... transactional code here ...
        });
    
        // The transaction definitely reached the commit point.
        // Unstaging the individual documents may or may not have completed
    
        if (!result.unstagingComplete()) {
            // In rare cases, the application may require the commit to have
            // completed (recall that the asynchronous cleanup process is
            // still working to complete the commit).
            // The next step is application-dependent.
        }
    } catch (TransactionCommitAmbiguousException e) {
        throw logCommitAmbiguousError(e);
    } catch (TransactionFailedException e) {
        throw logFailure(e);
    }