<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Why does Axon.ivy issue a lot of warning message of "Entity manager will be closed now."</title><link>https://answers.axonivy.com/questions/1416/why-does-axon-ivy-issue-a-lot-of-warning-message-of-entity-manager-will-be-closed-now</link><description>&lt;p&gt;Ever since we upgrade into Axon.ivy 5.1.x, there are a lot of WARN in the log files:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;16:47:08 WARN [runtimelog.alag_bpmp.alag_integration.persistence] [pool-2-thread-302] [requestId=171088, request=, executionContext=SYSTEM] 
  Open entity manager 'org.hibernate.ejb.EntityManagerImpl@3442bb05' detected. Entity manager will be closed now.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Could you explain under what circumstance Axon.ivy write this WARN message? What does it mean?&lt;/p&gt;
&lt;p&gt;P.S: In our project, we use JPA &lt;code&gt;EntityManager&lt;/code&gt; to maintain transaction by obtaining it from &lt;code&gt;IIvyEntityManager&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1416/why-does-axon-ivy-issue-a-lot-of-warning-message-of-entity-manager-will-be-closed-now" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 29 Jul 2015 11:03:40 -0400</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/1416/why-does-axon-ivy-issue-a-lot-of-warning-message-of-entity-manager-will-be-closed-now/1419</link><description>&lt;p&gt;When you create a hibernate EntityManager you should also close its instance when the EntityManager is no longer used. This must be done as else every EntityManager will hold and keep a connection from the connection pool and this can lead to a leak.&lt;/p&gt;
&lt;p&gt;To prevent a connection leak the ivy core now closes all open EntityManager instances on HttpRequestEnd. If a not correctly closed instance is found, we log the warning you mentioned.&lt;/p&gt;
&lt;p&gt;To avoid the warning you should correctly close open EntityManager instances. Best practice is to handle EntityManager.close() in a finally block:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;private static List querySomething(){
    IIvyEntityManager ivyEntityManager = Ivy.persistence().get("myPersistencUnitName");
    EntityManager em = ivyEntityManager.createEntityManager();
    try{
        em.getTransaction().begin();
        List results = em.createQuery("SELECT XXX").getResultList();
        em.getTransaction().commit();
        return results;
    } finally {
        em.close();
    }
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Wed, 29 Jul 2015 11:03:40 -0400</pubDate><guid>https://answers.axonivy.com/questions/1416/why-does-axon-ivy-issue-a-lot-of-warning-message-of-entity-manager-will-be-closed-now/1419</guid></item></channel></rss>