Persistence units encapsulate the object-relational mapping to the database. This section is about how they encounter transactions, concurrency, and multiple requests.
Here are some helpful rules about
EntityManageras a dependency in a Java EE application. Therefore, the application server takes care of its lifecycle and the responsibility to close the persistence unit. Use
EntityManagerinstance is not thread-safe.
EntityManagerinstance manually retrieved from
EntityManagerFactorymust be closed in a retrieval method request for that particular state lest it goes out of scope, escapes, and ...