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

Reply via email to