i'm using hibernate , have trouble in how use session, best method :
use 1 session , open in singleton
public static commonserviceimpl getinstance() { if (session == null || !session.isopen() ) { session = sessionfactory.opensession(); } //session.clear(); return instance; }
use
sessionfactory
each time when wanna use sessionsessionfactory.getcurrentsession()
?
the session object designed lightweight, disposable object can open , dispose on demand.. should not try reuse on own (like singleton)
real problem thread safety: while sessionfactory object thread safe, session object not , cause havoc when pointed several threads.
if don't want open new session every time can use current session option allows bind session specific context (like thread) within session factory , retrieve each time..
Comments
Post a Comment