OpenSessionInViewFilter v.s. OpenSessionInViewInterceptor

July 31, 2008 at 7:24 am | Posted in Uncategorized | 2 Comments
Tags: ,

Spring provides two implementation to achieve open session in view pattern.

OpenSessionInViewFilter
OpenSessionInViewInterceptor

Both of them will keep hibernate session open until view is rendered. However, even though both provides same function, they are implemented on fundamentally different techniques.

Interceptor:

  • use Spring MVC function
  • small granularity to apply
  • configure in applicationContext.xml, [servlet_name]-servlet.xml, or spring config file.

* use DispatcherServlet (Spring MVC) interceptor function to keep opening hibernate session.

* If you only need to open hibernate session for specific controllers, interceptor is a best choice because it can be configured to each controller(actually, handler mapping level).

e.g.) one controller uses hibernate, but another controller uses different technology.

Filter:

  • use ServletFilter function
  • large granularity to apply
  • configure in web.xml
  • Spring MVC is not required.

* servlet specification

Advertisements

2 Comments »

RSS feed for comments on this post. TrackBack URI

  1. To be honest, both are kind of scary in that you are handing a jdbc connection to the request for its full duration. This tends to greatly increase the size of the connection pool you need on each server :-/

  2. Brian, than you for your comment.
    Well, if that is the case, I don’t recommend to use open session in view pattern. Probably, better to fetch all required data in service or just simply don’t use lazy. Pass back objects to a view with all required data fully populated. It is still very possible approach, and I’ve worked a couple of projects doing this way without using open session in view. Hibernate provides Hibernate.initialize() method.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: