Hibernate persist() does not update model in PersistentContext
<p>I am working with Hibernate 4.3.8 on a web app and It looks like that the <code>persist()</code> method does not update the PersistentContext (cache level 1). Here is my configuration and the singleton to manage the persistent operations:</p>

<p><strong>Hibernate configuration</strong></p>

<pre class="lang-xml prettyprint-override"><code>&lt;persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL"&gt;
    &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
    &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
        &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/&gt;
        &lt;property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/irm"/&gt;
        &lt;property name="hibernate.connection.username" value="nrossi"/&gt;
        &lt;property name="hibernate.connection.password" value="nicolas"/&gt;
        &lt;property name="hibernate.connection.pool_size" value="5"/&gt;
        &lt;property name="hibernate.show_sql" value="true"/&gt;
        &lt;property name="hibernate.format_sql" value="true"/&gt;
        &lt;property name="hibernate.max_fetch_depth" value="5"/&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
    &lt;/properties&gt;
&lt;/persistence-unit&gt;
</code></pre>

<p><strong>Persistence Manager</strong></p>

<pre class="lang-java prettyprint-override"><code>public class PersistenceManager
{
    private static final Logger log = LogManager.getLogger();
    private static final EntityManagerFactory emf;
    private static final ThreadLocal&lt;EntityManager&gt; threadLocal;

    static
    {
        emf = Persistence.createEntityManagerFactory("PersistenceUnit");
        threadLocal = new ThreadLocal&lt;EntityManager&gt;();
    }

    public static EntityManager getEntityManager()
    {
       EntityManager em = threadLocal.get();

       if (em == null)
       {
           em = emf.createEntityManager();
           threadLocal.set(em);
       }
       return em;
    }

    public static &lt;T&gt;T get(Class&lt;T&gt; clazz, Object id)
    {
       return getEntityManager().find(clazz, id);
    }

    public static void save(Object object)
    {
       EntityManager em = getEntityManager();
       EntityTransaction et = em.getTransaction();
       et.begin();
       try
       {
           em.persist(object);
           et.commit();
       }
       catch (Exception e)
       {
           et.rollback();
           throw new RuntimeException("Error saving object", e);
       }
   }
}
</code></pre>

<p>I update a model calling <code>PersistenceManager.save(model)</code> and it updates the record on the database, but after that when I call <code>PersistenceManager.get(model.id)</code> it returns the model from memory with the old values. It looks like the persist method is not updating the PersistenceCache. </p>

<p>By the way, If I call the <code>PersistenceManager.get(model.id)</code> on a new thread (i.e. incognito window) it returns the updated model.</p>

<p>I tried adding a refresh call <code>em.refresh(model)</code> after the commit and It is working, but I am not sure if this is the right way to get the context updated.</p>

<p><strong>Updated info</strong>
I coded a simple JSP page just to reproduce the behavior. If I update the entity description and wait 5' and refresh the page It returns the old value:</p>

<pre class="lang-jsp prettyprint-override"><code>&lt;%@page import="com.identicum.framework.persistence.PersistenceManager"%&gt;
&lt;%@page import="com.identicum.irm.core.model.Entity"%&gt;
&lt;%
    Entity entity = PersistenceManager.get(Entity.class, 1L);
    String value = request.getParameter("entityName");
    if(value != null)
    {
        entity.setDescription(value);
        PersistenceManager.save(entity);
    }
%&gt;
&lt;html&gt;
&lt;body&gt;

Entity description: &lt;b&gt;&lt;%= entity.getDescription() %&gt;&lt;/b&gt;

&lt;br&gt;
&lt;br&gt;

&lt;form method="post"&gt;

    Enter new entity description &lt;br&gt;
    &lt;input type="text" name="entityName"/&gt;

    &lt;input type="submit" /&gt;

&lt;/form&gt;


&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>** New information **</p>

<p>My PersistenceManager is a copy of this <a href="https://stackoverflow.com/questions/14888040/java-an-entitymanager-object-in-a-multithread-environment">suggestion</a>. My application is an Application-managed EntityManager. I have to access to the same EntityManager during the lifecycle of each request. That's the reason this approach. Is there any other way to implement this ?</p>