Page 1 of 1

About Question enthuware.oce-jpad.v6.2.593 :

Posted: Tue Sep 08, 2015 4:34 pm
by romsky
EntityManager injected by container is a proxy object. It will create separate real EntityManagers for separate threads(transactions).

Injection into servlet is similar to injection into singleton bean. There is no issue there with injection of EntityManager.

Please, provide links or quotes from specs to support other opinion.
Please be aware that, "EntityManager are NOT thread safe" is about application-managed EntityManager created by EntityManagerFactory.createEntityManager(), rather then injected container-managed one.

Re: About Question enthuware.oce-jpad.v6.2.593 :

Posted: Tue Sep 08, 2015 7:56 pm
by admin
Section 7.2 of JPA specification:
An entity manager must not be shared among multiple concurrently executing threads, as the entity
manager and persistence context are not required to be threadsafe. Entity managers must only be
accessed in a single-threaded manner.
It applies to all sorts of entity managers.
Section 4.8.5 of EJB 3.1 specification says,
It is legal to store Java EE objects that do not support concurrent access (e.g. Entity Managers, Stateful Session Bean references) within Singleton bean instance state. However, it is the responsibility of the Bean Developer to ensure such objects are not accessed by more than one thread at a time.
This quite clearly means that you cannot let EM be accessed by multiple threads even in a singleton bean.

Re: About Question enthuware.oce-jpad.v6.2.593 :

Posted: Wed Sep 09, 2015 5:33 am
by romsky
admin wrote: This quite clearly means that you cannot let EM be accessed by multiple threads even in a singleton bean.

Keith M., Schincariol M. - Pro JPA 2, 2nd edition, page 37:

By default, the container will manage the synchronization of the business methods to ensure that data corruption does not occur. That means all access to the bean is serialized so that only one client is invoking a business method on the instance at any time.

Re: About Question enthuware.oce-jpad.v6.2.593 :

Posted: Wed Sep 09, 2015 9:30 pm
by admin
Well, you need to ask the author of the book why he wrote that when the specification says otherwise :)

Re: About Question enthuware.oce-jpad.v6.2.593 :

Posted: Sun Nov 29, 2020 11:07 am
by johnlong
Well, you need to ask the author of the book why he wrote that when the specification says otherwise
Author of the book is one of the JPA specs authors.
However he does not say anything about access to Servlet instance field.
It is legal to store Java EE objects that do not support concurrent access (e.g. Entity Managers, Stateful Session Bean references) within Singleton bean instance state. However, it is the responsibility of the Bean Developer to ensure such objects are not accessed by more than one thread at a time.
Here the question is about Servlet, how do EJB specs apply ?

I believe the right (recommended) should be to inject instance of EntityManagerFactory.

Code: Select all

@PersistenceUnit
EntityManagerFactory emf;

public void goGet(HttpServletRequest req, HttpServletResponse res) {
	EntityManager em = emf.createEntityManager();
}

Re: About Question enthuware.oce-jpad.v6.2.593 :

Posted: Mon Nov 30, 2020 3:03 am
by admin
Your code example is irrelevant because it is creating a new EntityManager everytime any thread enters the method. So, there is no concurrent access here!