我们正在尝试在启动时将一些实体加载到我们的类中。我们正在加载的实体(LocationGroup实体)与另一个实体(位置实体)具有@ManyToMany关系,由连接表(LOCATION_GROUP_MAP)定义。应该热切地想要这种关系。
这适用于单个线程,或者同步执行JPA查询的方法。但是,当有多个线程都异步执行JPA查询时(所有这些都是通过相同的Singleton DAO类),我们得到的位置收集数据应该是Eagerly获取的,在某些情况下保留为NULL。
我们在Glassfish v3.0.1中使用EclipseLink。
我们的数据库表(在Oracle DB中)如下所示:
LOCATION_GROUP
location_group_id | location_group_type
------------------+--------------------
GROUP_A | MY_GROUP_TYPE
GROUP_B | MY_GROUP_TYPE
LOCATION_GROUP_MAP
location_group_id | location_id
------------------+------------
GROUP_A | LOCATION_01
GROUP_A | LOCATION_02
GROUP_A | ...
GROUP_B | LOCATION_10
GROUP_B | LOCATION_11
GROUP_B | ...
LOCATION
location_id
-----------
LOCATION_01
LOCATION_02
...
我们的Java代码看起来像这样(我从实体中省略了getters / setters和hashCode,equals,toString - 实体是通过NetBeans从DB生成的,然后稍微修改,所以我不相信有任何问题和他们一起):
LocationGroup.java:
@Entity
@Table(name = "LOCATION_GROUP")
@NamedQueries({
@NamedQuery(name = "LocationGroup.findAll", query = "SELECT a FROM LocationGroup a"),
@NamedQuery(name = "LocationGroup.findByLocationGroupId", query = "SELECT a FROM LocationGroup a WHERE a.locationGroupId = :locationGroupId"),
@NamedQuery(name = "LocationGroup.findByLocationGroupType", query = "SELECT a FROM LocationGroup a WHERE a.locationGroupType = :locationGroupType")})
public class LocationGroup implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "LOCATION_GROUP_ID")
private String locationGroupId;
@Basic(optional = false)
@Column(name = "LOCATION_GROUP_TYPE")
private String locationGroupType;
@JoinTable(name = "LOCATION_GROUP_MAP",
joinColumns = { @JoinColumn(name = "LOCATION_GROUP_ID", referencedColumnName = "LOCATION_GROUP_ID")},
inverseJoinColumns = { @JoinColumn(name = "LOCATION_ID", referencedColumnName = "LOCATION_ID")})
@ManyToMany(fetch = FetchType.EAGER)
private Collection<Location> locationCollection;
public LocationGroup() {
}
public LocationGroup(String locationGroupId) {
this.locationGroupId = locationGroupId;
}
public LocationGroup(String locationGroupId, String locationGroupType) {
this.locationGroupId = locationGroupId;
this.locationGroupType = locationGroupType;
}
public enum LocationGroupType {
MY_GROUP_TYPE("MY_GROUP_TYPE");
private String locationGroupTypeString;
LocationGroupType(String value) {
this.locationGroupTypeString = value;
}
public String getLocationGroupTypeString() {
return this.locationGroupTypeString;
}
}
}
Location.java
@Entity
@Table(name = "LOCATION")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "LOCATION_ID")
private String locationId;
public Location() {
}
public Location(String locationId) {
this.locationId = locationId;
}
}
LocationGroupDaoLocal.java
@Local
public interface LocationGroupDaoLocal {
public List<LocationGroup> getLocationGroupList();
public List<LocationGroup> getLocationGroupList(LocationGroupType groupType);
}
LocationGroupDao.java
@Singleton
@LocalBean
@Startup
@Lock(READ)
public class LocationGroupDao implements LocationGroupDaoLocal {
@PersistenceUnit(unitName = "DataAccess-ejb")
protected EntityManagerFactory factory;
protected EntityManager entityManager;
@PostConstruct
public void setUp() {
entityManager = factory.createEntityManager();
entityManager.setFlushMode(FlushModeType.COMMIT);
}
@PreDestroy
public void shutdown() {
entityManager.close();
factory.close();
}
@Override
public List<LocationGroup> getLocationGroupList() {
TypedQuery query = entityManager.createNamedQuery("LocationGroup.findAll", LocationGroup.class);
return query.getResultList();
}
@Override
public List<LocationGroup> getLocationGroupList(LocationGroupType groupType) {
System.out.println("LOGGING-" + Thread.currentThread().getName() + ": Creating Query for groupType [" + groupType + "]");
TypedQuery query = entityManager.createNamedQuery("LocationGroup.findByLocationGroupType", LocationGroup.class);
query.setParameter("locationGroupType", groupType.getLocationGroupTypeString());
System.out.println("LOGGING-" + Thread.currentThread().getName() + ": About to Execute Query for groupType [" + groupType + "]");
List<LocationGroup> results = query.getResultList();
System.out.println("LOGGING-" + Thread.currentThread().getName() + ": Executed Query for groupType [" + groupType + "] and got [" + results.size() + "] results");
return results;
}
}
Manager.java
@Singleton
@Startup
@LocalBean
public class Manager {
@EJB private LocationGroupDaoLocal locationGroupDao;
@PostConstruct
public void startup() {
System.out.println("LOGGING: Starting!");
// Create all our threads
Collection<GroupThread> threads = new ArrayList<GroupThread>();
for (int i=0; i<20; i++) {
threads.add(new GroupThread());
}
// Start each thread
for (GroupThread thread : threads) {
thread.start();
}
}
private class GroupThread extends Thread {
@Override
public void run() {
System.out.println("LOGGING-" + this.getName() + ": Getting LocationGroups!");
List<LocationGroup> locationGroups = locationGroupDao.getLocationGroupList(LocationGroup.LocationGroupType.MY_GROUP_TYPE);
for (LocationGroup locationGroup : locationGroups) {
System.out.println("LOGGING-" + this.getName() + ": Group [" + locationGroup.getLocationGroupId() +
"], Found Locations: [" + locationGroup.getLocationCollection() + "]");
try {
for (Location loc : locationGroup.getLocationCollection()) {
System.out.println("LOGGING-" + this.getName() + ": Group [" + locationGroup.getLocationGroupId()
+ "], Found location [" + loc.getLocationId() + "]");
}
} catch (NullPointerException npe) {
System.out.println("LOGGING-" + this.getName() + ": Group [" + locationGroup.getLocationGroupId()
+ "], NullPointerException while looping over locations");
}
try {
System.out.println("LOGGING-" + this.getName() + ": Group [" + locationGroup.getLocationGroupId()
+ "], Found [" + locationGroup.getLocationCollection().size() + "] Locations");
} catch (NullPointerException npe) {
System.out.println("LOGGING-" + this.getName() + ": Group [" + locationGroup.getLocationGroupId()
+ "], NullPointerException while printing Size of location collection");
}
}
}
}
}
所以我们的经理启动,然后创建20个线程,所有这些线程同时调用Singleton LocationGroupDao,尝试加载类型为MY_GROUP_TYPE的LocationGroups。始终返回两个LocationGroup。但是,当返回LocationGroup实体时,LocationGroup上的Location集合(由@ManyToMany关系定义)有时为NULL。
如果我们使LocationGroupDao.getLocationGroupList(LocationGroupType groupType)方法同步一切正常(我们从未看到输出行指示发生NullPointerException),并且类似地,如果您将Manager.startup()中的for循环更改为仅具有单次迭代(因此只创建/执行一个Thread)。
但是,使用代码,我们确实得到了NullPointerException的输出行,例如(仅过滤掉其中一个线程的行):
LOGGING-Thread-172: Getting LocationGroups!
LOGGING-Thread-172: Creating Query for groupType [MY_GROUP_TYPE]
LOGGING-Thread-172: About to Execute Query for groupType [MY_GROUP_TYPE]
LOGGING-Thread-172: Executed Query for groupType [MY_GROUP_TYPE] and got [2] results
LOGGING-Thread-172: Group [GROUP_A], Found Locations: [null]
LOGGING-Thread-172: Group [GROUP_A], NullPointerException while looping over locations
LOGGING-Thread-172: Group [GROUP_A], NullPointerException while printing Size of location collection
LOGGING-Thread-172: Group [GROUP_B], Found Locations: [null]
LOGGING-Thread-172: Group [GROUP_B], NullPointerException while looping over locations
LOGGING-Thread-172: Group [GROUP_B], NullPointerException while printing Size of location collection
但是,在同一次运行期间稍后完成执行的线程没有NullPointerExceptions:
LOGGING-Thread-168: Getting LocationGroups!
LOGGING-Thread-168: Creating Query for groupType [MY_GROUP_TYPE]
LOGGING-Thread-168: About to Execute Query for groupType [MY_GROUP_TYPE]
LOGGING-Thread-168: Executed Query for groupType [MY_GROUP_TYPE] and got [2] results
LOGGING-Thread-168: Group [GROUP_A], Found Locations: [...]
LOGGING-Thread-168: Group [GROUP_A], Found location [LOCATION_01]
LOGGING-Thread-168: Group [GROUP_A], Found location [LOCATION_02]
LOGGING-Thread-168: Group [GROUP_A], Found location [LOCATION_03]
...
LOGGING-Thread-168: Group [GROUP_A], Found [8] Locations
LOGGING-Thread-168: Group [GROUP_B], Found Locations: [...]
LOGGING-Thread-168: Group [GROUP_B], Found location [LOCATION_10]
LOGGING-Thread-168: Group [GROUP_B], Found location [LOCATION_11]
LOGGING-Thread-168: Group [GROUP_B], Found location [LOCATION_12]
...
LOGGING-Thread-168: Group [GROUP_B], Found [11] Locations
肯定似乎是一个并发问题,但我不明白为什么返回LocationGroup实体,除非他们已经加载了所有Eagerly Fetched相关实体。
作为旁注,我也尝试过使用Lazy抓取 - 我遇到了类似的问题,执行的前几个线程显示Location集合未初始化,然后在某些时候它被初始化并且所有后续线程都工作如预期的那样。
答案 0 :(得分:0)
我不认为从多个线程访问单个应用程序管理的EntityManager
是有效的。
使其成为容器管理的事务范围:
@PersistenceContext(unitName = "DataAccess-ejb")
protected EntityManager entityManager;
或为每个帖子创建一个单独的EntityManager
(在getLocationGroupList()
内)。
编辑:默认情况下,EntityManager
不是线程安全的。唯一的例外是容器管理的事务范围EntityManager
,EntityManager
通过@PersistenceContext
注入而没有scope = EXTENDED
。在这种情况下,EntityManager
就像实际的线程本地EntityManager
的代理,因此它可以在多个线程中使用。
有关详细信息,请参阅JPA Specification的§3.3。