chuck7 wrote:
The way I thought of would be to create a servlet that would instantiate a SessionFactory and drop it in to the application context. From there in my struts actions, I would get a reference to the application context via the request and session and the retreave the SessionFactory. This does not seem like the most elegant solution. It would also make it impossible for any support classes to get access to the SessionFactory without haveing it provided as an argument to the support class.
I read in the tutorial about the HibernateUtil ThreadLocal method, but I'm a little cloudy about how that works. In the class defined in the tutorial, the SessionFactory is created in a static block, and then other classes get sessions from it via static methods. Wouldn't this require that the SessionFactory be instantiated each time a call is made to one of the static methods? Or does the fact that the SessionFactory is instantiated in the static block mean that it stays resendent in memory for the entier time the JVM is running. For give me if this seems more like a "beginner to java" question more than a "beginner to hibernate" question.
Using the AppplicationContext is OK, but it has the diadvantage of having to pass the HttpServletRequest to everything that wants to use Hibernate, which conflicts with the whole MVC deal that Struts pushes.
The nice thing about ThreadLocal is that the SessionFactory gets created at VM start up, and is available from just about anywhere, without violating the MVC model (if you know what I mean...). It also eases your unit testing problems because it is much easier to simulate ThreadLocal than it is to synthesize a HttpServletRequest outside of the App server environment.