0

I am trying to use Hibernate with Struts. Will it be a good pratice if I extend the FilterDispacther for calling Hibernate Utility classes?

Anyone have any views? I want to discuss the pros and cons with this approach.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85

3 Answers3

1

It sounds to me like you want to have functionality similar to Spring's OpenSessionInViewInterceptor. If yes, the fact that another framework has already done such a thing suggests that it is a good practice.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • i don't know spring. but the problem is that the `FilterDispatcher` is deprecated. – Akhil K Nambiar May 29 '11 at 14:37
  • Why not just a Filter? What's the FilterDispatcher doing for you? – duffymo May 29 '11 at 14:39
  • `public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); try{ HibernateUtil.createSessionFactory(); System.out.print("========= Application Intialization ========"); } catch (HibernateException e) { throw new ServletException(); } }` FilterDispatcher is just openning a session. nothing else.. – Akhil K Nambiar May 29 '11 at 14:51
1

Don't extend FilterDispatcher.

As duffymo indicated, if you need to set up and tear down a Hibernate session for the current request, you should use an interceptor. You can use the OpenSessionInViewInterceptor that he linked too, or if you don't use Spring, you can create your own.

Community
  • 1
  • 1
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
0

The point is usually you don't want to map a filter to all your requests - For example you don't need a transaction for some pages - The api doc for FilterDispatcher says that

IMPORTANT: this filter must be mapped to all requests. Unless you know exactly what you are doing, always map to this URL pattern: /*

So FilterDispatcher is all or nothing. Which tells a old good Filter is a better choice for open session in view pattern.

Arash
  • 11,697
  • 14
  • 54
  • 81