Skip to content

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

Closed
@lixyou

Description

@lixyou

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?

Activity

rwinch

rwinch commented on Sep 6, 2018

@rwinch
Member

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

lixyou commented on Sep 6, 2018

@lixyou
Author

@rwinch

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

self-assigned this
on Sep 6, 2018
vpavic

vpavic commented on Sep 6, 2018

@vpavic
Contributor

@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

Metadata

Metadata

Assignees

Labels

for: stack-overflowA question that's better suited to stackoverflow.com

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rwinch@vpavic@lixyou

      Issue actions

        Why is the request.commitSession() method called repeatedly? · Issue #1183 · spring-projects/spring-session