• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple question about session

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
reply
    Bookmark Topic Watch Topic
  • New Topic