Skip to content

Why is the request.commitSession() method called repeatedly? #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lixyou opened this issue Sep 6, 2018 · 3 comments
Closed

Why is the request.commitSession() method called repeatedly? #1183

lixyou opened this issue Sep 6, 2018 · 3 comments
Assignees
Labels
for: stack-overflow A question that's better suited to stackoverflow.com

Comments

@lixyou
Copy link

lixyou commented Sep 6, 2018

based on spring-session 2.0.5.RELEASE

There are two code snippets here. eg:

First, for the method of doFilterInternal in SessionRepostoryFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request,
		HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
	request.setAttribute(SESSION_REPOSITORY_ATTR, this.sessionRepository);
	SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(
			request, response, this.servletContext);
	SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(
			wrappedRequest, response);
	try {
		filterChain.doFilter(wrappedRequest, wrappedResponse);
	}
	finally {
		wrappedRequest.commitSession();
	}
}

Second, for the method of onResponseCommitted in SessionRepositoryResponseWrapper.java

private final class SessionRepositoryResponseWrapper
		extends OnCommittedResponseWrapper {
	private final SessionRepositoryRequestWrapper request;
	/**
	 * Create a new {@link SessionRepositoryResponseWrapper}.
	 * @param request the request to be wrapped
	 * @param response the response to be wrapped
	 */
	SessionRepositoryResponseWrapper(SessionRepositoryRequestWrapper request,
			HttpServletResponse response) {
		super(response);
		if (request == null) {
			throw new IllegalArgumentException("request cannot be null");
		}
		this.request = request;
	}
	@Override
	protected void onResponseCommitted() {
		this.request.commitSession();
	}
}

The request.commitSession() method is called in both places.

why? Is this not a repeated call?

@rwinch
Copy link
Member

rwinch commented Sep 6, 2018

Is this causing you problems? The reason is that we need to ensure that the session is created before the response is committed. If the response is already committed there will be no way to track the session (i.e. a cookie cannot be written to the response to keep track of which session id).

@lixyou
Copy link
Author

lixyou commented Sep 6, 2018

@rwinch

Thanks, No problem. just me wondering. I understand this logic after reading JavaTM Servlet Specification.

@lixyou lixyou closed this as completed Sep 6, 2018
@vpavic vpavic added the for: stack-overflow A question that's better suited to stackoverflow.com label Sep 6, 2018
@vpavic vpavic self-assigned this Sep 6, 2018
@vpavic
Copy link
Contributor

vpavic commented Sep 6, 2018

@lixyou On top what @rwinch provided here, you can also find information about this in the Custom SessionRepository section of our reference manual and in this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stack-overflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

3 participants