1 package org.hibernate.ce.auction.persistence; 2 3 import org.hibernate.Session; 4 import org.apache.commons.logging.*; 5 6 import javax.servlet.*; 7 import javax.servlet.http.*; 8 import java.io.IOException ; 9 10 20 public class HibernateFilterLong 21 implements Filter { 22 23 private static final String HTTPSESSIONKEY = "HibernateSession"; 24 private static Log log = LogFactory.getLog(HibernateFilterLong.class); 25 26 public void init(FilterConfig filterConfig) throws ServletException { 27 log.info("Servlet filter init, now disconnecting/reconnecting a Session for each request."); 28 } 29 30 public void doFilter(ServletRequest request, 31 ServletResponse response, 32 FilterChain chain) 33 throws IOException , ServletException { 34 35 HttpSession userSession = 37 ((HttpServletRequest) request).getSession(); 38 Session hibernateSession = 39 (Session) userSession.getAttribute(HTTPSESSIONKEY); 40 41 if (hibernateSession != null) 42 HibernateUtil.reconnect(hibernateSession); 43 44 try { 48 chain.doFilter(request, response); 49 50 HibernateUtil.commitTransaction(); 52 53 } finally { 54 56 hibernateSession = HibernateUtil.disconnectSession(); 58 userSession.setAttribute(HTTPSESSIONKEY, hibernateSession); 60 } 61 } 62 63 public void destroy() {} 64 65 } | Popular Tags |