Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potential Issues with creating EntityManager per HttpSession in web application

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
}
  1. What are potential problems with this design.
  2. What if 100K Users logged in into application simultaneously, will we run out of jdbc connections ?
  3. Does every entity manager has a separate JDBC connection associated with it?
like image 352
Rajendra Thorat Avatar asked Oct 01 '16 09:10

Rajendra Thorat


People also ask

What happens if EntityManager is not closed?

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.

Does EntityManager close connection?

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

Which method of EntityManager would begin the transaction perform your operations and then either commit or rollback your transaction in JPA?

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.

What is the difference between session and EntityManager?

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


1 Answers

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.

like image 74
coladict Avatar answered Oct 22 '22 22:10

coladict