-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate and Weblogic
PostPosted: Wed Mar 17, 2004 9:02 pm 
Newbie

Joined: Mon Mar 01, 2004 9:49 pm
Posts: 13
Hi,
I have couple of Questions using ThreadLocal in J2EE server deployment.

1. Whats the recommended way of managing hibernate session, I mean, is it OK to use ThreadLocal to get the session object.

2. I am currently using ThreadLocal to get my Session and having the following error.

Code giving the session

public static Session currentSession() throws DaoException {
Session s = (Session) threadLocal.get();
try {
if(s == null) {
if (sf == null) {
try {
loadClasses();
sf = cfg.buildSessionFactory();
}
catch(Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
s = sf.openSession();
threadLocal.set(s);
}
}
catch (HibernateException he) {
throw new DaoException(he);
}
return s;
}

my DAO has methods like this:

public Location getLocationByPk(String locId) throws DaoException {
Location loc = null;
Session sess = getSession();
try {
loc = (Location) sess.get(Location.class, locId);
return loc;
} catch (HibernateException e) {
throw new DaoException("Error Creating Query: getLocationByPk() " + e.getMessage());
} finally {
closeSession(sess);
}
}

public State getStateByPk(String stateId) throws DaoException {
Session sess = getSession();
State loc = null;
try {
loc = (State) sess.get(State.class, stateId);
return loc;
} catch (HibernateException e) {
throw new DaoException("Error Creating Query: getStateByPK() " + e.getMessage());
} finally {
closeSession(sess);
}
}


public static void main(String[] args) {
try {
Location temp = dao.getLocationByPk("HQ");
State st = dao.getStateByPk("NV");
temp.setStateId("NV");
dao.updateLocation(temp);
} catch (DaoException e) {
e.printStackTrace();
}
}

Giving the error after executing the first getLocationByPK();
net.sf.hibernate.HibernateException: Session is closed
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3250)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:704)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:831)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:851)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2081)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1955)
at net.sf.hibernate.impl.SessionImpl.get(SessionImpl.java:1891)

My question is:
1.Do I need to close the session at all in the methods..?
2. Is it a problem thats arising because of ThreadLocal..?


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 6:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you use this implem of TLS you should not close the session inside sub methods (only the first one)
To prevent that you can use a counter inside the TLS to implement fake close and keep your code coherent.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.