Reputation: 71
I was trying to run a server side JavaScript code in marklogic to understand the transaction property of marklogic. I have 2 statements, one update and another query statement in a single transaction.
Below is my JavaScript code :
declareUpdate({explicitCommit: true});
xdmp.documentInsert("/docs/first.json", {"first": 1});
fn.doc("/docs/first.json");
xdmp.commit();
//xdmp.rollback()
The above code running in a Query Console is supposed to insert "/docs/first.json" and then query "/docs/first.json".The first statement within the transaction which is document insert is working fine but then the second statement is not running and the query is returning me "null". Also when I was using xdmp.rollback it threw an error:[javascript] TypeError: Cannot read property 'result' of null. Can anyone please explain this because within a single transaction the statements should be able to see the results of the previously executed statements. The same query I had executed in Xquery and I was able to see a result.
Below is my Xquery code:
xquery version "1.0-ml";
declare option xdmp:commit "explicit";
xdmp:document-insert('/docs/mst1.xml', <data/>);
fn:doc('/docs/mst1.xml');
xdmp:document-insert('/docs/mst2.xml', fn:doc('/docs/mst1.xml'));
xdmp:rollback()
The above code is returning me the result document present in uri '/docs/mst1.xml' which is <data> </data>
Upvotes: 1
Views: 94
Reputation: 11771
Based on my understanding of the documentation, it's not possible to execute multi-statement transactions this way from JavaScript:
- In XQuery, semi-colon acts as a separator between statements in the same transaction.
- In Server-Side JavaScript, the entire program (script) is considered a single transactional statement, regardless how many JavaScript statements it contains.
You may need to rely on functions like xdmp.invokeFunction
to explicitly commit multiple statement transactions from JavaScript.
Upvotes: 3