与DataNucleus 3.x的1-N关系的不稳定加载 [英] Flaky Loading of 1-N Relationships with DataNucleus 3.x

查看:65
本文介绍了与DataNucleus 3.x的1-N关系的不稳定加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataNucleus 3.0.0版本的JDO实现将Java应用程序连接到MongoDB.

I am using DataNucleus 3.0.0-release JDO implementation to connect my Java app to MongoDB.

我的CLASSPATH中的相关JAR如下:

The related JARs in my CLASSPATH are as follows:

  • datanucleus-api-jdo-3.0.0-release.jar
  • datanucleus-core-3.0.0-release.jar
  • datanucleus-jdo-query-3.0.0-release.jar
  • datanucleus-mongodb-3.0.0-release.jar
  • mongo-java-driver-2.5.2.jar

更新:此问题也发生在3.1版中,并且与DataNucleus的Level2缓存有关.解决方法请参见下面的答案.

UPDATE: This problem also occurred with version 3.1 and is related to DataNucleus' Level2 cache. See my answer below for a workaround.

我要加载的类如下:

@PersistenceCapable(detachable = "true")
public class UserProfile implements Serializable, Cacheable<String> {

    private static final long serialVersionUID = 7132595253084641884L;

    @PrimaryKey
    @Persistent(defaultFetchGroup = "true")
    private String username;

    @Persistent(defaultFetchGroup = "true")
    private String name;

    @Persistent(defaultFetchGroup = "true")
    private String surname;

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private Map<String, UserProfileSummary> connections;

    ...
}

以下是UserProfileSummary类:

The following is the UserProfileSummary class:

@PersistenceCapable(embeddedOnly = "true")
public class UserProfileSummary implements Serializable {

    private static final long serialVersionUID = 6120670018375572406L;

    @Persistent(defaultFetchGroup = "true")
    private String username;

    @Persistent(defaultFetchGroup = "true")
    private String fullName;
    ...
}

以下是我尝试加载类型为UserProfile的存储对象的方法:

The following is how I am trying to load stored objects of type UserProfile:

PersistenceManager pm = MyPersistenceManagerFactory.get().getPersistenceManager();

UserProfile profile = null;
try {
    profile = pm.getObjectById(UserProfile.class, username);
}
catch (JDOObjectNotFoundException onfe) {
    // handle
}
catch (Exception e) {
    // handle
}

问题是UserProfileconnections字段有时会正确加载,但有时会以与getObjectById()相同的调用作为null加载.该字段被标记为在defaultFetchGroup中,因此应每次都加载该字段.

The problem is that the connections field of UserProfile is sometimes loaded correctly, but sometimes loaded as null with the same call to getObjectById(). The field is marked to be in the defaultFetchGroup, so it should be loaded every time.

这可能是DataNucleus错误(竞赛条件吗?)?还有其他人遇到同样的问题吗?

注意:我知道DataNucleus 3.1.0版本可用,但是由于推荐答案

我已经关闭了DataNucleus level2缓存,从初步测试来看,这个问题似乎已经消失了.

I have turned DataNucleus level2 cache off and from preliminary tests it seems like this problem has disappeared.

要关闭二级缓存,请将以下行添加到jdoconfig.xml中:

To turn the level2 cache off, add the following line to your jdoconfig.xml:

<jdoconfig xmlns="http://java. ... .xsd">
    <persistence-manager-factory name="MyPMF">
        ...
        <property name="datanucleus.cache.level2.type" value="none" />
    </persistence-manager-factory>
</jdoconfig>

这篇关于与DataNucleus 3.x的1-N关系的不稳定加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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