Uploaded image for project: 'Couchbase Server'
  1. Couchbase Server
  2. MB-50491

[N1QL][UDF-TXN] getting error savepoint not supported outside of txn while trying to begin work through a prepared statement

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Critical
    • 7.1.0
    • 7.1.0
    • js-evaluator, query
    • None
    • 7.1.0-2093
    • Untriaged
    • 1
    • Unknown

    Description

      2 nodes
      kv,n1ql,index

      PREPARE beginWork as BEGIN WORK
      PREPARE commitWork AS COMMIT
      PREPARE rollbackWork AS ROLLBACK TRANSACTION

      function savepoint_default() {            
          var start_txn = EXECUTE beginWork;            
          var query1 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":1, "b":2}) ;            
          var query2 = SAVEPOINT S1;            
          var query3 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":2, "b":2}) ;            
          var query4 = SAVEPOINT S2;            
          var query5 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":3, "b":2}) ;            
          var query6 = SELECT * FROM default.`_default`.txn_scope ORDER BY a; 
          var acc = [];            
          for (const row of query6) { acc.push(row); }            
          var query7 = EXECUTE rollbackWork;            
          return acc;}
      

      execute function savepoint_default()

      [
      {
      "code": 10109,
      "msg": "Error executing function savepoint_default (n1ql:savepoint_default): Evaluator error Exceptions from JS code : run savepoint_default failed:\nException: Error: SAVEPOINT statement is not supported outside the transaction\nLocation: functions/n1ql.js:4 \nCode: var query2 = N1QL('SAVEPOINT S1;', {}, false); \nError\n at savepoint_default (functions/n1ql.js:4:18)\n - cause: Evaluator error Exceptions from JS code : run savepoint_default failed:\nException: Error: SAVEPOINT statement is not supported outside the transaction\nLocation: functions/n1ql.js:4 \nCode: var query2 = N1QL('SAVEPOINT S1;', {}, false); \nError\n at savepoint_default (functions/n1ql.js:4:18)\n"
      }
      ]

      However if i modify the above function to instead manually begin work

      function savepoint_default() {            
          var start_txn = BEGIN WORK;            
          var query1 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":1, "b":2}) ;            
          var query2 = SAVEPOINT S1;            
          var query3 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":2, "b":2}) ;            
          var query4 = SAVEPOINT S2;            
          var query5 = INSERT INTO default.`_default`.txn_scope(key, value) VALUES (UUID(), {"a":3, "b":2}) ;            
          var query6 = SELECT * FROM default.`_default`.txn_scope ORDER BY a; 
          var acc = [];            
          for (const row of query6) { acc.push(row); }            
          var query7 = EXECUTE rollbackWork;            
          return acc;}
      

      execute function savepoint_default()

      [
      [
      {
      "txn_scope":

      { "a": 1, "b": 2 }

      },
      {
      "txn_scope":

      { "a": 2, "b": 2 }

      },
      {
      "txn_scope":

      { "a": 3, "b": 2 }

      }
      ]
      ]

      select * from default.`_default`.txn_scope

      { "results": [] }

      We see the correct behavior, all the inserts happen and at the end it is rolled back so there are no documents actually added to the collection.

      If I simply run EXECUTE beginWork directly, i see it successfully start a txn

      EXECUTE beginWork;

      [

      { "txid": "d905a6a9-3a2a-4f5d-846f-45e943d72e7a" }

      ]

      Attaching logs

      Attachments

        Activity

          People

            ajay.bhullar Ajay Bhullar
            ajay.bhullar Ajay Bhullar
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              PagerDuty