@PersistenceContext EntityManager потокобезопасность в Spring и Java EE

EntityManager являетсяnot thread-safe по определению. Спецификация сервлетов говорит, что в нераспределенной среде и без реализацииSingleThreadModel, естьonly one servlet instance per definition.

Поэтому в Java EE, когда вы вводитеEntityManager сквозь@PersistenceContext в поле сервлета - это не потокобезопасно:

<code>public class MyServlet extends HttpServlet {

    // Not thread-safe, should be using EMF instead.
    @PersistenceContext
    private EntityManager em;
}
</code>

Is this correct to say that even though the default scope of Spring beans is singleton, the EntityManager is thread-safe as the Spring uses ThreadLocal to bind its transaction and EntityManager to it?

Is the above Servlets example still valid in Spring? Is it still not thread-safe?

Does the ThreadLocal approach works only for Spring managed beans and plain servlet is not one of those?

As far as I remember, it's the container responsibility to inject the EntityManager. In Glassfish Java EE implementation, it was the application server who discovers the @PersistenceContext as injection point.
How does it look like in Spring? Is the Spring Framework responsible for discovering those annotations or it's responsibility of the JPA implementor?

Ответы на вопрос(1)

Ваш ответ на вопрос