Code example for WebRequest

Methods: getAttribute, setAttribute

0
 * {@code Session} for the processing of an entire request. In particular, the 
 * reassociation of persistent objects with a Hibernate {@code Session} has to 
 * occur at the very beginning of request processing, to avoid clashes with already 
 * loaded instances of the same objects. 
 * 
 * @author Juergen Hoeller 
 * @since 3.1 
 * @see OpenSessionInViewFilter 
 * @see OpenSessionInterceptor 
 * @see org.springframework.orm.hibernate4.HibernateTransactionManager 
 * @see org.springframework.transaction.support.TransactionSynchronizationManager 
 * @see org.hibernate.SessionFactory#getCurrentSession() 
 */ 
public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor { 
 
	/** 
	 * Suffix that gets appended to the {@code SessionFactory} 
	 * {@code toString()} representation for the "participate in existing 
	 * session handling" request attribute. 
	 * @see #getParticipateAttributeName 
	 */ 
	public static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; 
 
	protected final Log logger = LogFactory.getLog(getClass()); 
 
	private SessionFactory sessionFactory; 
 
 
	/** 
	 * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions. 
	 */ 
	public void setSessionFactory(SessionFactory sessionFactory) { 
		this.sessionFactory = sessionFactory; 
	} 
 
	/** 
	 * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions. 
	 */ 
	public SessionFactory getSessionFactory() { 
		return this.sessionFactory; 
	} 
 
 
	/** 
	 * Open a new Hibernate {@code Session} according and bind it to the thread via the 
	 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}. 
	 */ 
	@Override 
	public void preHandle(WebRequest request) throws DataAccessException { 
		String participateAttributeName = getParticipateAttributeName(); 
 
		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); 
		if (asyncManager.hasConcurrentResult()) { 
			if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) { 
				return; 
			} 
		} 
 
		if (TransactionSynchronizationManager.hasResource(getSessionFactory())) { 
			// Do not modify the Session: just mark the request accordingly. 
			Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 
			int newCount = (count != null ? count + 1 : 1); 
			request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST); 
		} 
		else { 
			logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor"); 
			Session session = openSession(); 
			SessionHolder sessionHolder = new SessionHolder(session);