• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Spring-Injected EntityManagers  RSS feed

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


In the Spring documentation (section 13.5.2) I see a DAO:




which is described as a thread-safe way to use a shared EntityManager. So I assume this means I could use this DAO as a singleton in a web container and not have to worry about different users grabbing the same EntityManager?
Just want to make sure. Just learning this stuff.

Thanks, Eric
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
Basically, spring by default nature is handing over singleton objects. Now what you are seeing is the instance of entity manager will be
provided via entity manager factory which you have already configured in the context. As these resources are pretty expensive, the responsibility
is of entity manager factory to give the manager when desired. Resource intensive things like entity manager factory implement singleton pattern
themselves, so always a singleton object will be handed over. Thread safety is a different thing, i guess some of the responsibility, programmer has
to cater themsevles also. I will strongly welcome some other opinions as well. Eric, there should be one entity manager for a whole complete web application,
so yes you should not worry about shared entity manager at all.
BR,
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. I can use this code for multiple transactions (all using the same DAO) on a single thread and somehow it knows to create a new EntityManager for each. The documentation seems very nonchalant about this but it is usually very good to warn about misuse of what it presents. I wonder if AOP is helping but I never specified any AOP in my application context.

Thanks, Eric
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!