Date: 2004-11-29T05:26:20
Editor: JohannesTextor <[EMAIL PROTECTED]>
Wiki: Cocoon Wiki
Page: CocoonAndHibernateTutorial
URL: http://wiki.apache.org/cocoon/CocoonAndHibernateTutorial
no comment
Change Log:
------------------------------------------------------------------------------
@@ -320,6 +320,58 @@
=== Creating the filter ===
+{{{
+package org.test;
+
+import java.io.*;
+import java.sql.SQLException;
+
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import net.sf.hibernate.Session;
+import net.sf.hibernate.HibernateException;
+
+public class HibernateFilter implements Filter {
+
+ private FilterConfig config = null;
+
+ public void init(FilterConfig config) throws ServletException {
+ this.config = config;
+ }
+
+ public void destroy() {
+ config = null;
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain) throws IOException, ServletException {
+ // Pass the request on to cocoon
+ chain.doFilter(request, response);
+
+ // After cocoon has finished processing, close the
+ // corresponding Hibernate session if it has been opened
+ if(((HttpServletRequest)request).getSession() != null &&
+
((HttpServletRequest)request).getSession().getAttribute("DisposeHibernateSession")!=null)
+ {
+ Session hs = (Session)
((HttpServletRequest)request).getSession().getAttribute("DisposeHibernateSession");
+ try{
+ hs.flush();
+ hs.connection().close();
+ hs.close();
+ }
+ catch( HibernateException e ){
+ System.out.println(e.getMessage());
+ }
+ catch( SQLException e ){
+ System.out.println(e.getMessage());
+ }
+
((HttpServletRequest)request).getSession().setAttribute("DisposeHibernateSession",null);
+ }
+ }
+}
+}}}
+
=== Installing the filter ===
In the file WEB-INF/web.xml, insert the following code avove the "Servlet
Configuration" part: (as always, insert