• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple question about session

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at one example --

Query q = getSession().createQuery("from .. WHERE .."); //line 1
q.setString("..", ..) //line 2


If, immediately after line 1, the session is closed for whatever reason, will the variable "q" still be available in the code, i.e will line 2 still be executable and does "q" lose its value that is set in line 1 ?

Thanks.
 
Walter Bernstein
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You can't loose the session, it's a hibernate session.
2) No (but also see answer #1).

Read the hibernate reference documentation before you continue: http://www.hibernate.org/hib_docs/reference/en/html/index.html
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Walter Bernstein:
1) You can't loose the session, it's a hibernate session.
2) No (but also see answer #1).

Read the hibernate reference documentation before you continue: http://www.hibernate.org/hib_docs/reference/en/html/index.html


walter, thanks for the response. maybe I should explain it more clearly why I thought the session may be closed or lost. I know it is a hibernate session. In the code, this session is created as

***
private static final ThreadLocal session = new ThreadLocal();
****

And there is a "getSession()" to create open a session if session is null; there is a "close()" method to close the session. Since it is a static variable, that's why I think it can be closed by another client. And that's why I am concerned if line 2 of my code still holds the value of "Query".
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic