2

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

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

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

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
    }
}
4

1 回答 1

2

这两种方法都尝试访问为空的 TransactionMode 的属性。

调用setAutoCommit()orsetUpdate()或显式设置 TransactionModesetTransactionMode()将确保txnModeis not null

如果升级到 9.0.4,SessionImpl将返回没有 NPEisAutoCommit()的默认值:true

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

但是如果你getUpdate()在没有txnMode建立的情况下调用,你仍然会得到一个 NPE:

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

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

于 2018-02-25T18:06:27.163 回答