JavaScript Multi-statement Transaction in Marklogic












0















I wanted to write a multi-statement transaction in server-side JavaScript in marklogic. What I wanted to achieve is, do an update transaction and then write a query statement which queries for the updated document and confirm that the update is visible within the transaction and finally do a rollback. By doing the rollback, I wanted to confirm that the update made within the transaction is not visible outside the transaction and it is visible within the transaction.
I wrote a code both in Xquery as well as serverside JavaScript inorder to achieve this using xdmp:eval/xdmp.eval. I was able to successfully achieve it using Xquery but not in serverside Javascript.



Following is my Xquery code:



xquery version "1.0-ml";
declare option xdmp:transaction-mode "update";

let $query :=
'xquery version "1.0-ml";
xdmp:document-insert("/docs/first.json", <myData/>)
'
return xdmp:eval(
$query, (),
<options xmlns="xdmp:eval">
<isolation>same-statement</isolation>
</options>);

if (fn:doc("/docs/first.json"))
then ("VISIBLE")
else ("NOT VISIBLE");

xdmp:rollback()


Below is my serverside JavaScript code:



declareUpdate();
var query = 'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1}); '
xdmp.eval(query,null,{isolation:'same-statement'})

fn.doc("/docs/first.json")

if (fn.doc("/docs/first.json"))
var result = ("visible")
else var result = ("not visible");

xdmp.rollback()
result


I am executing both these codes through Query Console. I am expecting to see the result "visible" in both cases. But when running the serverside JavaScript code, it is throwing me error: [javascript] TypeError: Cannot read property 'result' of null due to xdmp.rollback and not able see the value in variable 'result'



Can someone please correct what is going wrong in my serverside javascript code?










share|improve this question



























    0















    I wanted to write a multi-statement transaction in server-side JavaScript in marklogic. What I wanted to achieve is, do an update transaction and then write a query statement which queries for the updated document and confirm that the update is visible within the transaction and finally do a rollback. By doing the rollback, I wanted to confirm that the update made within the transaction is not visible outside the transaction and it is visible within the transaction.
    I wrote a code both in Xquery as well as serverside JavaScript inorder to achieve this using xdmp:eval/xdmp.eval. I was able to successfully achieve it using Xquery but not in serverside Javascript.



    Following is my Xquery code:



    xquery version "1.0-ml";
    declare option xdmp:transaction-mode "update";

    let $query :=
    'xquery version "1.0-ml";
    xdmp:document-insert("/docs/first.json", <myData/>)
    '
    return xdmp:eval(
    $query, (),
    <options xmlns="xdmp:eval">
    <isolation>same-statement</isolation>
    </options>);

    if (fn:doc("/docs/first.json"))
    then ("VISIBLE")
    else ("NOT VISIBLE");

    xdmp:rollback()


    Below is my serverside JavaScript code:



    declareUpdate();
    var query = 'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1}); '
    xdmp.eval(query,null,{isolation:'same-statement'})

    fn.doc("/docs/first.json")

    if (fn.doc("/docs/first.json"))
    var result = ("visible")
    else var result = ("not visible");

    xdmp.rollback()
    result


    I am executing both these codes through Query Console. I am expecting to see the result "visible" in both cases. But when running the serverside JavaScript code, it is throwing me error: [javascript] TypeError: Cannot read property 'result' of null due to xdmp.rollback and not able see the value in variable 'result'



    Can someone please correct what is going wrong in my serverside javascript code?










    share|improve this question

























      0












      0








      0








      I wanted to write a multi-statement transaction in server-side JavaScript in marklogic. What I wanted to achieve is, do an update transaction and then write a query statement which queries for the updated document and confirm that the update is visible within the transaction and finally do a rollback. By doing the rollback, I wanted to confirm that the update made within the transaction is not visible outside the transaction and it is visible within the transaction.
      I wrote a code both in Xquery as well as serverside JavaScript inorder to achieve this using xdmp:eval/xdmp.eval. I was able to successfully achieve it using Xquery but not in serverside Javascript.



      Following is my Xquery code:



      xquery version "1.0-ml";
      declare option xdmp:transaction-mode "update";

      let $query :=
      'xquery version "1.0-ml";
      xdmp:document-insert("/docs/first.json", <myData/>)
      '
      return xdmp:eval(
      $query, (),
      <options xmlns="xdmp:eval">
      <isolation>same-statement</isolation>
      </options>);

      if (fn:doc("/docs/first.json"))
      then ("VISIBLE")
      else ("NOT VISIBLE");

      xdmp:rollback()


      Below is my serverside JavaScript code:



      declareUpdate();
      var query = 'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1}); '
      xdmp.eval(query,null,{isolation:'same-statement'})

      fn.doc("/docs/first.json")

      if (fn.doc("/docs/first.json"))
      var result = ("visible")
      else var result = ("not visible");

      xdmp.rollback()
      result


      I am executing both these codes through Query Console. I am expecting to see the result "visible" in both cases. But when running the serverside JavaScript code, it is throwing me error: [javascript] TypeError: Cannot read property 'result' of null due to xdmp.rollback and not able see the value in variable 'result'



      Can someone please correct what is going wrong in my serverside javascript code?










      share|improve this question














      I wanted to write a multi-statement transaction in server-side JavaScript in marklogic. What I wanted to achieve is, do an update transaction and then write a query statement which queries for the updated document and confirm that the update is visible within the transaction and finally do a rollback. By doing the rollback, I wanted to confirm that the update made within the transaction is not visible outside the transaction and it is visible within the transaction.
      I wrote a code both in Xquery as well as serverside JavaScript inorder to achieve this using xdmp:eval/xdmp.eval. I was able to successfully achieve it using Xquery but not in serverside Javascript.



      Following is my Xquery code:



      xquery version "1.0-ml";
      declare option xdmp:transaction-mode "update";

      let $query :=
      'xquery version "1.0-ml";
      xdmp:document-insert("/docs/first.json", <myData/>)
      '
      return xdmp:eval(
      $query, (),
      <options xmlns="xdmp:eval">
      <isolation>same-statement</isolation>
      </options>);

      if (fn:doc("/docs/first.json"))
      then ("VISIBLE")
      else ("NOT VISIBLE");

      xdmp:rollback()


      Below is my serverside JavaScript code:



      declareUpdate();
      var query = 'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1}); '
      xdmp.eval(query,null,{isolation:'same-statement'})

      fn.doc("/docs/first.json")

      if (fn.doc("/docs/first.json"))
      var result = ("visible")
      else var result = ("not visible");

      xdmp.rollback()
      result


      I am executing both these codes through Query Console. I am expecting to see the result "visible" in both cases. But when running the serverside JavaScript code, it is throwing me error: [javascript] TypeError: Cannot read property 'result' of null due to xdmp.rollback and not able see the value in variable 'result'



      Can someone please correct what is going wrong in my serverside javascript code?







      javascript transactions marklogic serverside-javascript acid-state






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 14:13









      sharusharu

      264




      264
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.



          (The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)



          Something similar to the following should work:



          'use strict';
          xdmp.eval(
          'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
          null,
          {isolation:'different-transaction'});
          const doc = xdmp.eval(
          'cts.doc("/docs/first.json")',
          null,
          {isolation:'different-transaction'});
          fn.exists(doc);


          That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.



          It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.



          Hoping that helps,






          share|improve this answer
























          • As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

            – sharu
            Nov 20 '18 at 14:18













          • This doesnt work :(

            – sharu
            Nov 27 '18 at 10:10











          • What error do you get? The code above works without issue for me on current MarkLogic 9.

            – ehennum
            Nov 27 '18 at 23:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282962%2fjavascript-multi-statement-transaction-in-marklogic%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.



          (The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)



          Something similar to the following should work:



          'use strict';
          xdmp.eval(
          'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
          null,
          {isolation:'different-transaction'});
          const doc = xdmp.eval(
          'cts.doc("/docs/first.json")',
          null,
          {isolation:'different-transaction'});
          fn.exists(doc);


          That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.



          It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.



          Hoping that helps,






          share|improve this answer
























          • As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

            – sharu
            Nov 20 '18 at 14:18













          • This doesnt work :(

            – sharu
            Nov 27 '18 at 10:10











          • What error do you get? The code above works without issue for me on current MarkLogic 9.

            – ehennum
            Nov 27 '18 at 23:07
















          1














          In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.



          (The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)



          Something similar to the following should work:



          'use strict';
          xdmp.eval(
          'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
          null,
          {isolation:'different-transaction'});
          const doc = xdmp.eval(
          'cts.doc("/docs/first.json")',
          null,
          {isolation:'different-transaction'});
          fn.exists(doc);


          That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.



          It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.



          Hoping that helps,






          share|improve this answer
























          • As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

            – sharu
            Nov 20 '18 at 14:18













          • This doesnt work :(

            – sharu
            Nov 27 '18 at 10:10











          • What error do you get? The code above works without issue for me on current MarkLogic 9.

            – ehennum
            Nov 27 '18 at 23:07














          1












          1








          1







          In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.



          (The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)



          Something similar to the following should work:



          'use strict';
          xdmp.eval(
          'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
          null,
          {isolation:'different-transaction'});
          const doc = xdmp.eval(
          'cts.doc("/docs/first.json")',
          null,
          {isolation:'different-transaction'});
          fn.exists(doc);


          That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.



          It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.



          Hoping that helps,






          share|improve this answer













          In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.



          (The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)



          Something similar to the following should work:



          'use strict';
          xdmp.eval(
          'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
          null,
          {isolation:'different-transaction'});
          const doc = xdmp.eval(
          'cts.doc("/docs/first.json")',
          null,
          {isolation:'different-transaction'});
          fn.exists(doc);


          That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.



          It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.



          Hoping that helps,







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:31









          ehennumehennum

          5,42797




          5,42797













          • As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

            – sharu
            Nov 20 '18 at 14:18













          • This doesnt work :(

            – sharu
            Nov 27 '18 at 10:10











          • What error do you get? The code above works without issue for me on current MarkLogic 9.

            – ehennum
            Nov 27 '18 at 23:07



















          • As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

            – sharu
            Nov 20 '18 at 14:18













          • This doesnt work :(

            – sharu
            Nov 27 '18 at 10:10











          • What error do you get? The code above works without issue for me on current MarkLogic 9.

            – ehennum
            Nov 27 '18 at 23:07

















          As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

          – sharu
          Nov 20 '18 at 14:18







          As per your answer the document insertion and reading the document happens in different transactions. But what I was expecting is document insertion and reading the document just happening in multiple lines within a single transaction.The purpose of doing the document read within the same transaction is that, I wanted to prove that, different statements within a single javascript transaction are able to see the updates of each other stements within thesame transaction, just like in the case of Xquery statements separated with a ";" in single transaction

          – sharu
          Nov 20 '18 at 14:18















          This doesnt work :(

          – sharu
          Nov 27 '18 at 10:10





          This doesnt work :(

          – sharu
          Nov 27 '18 at 10:10













          What error do you get? The code above works without issue for me on current MarkLogic 9.

          – ehennum
          Nov 27 '18 at 23:07





          What error do you get? The code above works without issue for me on current MarkLogic 9.

          – ehennum
          Nov 27 '18 at 23:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282962%2fjavascript-multi-statement-transaction-in-marklogic%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values