I am currently working on an application using Guice / JPA / Hibernate to retrieve information from my database.
I read Guice docs about working with JPA and EntityManagars here: http://code.google.com/p/google-guice/wiki/JPA ,
But I had trouble understanding when I have to make my singleton for DAO implementation.
I read this question in S / O regarding the use of Spring DAO, where it says:
Creating a DAO instance for each request will be insane.
Does this carry over for DI containers other than Spring? If I insert a DAO provider in my servlet and call when necessary, should the implementation of the DAO service be Singleton?
Here is a basic outline of one of my DAOs:
public DAOImpl implements DAOService { <-- SHOULD THIS BE ANNOTATED @Singleton? @Inject private EntityManager em; // OR // @Inject // private Provider<EntityManager> emProvider - If it a singleton. @Inject DAOImpl(OtherServices os) { this.otherServices = os; } @Transactional public MyPersistedObject getPersistedObject(long id) { MyPersistedObject mpo = em.find(MyPersistedObject.class, id); return mpo; } }
And what is it called:
@Singleton public MyServlet(HttpRequest req, HttpRequest res) extends ServletInterfaceOfTheDay { private final daoService;
Thanks for the help.
source share