3 Replies Latest reply: Jun 13, 2012 5:20 AM by gimbal2 RSS

    Concurrent access issue with JPA  for a Select query?

    java4ever
      Hi All,

      I am facing a very complex issue with JPA and have posted the details here.

      Concurrent access issue with JPA  for a Select query?


      Please feel free to suggest if you have come across this or have a better work around for this.


      Thanks
      VR
        • 1. Re: Concurrent access issue with JPA  for a Select query?
          gimbal2
          It isn't too productive to ask an OpenJPA question in a Toplink forum - even if they implement the same specification, they're totally different beasts. You'd better find an OpenJPA forum.

          In any case the one thing that I can spy from the tiny snippet of code is that you may be using the exact same EntityManager for each query invoked. You should make it such that a unique EntityManager is obtained and cleaned up within each call; in other words each thread will use a private EntityManager instance even when invoking the same method at the same time. That prevents any multithreading issues.
          • 2. Re: Concurrent access issue with JPA  for a Select query?
            java4ever
            First of all thanks for the prompt response :)

            Also, just adding to my original post
            //I create a EM Helper class which gets the EM for any invocations
            private static final ThreadLocal<EntityManager> threadLocal;
            
            //
            emf = Persistence.createEntityManagerFactory("PU");
            
            //Then i get the EM as follows
            manager = emf.createEntityManager();  // using ThreadLocal class to avoid unnecessary thread issues
            threadLocal.set(manager);
            I thought this should help and not lead to any thread issues...?


            Thanks
            VR
            • 3. Re: Concurrent access issue with JPA  for a Select query?
              gimbal2
              Well that's the way that Play framework does it. It seems totally unnecessary to do such a thing outside of framework code though; just use the factory directly and obtain an EntityManager when you need it.