cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Cocoon Wiki] Update of "CocoonAndHibernateTutorial" by JohannesTextor
Date Mon, 25 Jul 2005 15:46:48 GMT
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);
  }
  }}}
  

Mime
View raw message