java - Hibernate persist() does not update model in PersistentContext -


i working hibernate 4.3.8 on web app , looks persist() method not update persistentcontext (cache level 1). here configuration , singleton manage persistent operations:

hibernate configuration

<persistence-unit name="persistenceunit" transaction-type="resource_local">     <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider>     <properties>         <property name="hibernate.dialect" value="org.hibernate.dialect.postgresqldialect"/>         <property name="hibernate.connection.driver_class" value="org.postgresql.driver"/>         <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/irm"/>         <property name="hibernate.connection.username" value="nrossi"/>         <property name="hibernate.connection.password" value="nicolas"/>         <property name="hibernate.connection.pool_size" value="5"/>         <property name="hibernate.show_sql" value="true"/>         <property name="hibernate.format_sql" value="true"/>         <property name="hibernate.max_fetch_depth" value="5"/>         <property name="hibernate.hbm2ddl.auto" value="update"/>     </properties> </persistence-unit> 

persistence manager

public class persistencemanager {     private static final logger log = logmanager.getlogger();     private static final entitymanagerfactory emf;     private static final threadlocal<entitymanager> threadlocal;      static     {         emf = persistence.createentitymanagerfactory("persistenceunit");         threadlocal = new threadlocal<entitymanager>();     }      public static entitymanager getentitymanager()     {        entitymanager em = threadlocal.get();         if (em == null)        {            em = emf.createentitymanager();            threadlocal.set(em);        }        return em;     }      public static <t>t get(class<t> 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);        }    } } 

i update model calling persistencemanager.save(model) , updates record on database, after when call persistencemanager.get(model.id) returns model memory old values. looks persist method not updating persistencecache.

by way, if call persistencemanager.get(model.id) on new thread (i.e. incognito window) returns updated model.

i tried adding refresh call em.refresh(model) after commit , working, not sure if right way context updated.

updated info coded simple jsp page reproduce behavior. if update entity description , wait 5' , refresh page returns old value:

<%@page import="com.identicum.framework.persistence.persistencemanager"%> <%@page import="com.identicum.irm.core.model.entity"%> <%     entity entity = persistencemanager.get(entity.class, 1l);     string value = request.getparameter("entityname");     if(value != null)     {         entity.setdescription(value);         persistencemanager.save(entity);     } %> <html> <body>  entity description: <b><%= entity.getdescription() %></b>  <br> <br>  <form method="post">      enter new entity description <br>     <input type="text" name="entityname"/>      <input type="submit" />  </form>   </body> </html> 

** new information **

my persistencemanager copy of suggestion. application application-managed entitymanager. have access same entitymanager during lifecycle of each request. that's reason approach. there other way implement ?

first of persist() new entities (no record on database). if want change object must use merge().

but problem in multi threaded environment. cannot use threadlocal. each thread have own persistencemanager.

what app server or web container using?


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -