Hi Matt,
I know Christian just posted a mail that he uses a ThreadLocal variable to handle this, and that is one solution.
I use do it similar to how you describe it below, in that my web layer and my unit tests take care of opening the session and they pass it down to the persistance layer. I do this because within some requests I need to have multiple transactions running, so I wanted fine grained control over the opening and closing of sessions.
For 90% of the cases, the session that is opened by the ServletFilter will be the only session that I need, however there are enough instances where I need finer control that I decided to that passing the session to the persistance layer is the way to go.
I think you could easily combine the two approaches. The ServletFilter could be modified to initialize a ThreadLocal variable, and then you could use that to get the session that you pass to the persistance layer. You could also setup your JUnit test cases to initialize the same ThreadLocal variable with the session, so that the business logic would look that same for both cases. All that would change is an introduction of a servlet filter in your web tier, and the introduction of Session intialization code into your test cases.
Jon...
-----Original Message-----
From: Matt Raible [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 09, 2003 2:13 PM
To: Jon Lipsky; '[EMAIL PROTECTED]'; 'Raible,Matt'
Subject: Re: [Hibernate] Using Hibernate from a ServletFilter
Jon - this looks great. I'm assuming this works great when calling DAOs or Hibernate persistence classes directly from the web tier. If I have the following layers
web (action classes)
service (business logic)
persistence (daos)
And I'd like to use JUnit/Cactus to test them all. If I use your filter, I'm guessing I'll have to pass the Session object all the way down to the persistence layer? What is the recommended way to do this.
Furthermore, if I architect my app in this way, for my test cases, should I obtain a session in the TestCase and then pass that in to my methods?
All suggestions/recommendations welcome. You can see my example app (and how I'd doing it now) by downloading my struts-resume application from (http://www.raibledesigns.com/downloads).
I'd like to figure out the best way to do this, as this struts-resume app is going to end up in a book, and I'm assuming some folks might use it as an example to architect there apps.
Thanks,
Matt
> Hi All,
>
> I said in an email last week that I would post an example of how to
> use Hibernate from a ServletFilter.
>
> Hopefully this example works because I copied and pasted it together
> from the real one we are removing to remove some of the other
> dependencies that we have. If somone finds a problem with it, let me
> know, and I'll try to help you with it.
>
> I am attaching 3 files to this email:
>
> HibernateFilter.java - The source code for the ServletFilter
> SessionProxy - A proxy object that looks like a Hibernate session so
> that the session can be initialized when first used. web.xml - An
> example web.xml file that shows you how to configure the
> HibernateFilter
>
> The ServletFilter assumes that you are initializing the Hibernate
> datastore somewhere else, you could also modify the init() method of
> the HibernateFilter to initialize the datastore and session factories
> if you wanted. (That is actually what I do, but I removed that part
> to simplify the example.) On each request it will create a
> HibernateSession (or a proxy that looks like a session) and store it
> in a request attribute. At the end of the request, it will close it
> for you (in the case you use a proxy object, it will close the session
> only if you initialized the session.)
>
> Jon...
-------------------------------------------------------
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