wood burning stoves
The moose likes Object Relational Mapping and the fly likes what is the behaviour of OpenSessionInViewFilter in Spring ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "what is the behaviour of OpenSessionInViewFilter in Spring ?" Watch "what is the behaviour of OpenSessionInViewFilter in Spring ?" New topic

what is the behaviour of OpenSessionInViewFilter in Spring ?

Rao Potla

Joined: Jan 14, 2012
Posts: 11

Hi, can any one please explain the behaviour of OpenSessionInViewFilter with singleSession="true" and singleSession="false". Thanks in advance.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1682


Please UseCodeTags when posting code or configuration I have added them for you this time so you can see the difference in readability.

I am not a fan of OSIVF I commented on it here:

Load all the data you need when you are inside of a transaction in your service layer, that way it will be available when the transaction ends and the entity becomes detached. If you know you need the data than eagerly load it, and you wont have any lazy load exceptions. You don't need to make your mappings eager you can eagerly load data with a query if sometimes you want lazy loading and other times eager.

Your question is answered fairly well in the java doc I will quote that here:

WARNING: Applying this filter to existing logic can cause issues that have not appeared before, through the use of a single Hibernate Session for the processing of an entire request. In particular, the reassociation of persistent objects with a Hibernate Session has to occur at the very beginning of request processing, to avoid clashes with already loaded instances of the same objects.

Alternatively, turn this filter into deferred close mode, by specifying "singleSession"="false": It will not use a single session per request then, but rather let each data access operation or transaction use its own session (like without Open Session in View). Each of those sessions will be registered for deferred close, though, actually processed at request completion.

A single session per request allows for most efficient first-level caching, but can cause side effects, for example on saveOrUpdate or when continuing after a rolled-back transaction. The deferred close strategy is as safe as no Open Session in View in that respect, while still allowing for lazy loading in views (but not providing a first-level cache for the entire request).

Looks up the SessionFactory in Spring's root web application context. Supports a "sessionFactoryBeanName" filter init-param in web.xml; the default bean name is "sessionFactory". Looks up the SessionFactory on each request, to avoid initialization order issues (when using ContextLoaderServlet, the root application context will get initialized after this filter).

[How To Ask Questions][Read before you PM me]
I agree. Here's the link: http://aspose.com/file-tools
subject: what is the behaviour of OpenSessionInViewFilter in Spring ?
jQuery in Action, 3rd edition