在 MarkLogic XCC 版本 9.0-3 下,当尝试对新的 Session 对象调用 isAutoCommit
或 getUpdate
时,我收到 NullPointerException。
如果先调用 setAutoCommit
或 setUpdate
,则不会发生 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/