I added some debug parameters, and you're right that the lookup happens each
time.  It sounds to me like the HibernateSession class could be enhanced to
allow only one lookup.  Here is a first stab at it:

public class HibernateSession {
    //~ Static fields/initializers
=============================================

    public static final ThreadLocal session = new ThreadLocal();
        private static SessionFactory sf = null;
        private static HibernateSession me;
    private static Log log = LogFactory.getLog(HibernateSession.class);

        static {
                try {
                        me = new HibernateSession();
                } catch (Exception e) {
                        log.fatal("Error occurred initializing
HibernateSession");
                        e.printStackTrace();
                }
        }
        
    //~ Methods
================================================================
        private HibernateSession() throws HibernateException, SQLException {

                // Try to lookup a JNDI Connection
                try {
                        if (log.isDebugEnabled()) {
                                log.debug("Looking up Session in JNDI");
                        }
                        sf = (SessionFactory) new
InitialContext().lookup(Constants.SESSION_FACTORY);
                } catch (NamingException ne) {
                        if (log.isDebugEnabled()) {
                                log.info("error communicating with JNDI,
assuming testcase");
                        }

                        Datastore datastore = Hibernate.createDatastore();
            
            datastore.storeClass(eg.User.class);
            // continue for each persistable class

                        sf = datastore.buildSessionFactory();
                }
        }
        
    public static Session currentSession()
      throws HibernateException, SQLException {
        Session s = (Session) session.get();
                
        if (s == null) {

            s = sf.openSession();

            if (log.isDebugEnabled()) {
                log.debug("Opened hibernate session.");
            }

            session.set(s);
        }

        return s;
    }

    public static void closeSession() throws HibernateException,
SQLException {
        Session s = (Session) session.get();
        session.set(null);

        if (s != null) {
            s.close();

            if (log.isDebugEnabled()) {
                log.debug("Closed hibernate session.");
            }
        }
    }
}

How does this sound?

Matt

> -----Original Message-----
> From: Aapo Laakkonen [mailto:[EMAIL PROTECTED]
> Sent: Thursday, January 30, 2003 8:06 AM
> To: [EMAIL PROTECTED]
> Subject: RE: [Hibernate] Obtaining session in a filter
> 
> 
> > I'm wondering if there's any problem with the
> > following modifications to Jeff's Filter.
> 
> Yes there is. More new InitialContext() lookups. This was one 
> thing that
> Jeff tried to avoid. But yes your approach should work fine, but it
> causes a little more overhead (Or have you done some modification to
> HibernateSession?).
> 
> 
> Regards
> Aapo
> 
> 
> 
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> http://www.vasoftware.com
> _______________________________________________
> hibernate-devel mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> 



-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to