-->
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.  [ 7 posts ] 
Author Message
 Post subject: Session is currently disconnected
PostPosted: Sat Feb 14, 2004 3:08 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 23
Hi,

I got few good suggestions from this forum on the consequences of building the session factory only once but we were building the factory multiple times. I'm in the process of building the facotry only once and get session from there.

I looked at couple of samples and both of them are building the factory initially in Filter and then get the session from filter and pass the session to DAO's. Our UI doesnt contain any database related but we are handling them in a separate package structure which contains our business logic and also DAO managers. Instead of building the factory and getting session from it in Filter, I created the factory during the context initialization by calling a separate class in our DAO package. Most of the time it works fine but occassionally I'm getting "Session is currently get disconnected". I'm little paranoid of this and not sure why it's happening.

Here is my usage:

1. HibernateSessionFactory.java:

a. Contains a static block that builds the sessionfactory
b. It also contains getsession and closesession.
c. a static variable of type ThreadLocal. Sessions will be put and removed from this ThreadLocal.


2. ServletContext:

a. During contextIntialized, I call the above class to prepare the session Factory. This is just to load the above class into JVM.
b. I'm not putting the sessionFactory in the context as it is too much work to pass the session from UI to our DAO's.


3. DAOManager:

a. Get the Session by calling HibernateSessionFactory.getSession.
b. do necessary stuff
c. in the finally block, calls HibernateSessionFactory.closeSession.


Ocassionally, I'm getting the Session is currently disconnected. I'm just concerned about this. Please advice.

Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 7:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Does it happen in such case
Code:
DAO1
  getSession();
  DAO2
     getSession(); //get the one in the threadlocal context
      close(); //real close instead of counter decrease
  use of session // get a disconnected connection.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 2:30 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 23
Hi,

Thanks for the response. Each request in our application spans many DAO's to do specific task. Yes, I do really close the session at the end of each DAO and also get new session at the begining of new DAO. What are the consequences if I dont use the ThreadLocal objest. How about if I just get the session and close it at the end of each DAO. I'm refactoring the code that is already in production and changing too much is too much for me as I dont want to break whole lot.

All my intention is to build the session factoy once during the server start up and get sessions from it. When u say counter decrease, do u mean something that is similar to this http://www.hibernate.org/42.html.

Thanks a bunch


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 4:34 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
You really don't want to be opening and closing sessions in each and every DAO.

Is there a reason you don't want to use the ThreadLocalSession pattern where you open a session when you get a servlet request and close it before sending the response? Opening (and closing) sessions in every DAO is a prescription for bad performance and for having troubles tracking your session lifetime.

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Opening and closing session in the DAO boundaries will work but is very inefficient (no caching / no transparent lazy loading...)
What you must not do in such a case is a DAO calling another DAO because you keep opened session in a threadlocal var. You will open the session once (the second open will only retrieve it from the threadlocal) and you will close it twice. I'm not sure you're in such a case, you did not give enough information.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 11:00 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 23
Hi,

To better understand the situation, here is the sample code.

DAO1:

Public void create(object a) throws Exception{

try{
new DAO2().doSomeStuff( );
Session s = HibernateSessionFactory.getSession();
s.save(a);
}catch(Excpetion e){
throw new Exception(e);
}
}finally{
HibernateSessionFactory.closeSession();
}
}

DAO2:

Public void doSomeStugg() throws Exception{

try{
Session s = HibernateSessionFactory.getSession();
// do something with sess
}catch(Excpetion e){
throw new Exception(e);
}
}finally{
HibernateSessionFactory.closeSession();
}
}


HibernateSessionFactory is initally called from the contextListener and builds the session factory. I'm not passsing session from web interface rather I'm directly getting them in the DAO's.


Do you see problems with this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 3:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show the HibernateSessionFactory code

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.