-->
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.  [ 5 posts ] 
Author Message
 Post subject: When to call .openSession() and .close()?
PostPosted: Tue Feb 24, 2004 5:52 am 
Newbie

Joined: Mon Feb 16, 2004 7:11 am
Posts: 13
Hi everybody!

I'm using Hibernate in an JBOSS-Webapplication, together with Struts.
My architecture is somewhat 4-tierd with JSP, Struts, Singletons for Business-Operations and Hibernate together with Oracle.

My question now is: How and when should I call the factory.openSession() and session.close() methods. The JDBC connections are managed through JBOSS' pool. I implemented and use the HibernateUtil from the reference with the ThreadLocal session-factory.

Currently I'm opening a session at the beginning of a struts action, while passing it along to the Business-Singletons. When I read chapter 17, I somehow get the feeling that this is wrong and I should rather open() and close() in every Business-operation itself.

What should I do? And what are the different consequences on thread-safeness, connection-count and speed in the two different approaches?!

Thanks for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 8:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I'd say the approach you have taken is quite okay, keeping a session open for the scope of a user request is a good solution. (Which is what you do if I understand you correctly).

You could even open and close the Session in a Servlet Filter, to reuse it across multiple Struts actions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 8:17 am 
Newbie

Joined: Mon Feb 16, 2004 7:11 am
Posts: 13
What would be the advantage if I reused the session over several requests?! (Resulting in that I'd have to save it in the session)

Also, what would happen if I openend and closed the session seperately in the bussiness-singleton method?! Would that result in one session for all users (cause after all it is a singleton) or would there still be a session for every user?!

Thanks for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 8:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Reusing the Session could probably give you better usage of the Session level cache. I would however stick to the per-request solution.

If you keep the session method-local in your singletons it would still be oe session per thread. If you assign it to some singleton class attribute, you will run into problems.

Just stick to the solution you have, its prefectly fine IMHO.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 1:50 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
here you have an example of filter which allow you to:
- attach one session per thread (so per request) and that clean the thread after
- reuse the session attached to the HttpSession, but this solution need more helper Classes to be ok
We use it in our framework and both work well.

package com.auchan.fwk.technique.utilitaire.mvc.dao.hibernate;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

/**
*
* Filtre de servlet qui g


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