I am learning JPA and have one question:
In which situations we need more than one EntityManager
in our application?
The two situations that I am aware of are as follows:
When our application is a multi-threaded application and more than one thread needs JPA transaction because
EntityManager
is not thread-safe and we need oneEntityManager
per thread.When any of the thread needs multiple concurrent transactions, we need more than one
EntityManager
in that thread because there is one-to-one relationship betweenEntityManager
andEntityTransaction
.
Q1. Are there any other situations when we need more than one EntityManager
?
=================================================================================
Q2. Upto my understanding, there should be only one EntityManagerFactory
per Persitence Unit. Am I correct? If not, then what are those situations when we need multiple EntityManagerFactory
per Persistence Unit?