I'm building a web application using Spring and hibernate. I decided to use the OpenSessionInView filter to achieve the behaviour of keeping the session opened for the duration of the request, until a response has been sent back to the browser (for lazy loading).

When I made a request to my program, it invokes the OpenSessionInView filter which binds a SessionFactory to a SessionHolder. I can see it by looking at the TransactionSychronizationManager.getResourceMap(). I believe that getHibernateTemplate and SessionFactoryUtil both rely on the TransactionSynchronzationManager to grab the associated SessionHolder by using the SessionFactory as the key.

The problem that i'm getting is that the SessionFactory that I'm getting by using DI (in my dao) has a different memory address than the one that is in the resource map of TransactionSychronizationManager (which was binded for me inside the OSIV filter).

Does anyone have any ideas why?

I further tested this by playing around with the following code:

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicati onContext(getServletContext());
SessionFactory sf = (SessionFactory) wac.getBean("sessionFactory");

SessionFactory sf2 = getSessionFactory(); // this is the Sessionfactory that is wired by my spring config

From my debug console, sf and sf2 both have different proxy address. However, they are both referring to the same sessionFactory bean in the same spring xml config file.. so I'm not sure why their addresses are different.

sf has the same memory location as the one that's in the resource map. Is there a reason why i'm getting a different memory address for sf2, when they are essentially pointing to the same bean? its just that one was thru the container, and the other was throught DI.

Does anyone have any ideas? Any help is muchly appreciated.

Thanks in advance.