Java Code Examples for org.springframework.web.context.request.async.WebAsyncUtils#getAsyncManager()

The following examples show how to use org.springframework.web.context.request.async.WebAsyncUtils#getAsyncManager() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // gh-21972
public void responseBodyFluxWithError() throws Exception {

	this.request.addHeader("Accept", "text/event-stream");

	MethodParameter type = on(TestController.class).resolveReturnType(Flux.class, String.class);
	EmitterProcessor<String> processor = EmitterProcessor.create();
	this.handler.handleReturnValue(processor, type, this.mavContainer, this.webRequest);

	assertTrue(this.request.isAsyncStarted());

	IllegalStateException ex = new IllegalStateException("wah wah");
	processor.onError(ex);
	processor.onComplete();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	assertSame(ex, asyncManager.getConcurrentResult());
	assertNull(this.response.getContentType());
}
 
Example 2
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link 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);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 3
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 4
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link 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);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 5
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
@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 ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
		SessionFactoryUtils.isDeferredCloseActive(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 {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			SessionHolder sessionHolder = new SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
Example 6
Source File: OpenEntityManagerInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// Do not modify the EntityManager: 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 JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 7
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
@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 ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
			org.springframework.orm.hibernate3.SessionFactoryUtils.isDeferredCloseActive(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 {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			org.springframework.orm.hibernate3.SessionHolder sessionHolder = new org.springframework.orm.hibernate3.SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			org.springframework.orm.hibernate3.SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
Example 8
Source File: OpenEntityManagerInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applyEntityManagerBindingInterceptor(asyncManager, key)) {
		return;
	}

	EntityManagerFactory emf = obtainEntityManagerFactory();
	if (TransactionSynchronizationManager.hasResource(emf)) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(emf, emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
			asyncManager.registerCallableInterceptor(key, interceptor);
			asyncManager.registerDeferredResultInterceptor(key, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 9
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 10
Source File: OpenEntityManagerInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// Do not modify the EntityManager: 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 JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 11
Source File: OpenSessionInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applySessionBindingInterceptor(asyncManager, key)) {
		return;
	}

	if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, 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);
		TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(obtainSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(key, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(key, asyncRequestInterceptor);
	}
}
 
Example 12
Source File: OpenEntityManagerInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applyEntityManagerBindingInterceptor(asyncManager, key)) {
		return;
	}

	EntityManagerFactory emf = obtainEntityManagerFactory();
	if (TransactionSynchronizationManager.hasResource(emf)) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(emf, emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
			asyncManager.registerCallableInterceptor(key, interceptor);
			asyncManager.registerDeferredResultInterceptor(key, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 13
Source File: OpenSessionInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applySessionBindingInterceptor(asyncManager, key)) {
		return;
	}

	if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, 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);
		TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(obtainSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(key, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(key, asyncRequestInterceptor);
	}
}
 
Example 14
Source File: OpenEntityManagerInViewTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}
 
Example 15
Source File: OpenEntityManagerInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}
 
Example 16
Source File: OpenSessionInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenSessionInViewInterceptorAsyncScenario() throws Exception {
	// Initial request thread

	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
	interceptor.setSessionFactory(sf);

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf));

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	verify(session, never()).close();

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();
}
 
Example 17
Source File: OpenSessionInViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testOpenSessionInViewFilterAsyncScenario() throws Exception {
	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	// Initial request during which concurrent handling starts..

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");

	final AtomicInteger count = new AtomicInteger(0);

	final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
	filter.init(filterConfig);

	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			assertTrue(TransactionSynchronizationManager.hasResource(sf));
			count.incrementAndGet();
		}
	};

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(1, count.get());
	verify(session, never()).close();

	// Async dispatch after concurrent handling produces result ...

	this.request.setAsyncStarted(false);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	filter.doFilter(this.request, this.response, filterChain);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));
	assertEquals(2, count.get());

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();

	wac.close();
}
 
Example 18
Source File: OpenEntityManagerInViewTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}