Shalini
Shalini

Reputation: 348

How to resolve XDMP-CONFLICTINGUPDATES?

This Question is in reference with Data Hub Framework-

I am having 3-4 conditions in which i am doing operations like xdmp:node-replace and xdmp:document-delete and after all the conditions i am trying to insert the document using xdmp:document-insert.

When i am running the conditions independently by commenting the other conditions then it is working fine but if i am trying to run 2 or more conditions together- i am getting XDMP-CONFLICTINGUPDATES

$envelope is coming from STAGING Database which i am using in writer.xqy

The code sample is as below-

let $con1 := if($envelope/*:test/text() eq "abc")
                 then xdmp:node-replace(....) else ()
let $con2 := if($envelope/*:test/text() eq "123")
                 then xdmp:node-replace(....) else ()
let $con1 := if($envelope/*:test/text() eq "cde")
                 then xdmp:document-delete(....) else ()
return if($envelope//*FLAG/text() eq "1")
    then
   xdmp:document-insert($id, $envelope, xdmp:default-permissions(), map:get($options, "entity"))

Any Suggestions ?

Upvotes: 1

Views: 1166

Answers (4)

Navin Rawat
Navin Rawat

Reputation: 3138

This is multi transaction statement. There are multiple ways to handle it in your scenario:

  1. Use xdmp:eval
  2. Use mem library of MarkLogic to replace your nodes
  3. Rewrite your Query to avoid transaction conflict

Upvotes: 0

asusu
asusu

Reputation: 321

One general possibility for complicated updates: use XSLT.

Upvotes: 1

Likely the XQuery statement above is attempting multiple updates to the same node in the same single-statement transaction. The xdmp:node-replace calls are performing updates at each operation to the same node. See the documentation for more details.

Here are two solutions that may work for you

  1. Use conditional statements to decide what kind of update needs to be performed on the node, e.g., whether the node need to be deleted, whether the node needs to be updated and how. At the end of your script you could then apply the update behavior to the node.
  2. Perform in-memory updates to the node then commit the node to the database at the end of the transaction. Here is one library you could use https://github.com/ryanjdew/XQuery-XML-Memory-Operations

Upvotes: 4

Rob S.
Rob S.

Reputation: 3609

XDMP-ConflictingUpdates means you are trying to update the same node more than once within a single transaction. Solving these types of errors can be infamously tricky and are a rite of passage for every MarkLogician.

In your case, this is caused by updating a node with xdmp:node-replace and then updating the document node which is the parent of that node with xdmp:document-insert. Thus, because you are updating both the node and its parent, you are in effect updating that node twice causing the error. Or, this may also occur from trying to both delete and insert a document at the same URI within the same transaction.

Here is a simple query you can run in QConsole to reproduce this behavior:

xquery version "1.0-ml";

xdmp:document-insert("/test.xml", <test><value></value></test>);

xquery version "1.0-ml";

let $d := fn:doc("/test.xml")

let $_ := xdmp:node-replace($d//value, <value>test</value>)

return
  xdmp:document-insert("/test.xml", $d)

In the case of this demonstration, as well as your code, the xdmp:document-insert is redundant and can simply be removed.

Upvotes: 4

Related Questions