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

Moderator: admin

Post Reply
romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 9412
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 9412
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Well, you need to ask the author of the book why he wrote that when the specification says otherwise :)
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post 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();
}

admin
Site Admin
Posts: 9412
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest