Skip to content

Commit 89e064d

Browse files
committed
Avoid extraneous call to SessionRespository.findById(...) in SessionRepositoryRequestWrapper.commitSession() due to premature cache clearing.
Closes gh-1731
1 parent 2aae51b commit 89e064d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

spring-session-core/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ private void commitSession() {
221221
}
222222
else {
223223
S session = wrappedSession.getSession();
224-
clearRequestedSessionCache();
225-
SessionRepositoryFilter.this.sessionRepository.save(session);
226-
String sessionId = session.getId();
227-
if (!isRequestedSessionIdValid() || !sessionId.equals(getRequestedSessionId())) {
228-
SessionRepositoryFilter.this.httpSessionIdResolver.setSessionId(this, this.response, sessionId);
224+
try {
225+
SessionRepositoryFilter.this.sessionRepository.save(session);
226+
String sessionId = session.getId();
227+
if (!isRequestedSessionIdValid() || !sessionId.equals(getRequestedSessionId())) {
228+
SessionRepositoryFilter.this.httpSessionIdResolver.setSessionId(this, this.response, sessionId);
229+
}
230+
}
231+
finally {
232+
clearRequestedSessionCache();
229233
}
230234
}
231235
}

spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1313,8 +1313,8 @@ public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrap
13131313
}
13141314
});
13151315

1316-
// 3 invocations expected: initial resolution, after invalidation, after commit
1317-
verify(sessionRepository, times(3)).findById(eq(session.getId()));
1316+
// 3 invocations expected: initial resolution, after invalidation
1317+
verify(sessionRepository, times(2)).findById(eq(session.getId()));
13181318
verify(sessionRepository).deleteById(eq(session.getId()));
13191319
verify(sessionRepository).createSession();
13201320
verify(sessionRepository).save(any());

0 commit comments

Comments
 (0)