与DataNucleus 3.x的1-N关系的不稳定加载 [英] Flaky Loading of 1-N Relationships with DataNucleus 3.x
问题描述
我正在使用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
}
问题是UserProfile
的connections
字段有时会正确加载,但有时会以与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错误(竞赛条件吗?)?还有其他人遇到同样的问题吗?