Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3487552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:09:46+00:00 2026-05-18T11:09:46+00:00

We are trying to load some entities into our classes on startup. The entities

  • 0

We are trying to load some entities into our classes on startup. The entities we are loading (LocationGroup entities) have an @ManyToMany relationship with another entity (Location entities), defined by a Join Table (LOCATION_GROUP_MAP). This relationship should be eagerly fetched.

This works fine with a single thread, or when the method performing the JPA query is synchronized. However, when there are multiple threads all performing the JPA query asynchronously (all via the same Singleton DAO class) we get the Location Collection data, which should be Eagerly fetched, being left NULL in some cases.

We are using EclipseLink in Glassfish v3.0.1.

Our database tables (in an Oracle DB) look like this:

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

And our Java code looks like this (I have omitted getters / setters and hashCode, equals, toString from Entities – the Entities were generated from the DB via NetBeans, then modified slightly, so I don’t believe there is any issue with them):

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");
                }
            }
        }
    }
}

So our manager starts up, and then creates 20 threads, all of which call into the Singleton LocationGroupDao concurrently, attempting to load the LocationGroups of type MY_GROUP_TYPE. Both LocationGroups are always returned. However, the Location collection on the LocationGroup (defined by the @ManyToMany relationship) is sometimes NULL when the LocationGroup entities are returned.

If we make the LocationGroupDao.getLocationGroupList(LocationGroupType groupType) method synchronized all is well (we never see the output lines indicating a NullPointerException occurred), and similarly if you change the for loop in Manager.startup() to only have a single iteration (so only one Thread is created / executed).

However, with the code as is, we do get the output lines with NullPointerException, eg (filtering out just the lines for one of the threads):

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

However, threads that complete execution later during the same run have no 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

Definitely appears to be a concurrency issue, but I don’t see why the LocationGroup entities are returned unless all their Eagerly Fetched related entities have been loaded.

As a side note, I have tried this with Lazy fetching too – I get a similar problem, the first few threads to execute show that the Location collection is uninitialized, and then at some point it becomes initialized and all later threads work as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:09:47+00:00Added an answer on May 18, 2026 at 11:09 am

    I don’t think it’s valid to access a single application-managed EntityManager from multiple threads.

    Either make it container-managed transaction-scoped:

    @PersistenceContext(unitName = "DataAccess-ejb") 
    protected EntityManager entityManager; 
    

    or create a separate EntityManager for each thread (inside getLocationGroupList()).

    EDIT: By default EntityManager is not thread-safe. The only exception is a container-managed transaction-scoped EntityManager, that is EntityManager injected via @PersistenceContext without scope = EXTENDED. In this case EntityManager acts like a proxy for the actual thread-local EntityManagers, therefore it can be used from multiple threads.

    For more information see §3.3 of JPA Specification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call a function after I load some XML into Actionscript, and
So I'm trying to load some returned html from an .aspx page but a
In the NodeMouseClick event I'm trying to load some objects from the Node.Tag. Before
In the code below I'm trying to load some images and put them in
I'm trying to understand some fundamental best practices using Entity Framework. My EDM design
I am trying to load Linq on my .Net 3.5 enabled web server by
I'm trying to load a page that is basically an edit form inside a
I am am trying to load a SQL table from a flat file. The
I'm trying to load fragments of XHTML markup using jQuery's $.fn.load function, but it
I am trying to load UIImage object from NSData , and the sample code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.