On Aug 3, 2009, at 15:43 , Claudio Di Vita wrote:

Jean-Baptiste BRIAUD -- Novlog wrote:

Did you compared using == what you think is different instance of
EntityManager ?
Reading your message tend to prove that EntityManager is shared and
when you think you got a new one it is not a new one.


Why I have to check an instance returned by a threadlocal variable ??

Because TheadLocal used in certain way guarantee that a variable will be local to a thread, as its name impled, but doesn't guarantee that this instance will be different for each thread. The answer to your question is not in ThreadLocal class but inside the createEntityManager() method.


I use the following Th ThreadLocal<EntityManager>:

private static class ThreadLocalEntityManager extends
ThreadLocal<EntityManager> {

       /* (non-Javadoc)
        * @see java.lang.ThreadLocal#get()
        */
       @Override
       public EntityManager get() {

           /* Get the current entity manager */
           EntityManager em = super.get();

           /* The entity manager was closed */
           if (!em.isOpen()) {

               /* Create a new entity manager */
               em = factory.createEntityManager();

               /* Update the entity manager */
               set(em);
           }

           return em;
       }

       /* (non-Javadoc)
        * @see java.lang.ThreadLocal#initialValue()
        */
       @Override
       protected EntityManager initialValue() {

           return factory.createEntityManager();
       }
   }

Where factory is a static EntityManagerFactory.

What is going wrong ??

-----
Not everything that can be counted counts, and not everything that counts can
be counted - Albert Einstein
--
View this message in context: 
http://n2.nabble.com/Persist-issue-in-multithreaded-environment-tp3377510p3377741.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Reply via email to