Create Couchbase Transactions with SQL++

  • how-to
    +
    How to create Couchbase transactions using SQL++.

    Introduction

    Couchbase transactions enable you to carry out ACID (atomic, consistent, isolated, and durable) actions on the database. This how-to guide covers SQL++ support for Couchbase transactions. Some SDKs also support Couchbase transactions. Refer to Related Links for further details.

    Only DML (data modification language) statements are permitted within a transaction: INSERT, UPSERT, DELETE, UPDATE, MERGE, SELECT, EXECUTE FUNCTION, PREPARE, or EXECUTE.

    If you want to try out the examples in this section, follow the instructions given in Create a Free Account and Deploy Your Database to create a free account, deploy a database, and load a sample dataset. Read the following for further information about the tools available for editing and executing queries:

    Please note that the examples in this guide will alter the data in your sample database. To restore your sample data, remove and reinstall the travel sample data. Refer to Import Data with the Capella UI for details.

    Transaction Parameters

    You can specify various settings and parameters to control how transactions work. You can access transaction settings and parameters through any of the usual Query tools, such as the Query Workbench or the cbq shell.

    • Query Workbench

    To specify parameters for a Couchbase transaction, use the Query Options dialog.

    1. To display the Query Options dialog, click Query Options.

    2. To specify the transaction scan consistency, in the Scan Consistency list, select an option.

    3. To specify the transaction timeout, in the Transaction Timeout box, enter a value in seconds.

    4. To specify any other transaction parameters, click the + button in the Named Parameters section. When the new named parameter appears, enter the name in the name box and a value in the value box.

    5. To save your preferences and return to the Query Workbench, click Save.


    The following example shows transaction parameters for the examples on this page.

    The Query Options dialog, with Scan Consistency set to "not_bounded", Transaction Timeout set to "120", and named parameter "durability_level" set to "none"
    • The transaction scan consistency is set to not_bounded.

    • The durability level of all the mutations within the transaction is set to "none".

    • The transaction timeout is set to 120.

    For further details, refer to Transaction Settings and Parameters.

    Multiple Statement Transactions

    A Couchbase transaction may contain multiple DML statements. In this case, you must use SQL++ transaction statements to support the transaction:

    • Query Workbench

    To execute a transaction containing multiple statements:

    1. Compose the sequence of statements in the Query Editor. Each statement must be terminated with a semicolon.

    2. After each statement, press Shift+Enter to start a new line without executing the query.

    3. When you have entered the entire transaction, click Run to execute the transaction.


    For a worked example showing a complete transaction using SQL++, see Transaction Worked Example. Individual SQL++ transaction statements are described in the sections below.

    Begin a Transaction

    To start a transaction, use the BEGIN TRANSACTION statement.

    The following statement begins a transaction.

    BEGIN WORK;

    Click the View button to see this code in context.

    Result
    {
      "txid": "d3bbf182-1179-42ba-8900-eb20fda69e42" (1)
    }
    1 Beginning a transaction returns the transaction ID.

    For further details, refer to BEGIN TRANSACTION.

    Specify Transaction Settings

    To specify transaction settings, use the SET TRANSACTION statement.

    Currently, the only available transaction setting is "isolation level read committed". This setting is enabled by default. The SET TRANSACTION statement is therefore optional and may be omitted.

    The following statement specifies transaction settings.

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

    Click the View button to see this code in context.

    For further details, refer to SET TRANSACTION.

    Set a Savepoint

    To set a savepoint within a transaction, use the SAVEPOINT statement and specify a name for the savepoint.

    The following statement sets a savepoint.

    SAVEPOINT s1;

    Click the View button to see this code in context.

    For further details, refer to SAVEPOINT.

    Roll Back a Transaction

    To roll back a transaction, use the ROLLBACK TRANSACTION statement.

    By default, this statement rolls back the entire transaction. If you want to roll back to a savepoint, use the TO SAVEPOINT keywords and specify the savepoint name.

    The following statement rolls back a transaction to a specified savepoint.

    ROLLBACK TRAN TO SAVEPOINT s2;

    Click the View button to see this code in context.

    For further details, refer to ROLLBACK TRANSACTION.

    Commit a Transaction

    To commit a transaction, use the COMMIT TRANSACTION statement.

    The following statement commits a transaction.

    COMMIT WORK;

    Click the View button to see this code in context.

    For further details, refer to COMMIT TRANSACTION.