In one of the application ,I saw Entity Manager is created 'per user HttpSession' and EntityManagerFactory is created only once.
Spring or EJB is NOT used in application. Entity manager is cached in http session and closed when session invalidated.
public EntityManager getEntityManager() {
//Logic to get entity manger from session
//If its present , then return
//Else create new one , entityManagerFactory.createEntityManager();
//Put it into session and then return.
//Returned entity manager is not closed by dao methods ,
//but clear() is invoked
}
If you don't close it your entities will be kept as attached, even after you're done using them. Your context will be kept alive even when you can no longer access your EM.
It depends how you obtained it. If you created it using EntityManagerFactory you will have to close it no matter what framework you use. If you obtained it using dependency injection (eg using EJB and @PersistenceContext annotation) you should not close it by hand (AFAIK it will cause RuntimeException).
getTransaction defines an EntityTransaction to use in your database interactions. Just like database transactions, you will typically begin the transaction, perform your operations, and then either commit or rollback your transaction.
Session is a hibernate-specific API, EntityManager is a standardized API for JPA. You can think of the EntityManager as an adapter class that wraps Session (you can even get the Session object from an EntityManager object via the getDelegate() function).
The answer to 2 and 3 is yes. As for Q1:
One problem you will have is that sessions typically last anywhere from 2 to 24 hours (or more) after they were last accessed. This means that your session object will try to sustain the EntityManager open and the EntityManager will try to keep the JDBC connection alive and exclusive to itself. Even with 50 users per hour, you'll already have loads of exceptions and Error 500 pages because of this.
I believe Serge Ballesta listed the other major problems this approach will cause.
A much safer solution is to have a static ThreadLocal<EntityManager>
singleton access and a javax.servlet.Filter
on all URLs with a try-finally statement that makes sure the EntityManager is properly closed on each request. Otherwise any exception that might occur will leave the connection dangling and cause additional problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With