Return-Path: Delivered-To: apmail-cocoon-docs-archive@www.apache.org Received: (qmail 46481 invoked from network); 25 Jul 2005 15:47:25 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 25 Jul 2005 15:47:25 -0000 Received: (qmail 5851 invoked by uid 500); 25 Jul 2005 15:47:04 -0000 Delivered-To: apmail-cocoon-docs-archive@cocoon.apache.org Received: (qmail 5645 invoked by uid 500); 25 Jul 2005 15:47:02 -0000 Mailing-List: contact docs-help@cocoon.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: docs@cocoon.apache.org List-Id: Delivered-To: mailing list docs@cocoon.apache.org Received: (qmail 5561 invoked by uid 99); 25 Jul 2005 15:47:01 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [192.87.106.226] (HELO ajax.apache.org) (192.87.106.226) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2005 08:46:51 -0700 Received: from ajax.apache.org (ajax.apache.org [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id 346BC16 for ; Mon, 25 Jul 2005 17:46:48 +0200 (CEST) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: docs@cocoon.apache.org Date: Mon, 25 Jul 2005 15:46:48 -0000 Message-ID: <20050725154648.20166.94602@ajax.apache.org> Subject: [Cocoon Wiki] Update of "CocoonAndHibernateTutorial" by JohannesTextor X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Dear Wiki user, You have subscribed to a wiki page or wiki category on "Cocoon Wiki" for change notification. The following page has been changed by JohannesTextor: http://wiki.apache.org/cocoon/CocoonAndHibernateTutorial The comment on the change is: Improved the "OpenSessionInView"-like filter. ------------------------------------------------------------------------------ - COCOON-RELEASES: 2.1.5 2.1.6 2.1.7[[BR]] - DOCUMENT-STATUS: '''*draft*''' reviewed released[[BR]] ---- + + === Recent changes (as of 2005/07/25) === + + If you have visited this page before, you should check out the new Servlet Filter. It now + finally uses the request object instead of the servlet session to communicate with cocoon, + with is MUCH cleaner and most importantly does not require the creation of a Session ... + Sorry for taking so long to clean this up ! + + I am now using all of the code in a project with Hibernate 3. Probably the code snippets will + soon be updated to use the new Hibernate version by default. === What you will get from this page === @@ -395, +405 @@ === Creating the filter === - Our filter needs to communicate with cocoon. Unfortunately, this cannot be done via the Avalon framework. I could not + Our filter needs to communicate with cocoon. Unfortunately, this cannot be done via the Avalon framework. The most - come up with a better solution than abusing the servlet session context for this purpose. + straightforward way to get around this limitation is using the request object. - The example filter below will search the session context for an attribute called {{{DisposeHibernateSession}}}. If found, + The example filter below will search the current request for an attribute called {{{DisposeHibernateSession}}}. If found, it will try to cast the attribute value to a Hibernate Session, and close this session and the related JDBC connection if successful. @@ -442, +452 @@ // After cocoon has finished processing, close the // corresponding Hibernate session if it has been opened + if( request.getAttribute("DisposeHibernateSession") != null ) - if(((HttpServletRequest)request).getSession(false) != null && - ((HttpServletRequest)request).getSession().getAttribute("DisposeHibernateSession")!=null) { - Session hs = (Session) ((HttpServletRequest)request).getSession().getAttribute("DisposeHibernateSession"); + Session hs = (Session) request.getAttribute("DisposeHibernateSession"); try{ hs.flush(); hs.connection().close(); @@ -456, +465 @@ } catch( SQLException e ){ System.out.println(e.getMessage()); - } - ((HttpServletRequest)request).getSession().setAttribute("DisposeHibernateSession",null); + } + request.setAttribute("DisposeHibernateSession",null); } } } @@ -491, +500 @@ Since the filter only cares for disposing open sessions, you still have to open them yourself using the {{{PersistenceFactory}}} class designed above. Also you need to make sure that every - time a session is opened, it is correctly stored in the servlet session context such that the + time a session is opened, it is correctly stored in the servlet request such that the filter will later close it. It might be a good idea to create a @@ -520, +529 @@ cocoon.releaseComponent(factory); // Send "Message" to HibernateFilter to dispose the session after the view was rendered - cocoon.session.setAttribute("DisposeHibernateSession",hs); + cocoon.request.setAttribute("DisposeHibernateSession",hs); } }}}