persistence - JAVA: an EntityManager object in a multithread environment -
persistence - JAVA: an EntityManager object in a multithread environment -
if have multiple threads, each utilize injector entitymanager object, each utilize em object select list of other class objects. ready used in loop.
if thread finishes first , calls clear(), impact other threads? loop have exception?
how close()?
if reply "it depends", (class definition? method call?) , (java code? annotation? xml?) should @ find out how depended?
i did not write source, using else's library without documentation.
thank you.
here total working thread-safe entity manager helper
.
import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.persistence; public class entitymanagerhelper { private static final entitymanagerfactory emf; private static final threadlocal<entitymanager> threadlocal; static { emf = persistence.createentitymanagerfactory("persistent_name"); threadlocal = new threadlocal<entitymanager>(); } public static entitymanager getentitymanager() { entitymanager em = threadlocal.get(); if (em == null) { em = emf.createentitymanager(); // set flush mode here threadlocal.set(em); } homecoming em; } public static void closeentitymanager() { entitymanager em = threadlocal.get(); if (em != null) { em.close(); threadlocal.set(null); } } public static void closeentitymanagerfactory() { emf.close(); } public static void begintransaction() { getentitymanager().gettransaction().begin(); } public static void rollback() { getentitymanager().gettransaction().rollback(); } public static void commit() { getentitymanager().gettransaction().commit(); } }
java persistence entitymanager
Comments
Post a Comment