0

I have used annotation for using Entity Manager instead of using EntityManagerFactory.

@PersistenceContext
EntityManager entityManager;

I searched a lot regd the closing of entityManager. But in most places the EntityManager is being used

EntityManager em = EMF.get().createEntityManager();

And Im not sure how the closing varies for annotation based. Do we use it just like we use normal jdbc connection? Eg:

Connection conn=DataBaseConnection.getConnection();
PreparedStatement stmt;
ResultSet result;
stmt=conn.prepareStatement("SELECT * from table WHERE id = ?  ");
stmt.setString(1,id);
result=stmt.executeQuery();
conn.close();

So, do we need to add a begin tran to the entityManager, commit it and then close it for each method we use? or will the annotation take care of it all?

public class someClass{
    public someMethod1(){
     //use entityManager - do I need to close it for each method?
    }
    public someMethod2(){
     //use entityManager
    }
}

Or am I getting this entirely wrong? Please advice.

User-8017771
  • 1,512
  • 1
  • 11
  • 17
  • 2
    Take a look [here](https://stackoverflow.com/questions/10762974/should-jpa-entity-manager-be-closed). Might be useful. Note that in order to use the persistent context , you ll need to define a transaction manager. So , the closing / committing will take place while committing or failing a transaction, and not just per method. – AntJavaDev Sep 12 '17 at 06:25
  • @AntJavaDev Thanks for the response. Okay, I understand that I will need to define a transaction manager. But I was going through [this](https://docs.spring.io/spring/docs/2.5.x/reference/orm.html#orm-jpa-straight) and here please note that when `EntityManager em = this.emf.createEntityManager();` is used, `em.close();` em is closed. But if `@PersistenceContext private EntityManager em;` is used its not closed. Still confused.. – User-8017771 Sep 12 '17 at 06:46
  • Hmmm also in your example it defines a transaction manager in order to use the entityManager in that way. It actually depends on how globally you r going to wire them. f.e. for some cases you might need the exclusive closing but in some others when the entity manager is binded on a user session you dont want to manually close that... – AntJavaDev Sep 12 '17 at 09:15
  • @AntJavaDev hmm yes.. I do not have entity manager is binded on a user session. So, I have closed the entity manager for all the methods. But after some more digging I found that even though I close all the entity managers, the number of connections remains the same due to the default property - `spring.datasource.tomcat.initial-size= 10` I tried changing it in application properties file, but it doesnt get reflected.. :( So, for now I have left it as is. – User-8017771 Sep 13 '17 at 11:24
  • Ok thats why you should read as well about the overall process regarding datasource, managed connection pool and managed entity manager. In your case as the connection is pooled when you close it it simply returns back to the pool. The problem is that you are calling close on a bean that is not managed by you. If you want to manually close it then refer to `entityManagerFactory.openSession()` instead of `getSession()` – AntJavaDev Sep 13 '17 at 11:35
  • I exposed an api to check how many active connection present at a point of time. It seems like @Autowired EntityManager is not holding jdbc connection. – Ashish Doneriya Oct 03 '22 at 21:36

0 Answers0