Is JPA persistence context isolated between threads?

Viewed 417

I am aware of Hibernate 1st level cache.
I know that it is session-scoped and I expect a session to be bound to a thread.

Since JPA is only a specification and can be backed by other providers my question is
does JPA persistence context always give the same guarantee?

E.g. will changes made in one thread

        Object o = entityManager.find(Object.class, id);
        o.setAttr(..);
        // not flushed

ever be visible in another?

1 Answers

Within a persistence context, entities are managed and the EntityManager controls their lifecycle.

A persistence context is accessed through an EntityManager instance.

According to the documentation of Hibernate which is the most used JPA specification implementation (Chapter 5. Transactions and Concurrency ):

A EntityManagerFactory is an expensive-to-create, threadsafe object intended to be shared by all application threads. It is created once, usually on application startup.

An EntityManager is an inexpensive, non-threadsafe object that should be used once, for a single business process, a single unit of work, and then discarded.

Related