When we need more than one EntityManager?

后端 未结 1 1938
感动是毒
感动是毒 2020-12-23 17:38

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 a

相关标签:
1条回答
  • 2020-12-23 18:26

    Q1: The EntityManager is best to be compared with the "good old" Hibernate Session: an unit of work (a simple business action, e.g. "logging in an user", "placing an order", etc). It is not necessarily tied to a single thread. You will only run in trouble if the different threads execute DB tasks which depends on each other inside a single unit of work. You would need to execute them in sync (preferably, in order in a single thread). If you for example have the business requirement to clean up some "old logs" when an user logs in (which reasonably wouldn't disturb each other's information), you can perfectly execute it in two separate threads inside a single unit of work.

    Q2: Your understanding is correct. You can however create more than one, but that wouldn't make any sense nor have any benefits. It would only add significant overhead.

    0 讨论(0)
提交回复
热议问题