Originally posted by Mark Spritzler:
The key there, and I thought I had helped you on this one in another thread (shrug), because you are using ThreadLocal, each Thread will be using a different Session than each other. So you cannot have a case where a seperate client will be able to close the session of another client. Each client will have their own Session it is dealing with.
Mark
Mark, I don't mean that I don't believe you. But I did a
test and found one client can close the session and after that this "session" var is null.
public static void close() {
Session s = (Session)DAO.session.get();
// check session status
if(s == null) System.out.println("session is null");
if ( s!= null && s.isOpen()) System.out.println("session is not null but is open");
if(s!= null && !s.isOpen()) System.out.println("session is not null and is closed");
if(s != null && s.isOpen()) {
try {
s.close();
} catch (HibernateException e) {
log.error("Database can not close", e);
}
}
DAO.session.set(null);
}
I have two sub DAOs extends this DAO. I open sessions for each of them. Then after the first clients calls its close() method, variable session changes to "null", so when the 2nd client calss "close" it prints "session is null" before it tries to close.
As I said in my another email thread (but nobody answered it), the original code doesn't make sense to me that it
1) first get the session
2. if session is null opens one
3. close the opened one.
I see no point to open another one and then immediately close it. so I modified it to the above snippet. but the above snippet yields the result I just decribed..