Marklogic中的JavaScript多语句事务 [英] JavaScript Multi-statement Transaction in Marklogic

查看:156
本文介绍了Marklogic中的JavaScript多语句事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在marklogic的服务器端JavaScript中编写多语句事务.我想要实现的是,执行一个更新事务,然后编写一条查询语句,该语句查询更新后的文档并确认该更新在事务中可见,最后进行回滚.通过回滚,我想确认在事务内进行的更新在事务外不可见,并且在事务内是可见的. 为了使用xdmp:eval/xdmp.eval实现此目的,我在Xquery和服务器端JavaScript中都编写了代码.我能够使用Xquery成功实现它,但不能在服务器端Javascript中实现.

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.

以下是我的Xquery代码:

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()

下面是我的服务器端JavaScript代码:

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

我正在通过查询控制台执行这两个代码.我期望在两种情况下都可以看到结果可见".但是,当运行服务器端JavaScript代码时,会抛出错误: [javascript] TypeError:由于xdmp.rollback,无法读取null的属性"result" ,并且无法在变量"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'

有人可以更正我的服务器端javascript代码出了什么问题吗?

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

推荐答案

在SJS和XQuery中,检查事务结果的方法是评估事务和在与编排外部不同的语句中进行检查陈述.

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.

(XQuery分号语法将在不同事务中执行的语句分开-等效于一系列评估,而无需编排外部语句.)

(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.

也不必使用其他事务读取插入的文档以将其返回.只需在xdmp.insert()调用之后返回文档即可.

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.

希望有帮助,

这篇关于Marklogic中的JavaScript多语句事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆