Flaky Loading of 1-N Relationships with DataNucleus 3.x

丶灬走出姿态 提交于 2019-12-11 18:44:19

问题


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

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

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

The class that I am trying to load is as follows:

@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;

    ...
}

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;
    ...
}

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
}

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.

Could this be a DataNucleus bug (race condition?)? Is anyone else experiencing the same problem?

Note: I know that DataNucleus 3.1.0-release is available, but I can't use it because of a problem with its enhancer.


回答1:


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

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>


来源:https://stackoverflow.com/questions/11846170/flaky-loading-of-1-n-relationships-with-datanucleus-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!