-->
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.  [ 3 posts ] 
Author Message
 Post subject: Layered app problem with HibernateFilter
PostPosted: Sun Apr 03, 2005 1:57 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I have a problem where I get the following messages at times, I get the following messages:
* session is closed
* illegal attempt to assiciate a collection to two open sessions.

I wonder id this is a problem in my design or whether it occurs b/c of debugging mode. I also use Sitemesh.

I have no closeSession statements in the dao-layer and I use a HibernateFilter to close the session at the end of the request (open session in view pattern). A lot of the information I need to render in the view are collections of the user object, which is stored in the http session. Therefore I reassociate it with every request.

Hibernate version: 2.1.7

HibernateFilter.java:

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;

//reattach user object to Hibernate session to prevent lazy
// initialization errors
Object user0 = request.getSession().getAttribute("user");
User user = null;
if (user0 != null) {
user = (User) user0;
try {
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
session.lock(user, LockMode.NONE);
HibernateUtil.commit();
} catch (HibernateException he) {
logger.error(he);
}
}

try {

chain.doFilter(req, res);

} catch (ServletException e) {

logger.error("error handling filter chain: HibernateFilter "
, e);
throw e;

} catch (IOException io) {
logger.error("error handling filter chain: HibernateFilter "
, io);
throw io;
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
logger.error(he);
throw new ServletException(he);
}
}

}



HibernateUtil.java

private static final SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
logger.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}

public static void init(){}

public static Session getSession(){
Session s = (Session) threadSession.get();
try {
if(s == null){
s = sessionFactory.openSession();
threadSession.set(s);
}
if(!s.isOpen()){
s = sessionFactory.openSession();
}
} catch (HibernateException he) {
logger.error(he);
}
return s;
}

public static void closeSession() throws HibernateException{
try{
Session s = (Session) threadSession.get();
threadSession.set(null);
if(s != null && s.isOpen())s.close();
}catch(HibernateException e){
logger.error(e);
throw new HibernateException(e);
}
}



Thanks,

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 2:50 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
This is not thread safe code, try to synchronize filter or make http session "light weight".

Code:
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {

  synchronized(req.getSession()){

  //your stuff

  }


}


Top
 Profile  
 
 Post subject: using tiles or sitemesh: is this a problem using filter?
PostPosted: Wed Apr 06, 2005 3:07 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
I read on the hibernate site:
When to create and when to close a session depending on your framework (struts, spring, etc). First I was opening the session for each request call inside the Struts actions, but the session would close prematurely by the time the JSP is executed. Then I tried a Servlet Listener pattern as prescribed in the book Hibernate in Action (Christian Bauer, Gavin King - Manning). That would cause trouble as well because the session would remain open and Tiles insert would cause multiple session to open before others were closed.
My last resort was to extend Struts Actions servlet and wrap the processRequest() with the open/close call for the session and that seems to be very stable. (http://www.hibernate.org/113.html)

This seems to suggest that the hibernatefilter basically is not workable in many frameworks. Sitemesh and Tiles are pretty common. I'm a little surprised that such a crucial aspect of implementing Hibernate is clouded in uncertainty. To tell you the truth, I'm totally confused now, but I can't believe it could be this difficult. So, it must be me :-)

Can anybody enlighten me on the definitive way to use the open session in view in a layered application using sitemesh or tiles? Let's assume that objects will have lazy collection references and that those collections will be accessed in the jsp as well.

Thanks,

Marc


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