-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Using Hibernate in a servlet engine
PostPosted: Thu Jun 23, 2005 5:26 pm 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Have anybody tried to use Hibernate in a web app the way it is described in the book?
1. Initialize SessionFactory in a HibernateUtil class static block (I'd rather initialize it in the init() method of application controller servlet and put it into the application context).
2. Open Hibernate session and store it in a static ThreadLocal variable of HibernateUtil class (I'd rather open it in a servlet filter and attach it to a request as an attribute).
3. Obtain an reference to session in Action execute() method.
4. Commit transaction and close session in servlet filter doFilter() method.

According to the author:
<quote>The job of the servlet filter is to close the Session before the response is sent to the client (and after all views are rendered and actions are executed).</guote>

I would really love to commit transaction and close session "after all views are rendered and actions are executed", if someone can confirm that it is feasible to invoke a filter after servets and JSPs.
Unfortunately I have always developed with J2EE1.2, which does not support filters. However, according to Servlet 2.3 Spec <quote>When the last filter in the chain has been invoked, the next entity accessed is the target servlet or resource at the end of the chain.</quote>.

Please can you clarify this issue? If it is not feasible to utilize filters to close session, what would be the best place to do so?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I'm surprised, because your questions are answered in the book. There is a even a filter example that closes the session after the view has been rendered, thats the whole point of the section. Maybe you should read the chapter again and have a look at CaveatEmptor source code.

If you put the Session in the request you can't access it in DAOs and have to pass it around. Works but isn't very pretty, a ThreadLocal is nicer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 9:18 pm 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Christian, thank you for your response.

I have read the chapter carefully and have seen the filter example. I have studied the CaveatEmptor source code as well.

Unfortunately, you cannot build an enterprise or web archive from the source code, and deploy and test the app on the server. I haven't even found web.xml file in the package.

I would never question the code in chapter 8, however there is one thing I don't understand. As I thought, the web container invokes all the filters first, and then passes control to the servlet, which invokes Action's execute() method and forwads the request to a JSP page, which then renders a view.

HibernateFilter should be invoked after that, however, according to the spec filters are invoked BEFORE servlets, not AFTER. Can you clarify this part for me?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 3:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"Unfortunately, you cannot build an enterprise or web archive from the source code, and deploy and test the app on the server. I haven't even found web.xml file in the package. "

This is vendor dependent. It's also what you are paid to do :)

"HibernateFilter should be invoked after that, however, according to the spec filters are invoked BEFORE servlets, not AFTER"

I wonder what is so difficult about this:

Code:
   public void doFilter(ServletRequest request,
                   ServletResponse response,
                   FilterChain chain)
         throws IOException, ServletException {

      // There is actually no explicit "opening" of a Session, the
      // first call to HibernateUtil.beginTransaction() in control
      // logic (e.g. use case controller/event handler) will get
      // a fresh Session.
      try {
         chain.doFilter(request, response);

         // Commit any pending database transaction.
         HibernateUtil.commitTransaction();

      } finally {

         // No matter what happens, close the Session.
         HibernateUtil.closeSession();

      }


The chain.doFilter() call executes the action and renders the JSP. Please read the documentation of Servlet filters again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 9:45 am 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Quote:
This is vendor dependent.

This is incorrect statement. web.xml shouldn't be vendor dependent. It should be Servlet spec dependent.

Quote:
I wonder what is so difficult about this


There is nothing difficult about it.

The problem is, doFilter() method will NEVER be invoked AFTER servlets and JSP pages finish their work. Has this piece ever been tested?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 9:48 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ok, since you will soon start to insult me its better if we stop this conversation. Go to the regular user forum and hope that somebody copy/pastes the documentation for you. CaveatEmptor does not include a presentation layer (as per README), so no web.xml. Servlet filters do not work the way you think they work. Yes, I think a few thousand people use the filters and the code from the book.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 10:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
To be clear, shrek, your understanding of servlet filters is broken. You need to understand them first, before trying to argue with us.

A servlet filter is an around style interceptor, if that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 11:08 am 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Quote:
This is vendor dependent.

This is incorrect statement. web.xml shouldn't be vendor dependent. It should be Servlet spec dependent.

Quote:
I wonder what is so difficult about this


There is nothing difficult about it.

The problem is, doFilter() method will NEVER be invoked AFTER servlets and JSP pages finish their work. Has this piece ever been tested?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 11:28 am 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Thank you guys.

I am going to study filters thoroughly now.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 2:35 pm 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
I've checked the Intercepting Filter pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html) in the Core J2EE Pattern Catalog.

The solution section states:
Quote:
When a client requests a resource that matches this configured URL mapping, the filters in the chain are each processed in order before the requested target resource is invoked.


BEFORE, not AFTER.

If you look at the diagrams, you can see container invoking filters and then forwarding to the Controller (or Target). There is no filter invokation afterwards.

Please expain or post a link how is it going to work?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 5:56 pm 
Newbie

Joined: Sun Jun 12, 2005 10:55 pm
Posts: 7
Sorry guys and thank you for your patience, I finally got it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.