-->
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.  [ 4 posts ] 
Author Message
 Post subject: the proper place to open and close sessions in web app
PostPosted: Sun Jan 25, 2009 6:34 am 
Newbie

Joined: Thu Mar 17, 2005 5:50 am
Posts: 12
Hello All,

I have a web project that use servlets struts hibernate DAOs

I want to know where is the proper place to handle the session opening and closing

servlets , or struts action or DAOS or where exactly

I use this hibernate session factory





Code:
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

            private static final ThreadLocal threadLocal = new ThreadLocal();
            private static final SessionFactory sessionFactory;


            public static Session getSession() throws HibernateException {
        Session session = (Session)threadLocal.get();
        if (session == null || !session.isOpen()) {
            session = sessionFactory.openSession();
            threadLocal.set(session);
                }
        return session;
            }

            public static Session getNewSession() throws HibernateException {
        Session session = sessionFactory.openSession();
        return session;
            }

            public static void closeSession(Session session) {
        clearSession(session);
        session.close();
            }

            public static void closeSession() {
        Session session = getSession();
        closeSession(session);
            }

            public static void clearSession(Session session) {
        session.flush();
        session.clear();
            }

            public static void clearSession() {
        Session session = getSession();
        clearSession(session);
            }

       

            static  {
        try {
            System.out.println("-------Create the SessionFactory from hibernate.cfg.xml-------From Source Code");
            sessionFactory = (new Configuration()).configure("model/hibernate/config/hibernate.cfg.xml").buildSessionFactory();
                }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
                }
            }
}



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 4:49 am 
Newbie

Joined: Thu Mar 17, 2005 5:50 am
Posts: 12
ok i will explain more

when i follow the logic of closing session where ever you open it using method
Code:
getSession()


i got exception
Code:
org.hibernate.SessionException: Session is closed


then i found that thread local is not accurate in getting the current thread session

then i converted the another hibernate session factory that is mentioned in this article
http://www.hibernate.org/207.html

but after that i got

Code:
org.hibernate.NonUniqueObjectException


can anybody suggest a solution

Thank you in advance


Top
 Profile  
 
 Post subject: OpenSessionInViewFilter
PostPosted: Mon Jan 26, 2009 4:51 pm 
Spring offers OpenSessionInViewFilter. If you are not using Spring at least you can get the idea from it.
Hope it helps,
Dragos


Top
  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 5:02 pm 
Here is a better answer: http://www.hibernate.org/43.html


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