dwschulze (36) [Avatar] Offline
#1
On pg. 59 in section 2.5 Working with JPA you say "... shared, thread-safe EntityManager", but the JPA 2 EntityManager is not thread-safe.

From section 7.2 of the JPA 2 spec:

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

http://jcp.org/en/jsr/detail?id=317


The sample code for JpaContactDao does not indicate that the methods of that class are guaranteed to be accessed in a serialized manner. If Spring makes such a guarantee then your use of EntityManager is correct, but in the absence of such a guarantee it is not correct.

In any event the EntityManager class is not thread-safe and must be used in single-threaded code.