fyrkov
fyrkov

Reputation: 2715

Is JPA persistence context isolated between threads?

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?

Upvotes: 2

Views: 1022

Answers (1)

Thomas Kabassis
Thomas Kabassis

Reputation: 1356

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.

Upvotes: 2

Related Questions