Actually, the way I use it, I do not have the business logic components
tied to the weblayer. I have two classes. One class implements the
ThreadLocal pattern and is the class that my business components get
their sessions from. The other class, which extends the first,
implements javax.servlet.Filter. This class only takes care of closing
the session if there's one in the ThreadLocal.

E.g:

public class HibernateHelper
{
        protected static ThreadLocal hibernateHolder = new
ThreadLocal();
        protected static SessionFactory factory;
        private static boolean wasInitialised = false;

        public static void init() throws Exception
        {
                if (!wasInitialised)
                {
                        wasInitialised = true;
                        Configuration ds = new
Configuration().configure();
                        factory = ds.buildSessionFactory();
                }
        }

        public static Session getSession() throws HibernateException
        {
                Session sess = (Session)hibernateHolder.get();
                if (sess == null)
                {
                        sess = factory.openSession();
                        hibernateHolder.set(sess);
                }
                return sess;
        }

        public static SessionFactory getSessionFactory()
        {
                return factory;
        }

        public static void setSessionFactory(SessionFactory factory)
        {
                HibernateHelper.factory = factory;
        }
}

public class HibernateFilter extends HibernateHelper implements Filter
{
        public void init(FilterConfig filterConfig) throws
ServletException
        {
                try
                {
                        super.init();
                }
                catch (Exception e)
                {
                        throw new ServletException(e);
                }
        }

        public void doFilter(ServletRequest request, 
                ServletResponse response, FilterChain chain)
                throws IOException, ServletException
        {
                if (hibernateHolder.get() != null)
                        throw new IllegalStateException(
                                "A session is already associated with
this thread!  "
                                        + "Someone must have called
getSession() outside of the context "
                                        + "of a servlet request.");

                try
                {
                        chain.doFilter(request, response);
                }
                finally
                {
                        Session sess = (Session)hibernateHolder.get();
                        if (sess != null)
                        {
                                hibernateHolder.set(null);
                                try
                                {
                                        sess.close();
                                }
                                catch (HibernateException ex)
                                {
                                        ex.printStackTrace();
                                        throw new ServletException(ex);
                                }
                        }
                }
        }
}

Now, if you use:

Session session = HibernateHelper.getSession();

in your bussiness components, they will not have a dependency on the
weblayer. If not in a weblayer (like in a unit test), you just have to
make sure that HibernateHelper is initialized once, and that your
sessions are closed in some other way (like in the Junit tearDown()
method.

Eelco


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Matthew E. Porter
Sent: woensdag 6 augustus 2003 15:59
To: Joseph Fifield
Cc: [EMAIL PROTECTED]
Subject: Re: [Hibernate] Unit Testing With ThreadLocal Sessions


My only concern with this is that the business logic component is now  
coupled with the web tier.  For this project (which is basically a  
framework), we want to be able to use the components internal and  
external to the servlet container.  However, I do appreciate the  
feedback!  It helps confirm my thinking regarding potential solutions.


Cheers,
   matthew

On Wednesday, August 6, 2003, at 07:29 AM, Joseph Fifield wrote:

> I don't close the session at all in any of my business logic methods.
> In
> deployment, a servlet filter _always_ closes the session at the end of

> the
> request. For unit testing, I have a base test case class that does the

> same
> thing in the tearDown() method. That way, each test case can execute  
> in the
> same manner as the app running in deployment (i.e. each test case is  
> like a
> separate "request"). The result is each test case executes using the  
> same
> session, and they don't need to worry about closing it at the end.
>
> Hope this helps...
>
> Joe
>
> ----- Original Message -----
> From: "Matthew E.Porter" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, August 05, 2003 11:12 PM
> Subject: [Hibernate] Unit Testing With ThreadLocal Sessions
>
>
>> Greetings.  I am interested in hearing how people unit test method
>> that
>> call session.save(), update(), and delete() when the session is
>> provided via the ThreadLocal pattern.  For me, the primary question
is
>> whether or not the method containing the session method calls
actually
>> closes the database connection (session.close()) or is it called
after
>> returning from the tested method.  From a pure unit testing
>> stand-point, it would seem that it would be desired to be called  
>> inside
>> the tested method.
>>
>> For a project I am working on, we devised a solution (aka hack) to 
>> solve this.  A new Session interface implementation was created that 
>> wraps an internal Session object as normally provided.  Yet, we added
>> 2
>> methods: suspendClose() and unsuspendClose().  When closes are  
>> suspend,
>> the connection is NOT closed despite being called.  Specifically, it
>> does not call session.close() on the internal Session object.
>>
>> When a Session is retrieved, the closes are automatically suspended. 
>> The servlet filter unsuspends the close and closes the session if 
>> necessary at the end of the request.
>>
>> Is this a hack?  Yes.  Does it work?  Yes.  Do we get the benefits of
>> a
>> ThreadLocal session?  Yes.  Can it be confusing to newbies who expect
>> close to actual close the session?  Yes, but it only has this
behavior
>> during deployment.
>>
>> Any thoughts and comments are welcome.
>>
>>
>> Cheers,
>>    matthew
>>
>>
>>
>> -------------------------------------------------------
>> This SF.Net email sponsored by: Free pre-built ASP.NET sites 
>> including Data Reports, E-commerce, Portals, and Forums are available

>> now. Download today and enter to win an XBOX or Visual Studio .NET.
>>
> http://aspnet.click-url.com/go/psa00100003ave/
> direct;at.aspnet_072303_01/01
>> _______________________________________________
>> hibernate-devel mailing list [EMAIL PROTECTED]
>> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
>>
>>
>
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/ 
> direct;at.aspnet_072303_01/01
> _______________________________________________
> hibernate-devel mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to