You are viewing the documentation for a prerelease version.

SET TRANSACTION

  • Couchbase Server 7.0
    +
    The SET TRANSACTION statement enables you to specify settings for a transaction.

    Purpose

    The SET TRANSACTION statement enables you to specify settings for an ACID transaction. Refer to N1QL Support for Couchbase Transactions for further information.

    This statement may only be used within a transaction.

    If you are using the Query REST API, you must set the txid query parameter to specify the transaction ID.

    If you are using the Query Workbench, you don’t need to specify the transaction ID, as long as the statement is part of a multi-statement request. When you start a transaction within a multi-statement request, all statements within the request are assumed to be part of the same transaction until you rollback or commit the transaction.

    Similarly, if you are using the cbq shell, you don’t need to specify the transaction ID. Once you have started a transaction, all statements within the cbq shell session are assumed to be part of the same transaction until you rollback or commit the transaction. [1]

    You may also optionally specify settings when you start the transaction using the BEGIN TRANSACTION command.

    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.

    Syntax

    set-transaction ::= SET TRANSACTION ISOLATION LEVEL READ COMMITTED
    'SET' 'TRANSACTION' 'ISOLATION' 'LEVEL' 'READ' 'COMMITTED'

    Example

    If you want to try this example, first refer to Preparation to set up your environment.

    Example 1. Specify transaction settings
    Transaction
    BEGIN WORK;
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED (1)
    UPSERT INTO test VALUES("abc2", {"a":1});
    SAVEPOINT s1;
    UPDATE test AS d SET d.b = 10 WHERE d.a > 0;
    SELECT d.*, META(d).id FROM test AS d WHERE d.a >= 0;
    SAVEPOINT s2;
    UPDATE test AS d SET d.b = 10, d.c = "xyz" WHERE d.a > 0;
    SELECT d.*, META(d).id FROM test AS d WHERE d.a >= 0;
    ROLLBACK TRAN TO SAVEPOINT s2;
    SELECT d.*, META(d).id FROM test AS d WHERE d.a >= 0;
    COMMIT WORK;
    1 Specify transaction settings.
    Results
    [
      {
        "_sequence_num": 1,
        "_sequence_query": "BEGIN WORK;",
        "_sequence_query_status": "success",
        "_sequence_result": [
          {
            "txid": "9d0e0818-085a-40cf-92fc-8473cd5be586"
          }
        ]
      },
      {
        "_sequence_num": 2,
        "_sequence_query": "\nSET TRANSACTION ISOLATION LEVEL READ COMMITTED;",
        "_sequence_query_status": "success",
        "_sequence_result": {
          "results": []
        }
      },
      {
        "_sequence_num": 3,
        "_sequence_query": "\nUPSERT INTO test VALUES(\"abc2\", {\"a\":1});",
        "_sequence_query_status": "success",
        "_sequence_result": {
          "results": []
        }
      },
    ...
    ]

    1. You must be using cbq shell version 2.0 or above to use the automatic transaction ID functionality.