marklogic - 是否期望在 MarkLogic 9 中的新 Session 对象上的 isAutoCommit 或 getUpdate 上获得 NPE?

标签 marklogic marklogic-9

在 MarkLogic XCC 版本 9.0-3 下,当尝试对新的 Session 对象调用 isAutoCommitgetUpdate 时,我收到 NullPointerException。

如果先调用 setAutoCommitsetUpdate,则不会发生 NPE。这是故意行为吗?如果是这样,为什么?即使没有设置任何值,Session 的所有其他 getter 也会返回而不会出现错误。

我构建了一个最小的可行示例:

import java.net.URI;

import com.marklogic.xcc.ContentSource;
import com.marklogic.xcc.ContentSourceFactory;
import com.marklogic.xcc.Session;

public class mve {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("usage: xcc://user:password@host:port/contentbase");
            return;
        }

        System.out.println("Running minimal viable example of MarkLogic isAutoCommit/getUpdate bug...");

        URI uri = new URI(args[0]);
        ContentSource contentSource = ContentSourceFactory.newContentSource(uri);
        Session updateSession = contentSource.newSession();

        // comment out the following two lines to cause a NullPointerException to be thrown on getUpdate and isAutoCommit:
        updateSession.setAutoCommit(false);
        updateSession.setUpdate(Session.Update.TRUE);

        System.out.println("is AutoCommit?");
        System.out.println(updateSession.isAutoCommit()); // if lines 21 and 22 are both commented out, this will cause NPE

        System.out.println("getUpdate?");
        System.out.println(updateSession.getUpdate());  // if lines 21 and 22 are both commented out, this will cause NPE
    }
}

最佳答案

这两个方法都尝试访问 TransactionMode 的属性,该属性为 null。

调用 setAutoCommit()setUpdate() ,或使用 setTransactionMode() 显式设置 TransactionMode将确保txnMode不为null

如果升级到 9.0.4,SessionImpl isAutoCommit() 将返回默认的 true,不带 NPE:

public boolean isAutoCommit() {
    return txnMode == null ? true : txnMode.isAutoCommit();
} 

但如果您在未建立 txnMode 的情况下调用 getUpdate(),您仍然会得到 NPE:

public Update getUpdate() {
    return txnMode.getUpdate();
}

预期它返回默认的 TransactionMode.AUTO 而不是 NPE 可能是合理的。

关于marklogic - 是否期望在 MarkLogic 9 中的新 Session 对象上的 isAutoCommit 或 getUpdate 上获得 NPE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974060/

相关文章:

marklogic - 使用 cts :query 检查属性是否存在

xquery - 使用 mem :insert-child() is inconsistent 进行内存更新后的节点排序

marklogic - 使用 CPF 将 word 和 ppt 转换为 xml 的操作模块

MarkLogic - 自定义休息 GET REST 服务的性能变化

gradle - 通过ml-gradle为同一模块创建多个MarkLogic调度任务

MarkLogic 搜索包含多边形的文档

java - 使用 java 客户端 api 对 Marklogic 数据库中的文档进行批量修补

java - MarkLogic Java API 死锁检测

marklogic - 是否可以在没有管理员角色的情况下在 MarkLogic 9 上使用 ml-gradle?

java - 使用批处理程序时 MarkLogic 9 Java 客户端出现错误