Injecting EntityManager Vs. EntityManagerFactory

前端 未结 4 1491
遇见更好的自我
遇见更好的自我 2020-12-02 06:59

A long question, please bear with me.

We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the

4条回答
  •  渐次进展
    2020-12-02 07:55

    I am putting down what I have finally gathered. From the section "Implementing DAOs based on plain JPA" in the Spring Reference:

    Although EntityManagerFactory instances are thread-safe, EntityManager instances are not. The injected JPA EntityManager behaves like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification. It delegates all calls to the current transactional EntityManager, if any; otherwise, it falls back to a newly created EntityManager per operation, in effect making its usage thread-safe.

    This means as per JPA specifications EntityManager instances are not thread safe, but if Spring handles them, they are made thread safe.

    If you are using Spring, it is better to inject EntityManagers instead of EntityManagerFactory.

提交回复
热议问题