Hi,
 Few guidelines:
  1. stick to the principle of caching EntityManagerFactories by their
respective unit names. Do not cache EntityManagers. 

  2. Use a threading model that ensures a particular EntityManager
transaction is executed in a single thread. You need to tell us more about
your threading model for us to comment on how to do that reliably. 

  3. Decide the scope of EntityManagers. There are two scopes available:
TRANSACTION and EXTENDED. In a TRANSACTION scope, an EntityManager lives for
a transaction. In EXTENDED mode, an EntityManager lives for multiple
transactions. In former, you close the EntityManager after every transaction
commit in a finally block as you have done. In the later scope, you do not.
The appropriate scope is determined by your applications usage of data and I
can not provide a general guideline. 
   In a multi-threaded environment, with EXTENDED scope EntityManagers,
answer to (2) i.e. the affinity of thread and transactions becomes more
significant. 

  You may like to go through one way of using EXTENDED scope EntityManagers
in a multi-threaded environment in OpenBooks sample available with OpenJPA
distribution (see a class PersistenceService and its comments :). 

    

-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: 
http://openjpa.208410.n2.nabble.com/Multiple-concurrent-threads-attempted-to-access-a-single-broker-tp6820896p6824688.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to