1 16 17 package org.springframework.orm.hibernate.support; 18 19 import java.io.IOException ; 20 21 import javax.servlet.FilterChain ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import net.sf.hibernate.FlushMode; 27 import net.sf.hibernate.Session; 28 import net.sf.hibernate.SessionFactory; 29 30 import org.springframework.dao.DataAccessResourceFailureException; 31 import org.springframework.orm.hibernate.SessionFactoryUtils; 32 import org.springframework.orm.hibernate.SessionHolder; 33 import org.springframework.transaction.support.TransactionSynchronizationManager; 34 import org.springframework.web.context.WebApplicationContext; 35 import org.springframework.web.context.support.WebApplicationContextUtils; 36 import org.springframework.web.filter.OncePerRequestFilter; 37 38 94 public class OpenSessionInViewFilter extends OncePerRequestFilter { 95 96 public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory"; 97 98 99 private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME; 100 101 private boolean singleSession = true; 102 103 104 109 public void setSessionFactoryBeanName(String sessionFactoryBeanName) { 110 this.sessionFactoryBeanName = sessionFactoryBeanName; 111 } 112 113 117 protected String getSessionFactoryBeanName() { 118 return this.sessionFactoryBeanName; 119 } 120 121 130 public void setSingleSession(boolean singleSession) { 131 this.singleSession = singleSession; 132 } 133 134 137 protected boolean isSingleSession() { 138 return this.singleSession; 139 } 140 141 142 protected void doFilterInternal( 143 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 144 throws ServletException , IOException { 145 146 SessionFactory sessionFactory = lookupSessionFactory(request); 147 boolean participate = false; 148 149 if (isSingleSession()) { 150 if (TransactionSynchronizationManager.hasResource(sessionFactory)) { 152 participate = true; 154 } 155 else { 156 logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter"); 157 Session session = getSession(sessionFactory); 158 TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); 159 } 160 } 161 else { 162 if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) { 164 participate = true; 166 } 167 else { 168 SessionFactoryUtils.initDeferredClose(sessionFactory); 169 } 170 } 171 172 try { 173 filterChain.doFilter(request, response); 174 } 175 176 finally { 177 if (!participate) { 178 if (isSingleSession()) { 179 SessionHolder sessionHolder = 181 (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory); 182 logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter"); 183 closeSession(sessionHolder.getSession(), sessionFactory); 184 } 185 else { 186 SessionFactoryUtils.processDeferredClose(sessionFactory); 188 } 189 } 190 } 191 } 192 193 201 protected SessionFactory lookupSessionFactory(HttpServletRequest request) { 202 return lookupSessionFactory(); 203 } 204 205 212 protected SessionFactory lookupSessionFactory() { 213 if (logger.isDebugEnabled()) { 214 logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter"); 215 } 216 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 217 return (SessionFactory) wac.getBean(getSessionFactoryBeanName(), SessionFactory.class); 218 } 219 220 233 protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { 234 return openSession(sessionFactory); 235 } 236 237 251 protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { 252 Session session = SessionFactoryUtils.getSession(sessionFactory, true); 253 session.setFlushMode(FlushMode.NEVER); 254 return session; 255 } 256 257 267 protected void closeSession(Session session, SessionFactory sessionFactory) { 268 SessionFactoryUtils.closeSession(session); 269 } 270 271 } 272 | Popular Tags |