1 16 17 package org.springframework.orm.hibernate3.support; 18 19 import org.hibernate.HibernateException; 20 import org.hibernate.Session; 21 22 import org.springframework.dao.DataAccessException; 23 import org.springframework.orm.hibernate3.HibernateAccessor; 24 import org.springframework.orm.hibernate3.SessionFactoryUtils; 25 import org.springframework.orm.hibernate3.SessionHolder; 26 import org.springframework.transaction.support.TransactionSynchronizationManager; 27 import org.springframework.ui.ModelMap; 28 import org.springframework.web.context.request.WebRequest; 29 import org.springframework.web.context.request.WebRequestInterceptor; 30 31 82 public class OpenSessionInViewInterceptor extends HibernateAccessor implements WebRequestInterceptor { 83 84 89 public static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; 90 91 92 private boolean singleSession = true; 93 94 95 100 public OpenSessionInViewInterceptor() { 101 setFlushMode(FLUSH_NEVER); 102 } 103 104 113 public void setSingleSession(boolean singleSession) { 114 this.singleSession = singleSession; 115 } 116 117 120 protected boolean isSingleSession() { 121 return singleSession; 122 } 123 124 125 131 public void preHandle(WebRequest request) throws DataAccessException { 132 if ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) || 133 SessionFactoryUtils.isDeferredCloseActive(getSessionFactory())) { 134 String participateAttributeName = getParticipateAttributeName(); 136 Integer count = (Integer ) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 137 int newCount = (count != null) ? count.intValue() + 1 : 1; 138 request.setAttribute(getParticipateAttributeName(), new Integer (newCount), WebRequest.SCOPE_REQUEST); 139 } 140 else { 141 if (isSingleSession()) { 142 logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor"); 144 Session session = SessionFactoryUtils.getSession( 145 getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator()); 146 applyFlushMode(session, false); 147 TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session)); 148 } 149 else { 150 SessionFactoryUtils.initDeferredClose(getSessionFactory()); 152 } 153 } 154 } 155 156 163 public void postHandle(WebRequest request, ModelMap model) throws DataAccessException { 164 if (isSingleSession()) { 165 SessionHolder sessionHolder = 167 (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory()); 168 logger.debug("Flushing single Hibernate Session in OpenSessionInViewInterceptor"); 169 try { 170 flushIfNecessary(sessionHolder.getSession(), false); 171 } 172 catch (HibernateException ex) { 173 throw convertHibernateAccessException(ex); 174 } 175 } 176 } 177 178 184 public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { 185 String participateAttributeName = getParticipateAttributeName(); 186 Integer count = (Integer ) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 187 if (count != null) { 188 if (count.intValue() > 1) { 190 request.setAttribute(participateAttributeName, new Integer (count.intValue() - 1), WebRequest.SCOPE_REQUEST); 191 } 192 else { 193 request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 194 } 195 } 196 else { 197 if (isSingleSession()) { 198 SessionHolder sessionHolder = 200 (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); 201 logger.debug("Closing single Hibernate Session in OpenSessionInViewInterceptor"); 202 SessionFactoryUtils.closeSession(sessionHolder.getSession()); 203 } 204 else { 205 SessionFactoryUtils.processDeferredClose(getSessionFactory()); 207 } 208 } 209 } 210 211 217 protected String getParticipateAttributeName() { 218 return getSessionFactory().toString() + PARTICIPATE_SUFFIX; 219 } 220 221 } 222 | Popular Tags |