Avbasot
Avbasot

Reputation: 363

xdmp:eval and Update transactions

I have a test case here:

let $s := 
'xquery version "1.0-ml";
for $doc in xdmp:directory("/test/")
              return
              xdmp:node-insert-child(doc(xdmp:node-uri($doc))/a,
    <b>bbb</b>)
return
(xdmp:eval($s, (), <options xmlns="xdmp:eval">
      <isolation>different-transaction</isolation>
      <prevent-deadlocks>true</prevent-deadlocks>
    </options>), xdmp:directory("/test/"))

My main goal here is to add a new element to each document in "/test/" and return the results in the same transaction. With xdmp:eval() and the "different-transaction" option I thought the eval-ed code would be executed in a separate transaction whose updates would be available to subsequent calls... so the 2nd and last xdmp:directory("/test/") should be returning all the updated documents with the <b> element, yet it returns the non-updated documents.

Were there any caveats here with xdmp:eval and what I'm trying to achieve in a single transaction?

Upvotes: 0

Views: 713

Answers (1)

Marklogic 8 In this case, The idea is that I do the update in a function that is invoked from the main script. The settings I used not only commit the doc, but also expose the the new commit to the initial code. Because of how MarkLogic handles timestamps in code, just because something may be committed does not mean it is available to the code. Why? Because the timestamp of the commit is after the code started to run (or something like that). This approach solves that. Also, I chose MarkLogic 8 example because the original question did not scope it to a previous release. So I went with the solution for the current version of MarkLogic.

xquery version "1.0-ml";

(: load docs 
for $foo in (1 to 5)
  let $uri := "/test/child/foo" || xs:string($foo) || ".xml"
  return xdmp:document-insert($uri, <foo><bar></bar></foo>)
:)  

let $update := xdmp:invoke-function(function(){
    (for $uri in cts:uri-match("/test/child/foo*")
      return xdmp:node-insert-child(doc($uri)/foo/bar, <baz>bbb</baz>)  
    ,xdmp:commit()
    )
  },
  <options xmlns="xdmp:eval">
      <transaction-mode>update-auto-commit</transaction-mode>
      <isolation>different-transaction</isolation>
    </options>
)

return xdmp:directory("/test/child/")

Upvotes: 3

Related Questions