1 16 17 package org.springframework.orm.hibernate3.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 org.hibernate.FlushMode; 27 import org.hibernate.Session; 28 import org.hibernate.SessionFactory; 29 30 import org.springframework.dao.DataAccessResourceFailureException; 31 import org.springframework.orm.hibernate3.SessionFactoryUtils; 32 import org.springframework.orm.hibernate3.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 private FlushMode flushMode = FlushMode.NEVER; 104 105 106 111 public void setSessionFactoryBeanName(String sessionFactoryBeanName) { 112 this.sessionFactoryBeanName = sessionFactoryBeanName; 113 } 114 115 119 protected String getSessionFactoryBeanName() { 120 return this.sessionFactoryBeanName; 121 } 122 123 132 public void setSingleSession(boolean singleSession) { 133 this.singleSession = singleSession; 134 } 135 136 139 protected boolean isSingleSession() { 140 return this.singleSession; 141 } 142 143 public void setFlushMode(FlushMode flushMode) { 144 this.flushMode = flushMode; 145 } 146 147 protected FlushMode getFlushMode() { 148 return this.flushMode; 149 } 150 151 152 protected void doFilterInternal( 153 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 154 throws ServletException , IOException { 155 156 SessionFactory sessionFactory = lookupSessionFactory(request); 157 boolean participate = false; 158 159 if (isSingleSession()) { 160 if (TransactionSynchronizationManager.hasResource(sessionFactory)) { 162 participate = true; 164 } 165 else { 166 logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter"); 167 Session session = getSession(sessionFactory); 168 TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); 169 } 170 } 171 else { 172 if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) { 174 participate = true; 176 } 177 else { 178 SessionFactoryUtils.initDeferredClose(sessionFactory); 179 } 180 } 181 182 try { 183 filterChain.doFilter(request, response); 184 } 185 186 finally { 187 if (!participate) { 188 if (isSingleSession()) { 189 SessionHolder sessionHolder = 191 (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory); 192 logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter"); 193 closeSession(sessionHolder.getSession(), sessionFactory); 194 } 195 else { 196 SessionFactoryUtils.processDeferredClose(sessionFactory); 198 } 199 } 200 } 201 } 202 203 211 protected SessionFactory lookupSessionFactory(HttpServletRequest request) { 212 return lookupSessionFactory(); 213 } 214 215 222 protected SessionFactory lookupSessionFactory() { 223 if (logger.isDebugEnabled()) { 224 logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter"); 225 } 226 WebApplicationContext wac = 227 WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 228 return (SessionFactory) wac.getBean(getSessionFactoryBeanName(), SessionFactory.class); 229 } 230 231 245 protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { 246 Session session = SessionFactoryUtils.getSession(sessionFactory, true); 247 FlushMode flushMode = getFlushMode(); 248 if (flushMode != null) { 249 session.setFlushMode(flushMode); 250 } 251 return session; 252 } 253 254 264 protected void closeSession(Session session, SessionFactory sessionFactory) { 265 SessionFactoryUtils.closeSession(session); 266 } 267 268 } 269 | Popular Tags |