I think this has already been well lit, but only in order to reinforce a few points.
DAO, if Spring is entered , is singleton by default . You must explicitly specify the scope of the prototype to create a new instance each time.
The entity organizer introduced by @PersistenceContext is thread safe .
However, I had some problems with the single point DAO in my multi-threaded application. I turned the DAO into an instanced bean and this solved the problem. So, although the documentation may say one thing, you probably want to test your application thoroughly.
Follow-up:
I think part of my problem is to use
@PersistenceContext(unitName = "unit", type = PersistenceContextType.EXTENDED)
If you are using PersistenceContextType.EXTENDED, keep in mind that you must, if I understand correctly, manually close the transaction. See this for more details.
Another observation:
Using an instanced DAO is a very bad idea. Each DAO instance will have its own persistent memory cache, and changes in one cache will not be recognized by other DAO beans. Sorry for the bad advice.
James McMahon Aug 21 '09 at 11:43 2009-08-21 11:43
source share