java - How to make DAO thread safe JPA? -
i new jpa , java. understood how entitymanager , emf works littel bit. have jpautill entitymanager, using threadlocal variable, makes entitymanager thread safe. have dao in persisting new users when having 10 thousand thread accesses simultaneously.
i see following exception:
javax.persistence.persistenceexception: org.hibernate.sessionexception: session closed! @ org.hibernate.ejb.abstractentitymanagerimpl.throwpersistenceexception(abstractentitymanagerimpl.java:614)
here dao code
try{ entitymanager = jpautil.getentitymanager(); user.setarea(getarea()); user.setcity(getcity()); user.setpassword("xxxxx"); user.setemail_id(getemail_id()); user.setmobile_num(tempmobile_num); user.setfirst_name(getfirst_name()); user.setsession_id("0"); user.setemail_verification_status("not verified"); user.setbalance(new bigdecimal(0.00)); if (!entitymanager.gettransaction().isactive()){ entitymanager.gettransaction().begin(); } entitymanager.persist(user); jpautil.commit(); } catch (exception e) { logger.info(e); e.printstacktrace(); } finally{ if(entitymanager.isopen()) jpautil.closeentitymanager(); }
jpautil:
public class jpautil { private static logger logger = logger.getlogger(jpautil.class); private static entitymanager entitymanager = null; private static final threadlocal<entitymanager> threadlocal; private static entitymanagerfactory emf = null; static { try { emf = persistence.createentitymanagerfactory("xyzdb"); threadlocal = new threadlocal<entitymanager>(); } catch (throwable ex) { logger.info("jpautil exception", ex); throw new exceptionininitializererror(ex); } } public static entitymanager getentitymanager() { try { entitymanager = threadlocal.get(); if (entitymanager == null || !entitymanager.isopen()) { jpautil.entitymanager = emf.createentitymanager(); threadlocal.set(entitymanager); } else if (!emf.isopen()) { entitymanager = threadlocal.get(); emf = persistence.createentitymanagerfactory("xyzdb"); jpautil.entitymanager = emf.createentitymanager(); threadlocal.set(entitymanager); } } catch (throwable ex) { logger.info("jpautil exception", ex); throw new exceptionininitializererror(ex); } return jpautil.entitymanager; } public static void closeentitymanager() { entitymanager entitymanager = threadlocal.get(); if (entitymanager != null) { entitymanager.close(); threadlocal.set(null); } } public static void closeentitymanagerfactory() { emf.close(); } public static void commit() { if (!entitymanager.gettransaction().isactive()) { entitymanager.gettransaction().begin(); } entitymanager.gettransaction().commit(); } public static void rollback() { if (!entitymanager.gettransaction().isactive()) { entitymanager.gettransaction().begin(); } entitymanager.gettransaction().rollback(); } }
can please me understand issue , how resolve it?
remove static
variable form jpautil:
private static entitymanager entitymanager = null;
you still using static
variable; didn't use threadlocal
.
change getentitymanager
this:
public static entitymanager getentitymanager() { entitymanager entitymanager; try { entitymanager = threadlocal.get(); if (entitymanager == null || !entitymanager.isopen()) { entitymanager = emf.createentitymanager(); threadlocal.set(entitymanager); } } catch (throwable ex) { logger.info("jpautil exception", ex); throw new exceptionininitializererror(ex); } return entitymanager; }
Comments
Post a Comment