Skip to content

Commit 1079e9e

Browse files
committed
Ignore failed rename operation for deleted session
Attempting to change session id for a deleted session currently results in "ERR no such key" error on rename operation of expired key. This commit addressed the problem by ignoring the aforementioned error. Closes #1177
1 parent 5fa52be commit 1079e9e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryITests.java

+16
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,22 @@ public void changeSessionIdSaveTwice() {
581581
assertThat(this.repository.findById(originalId)).isNull();
582582
}
583583

584+
// gh-1137
585+
@Test
586+
public void changeSessionIdWhenSessionIsDeleted() {
587+
RedisSession toSave = this.repository.createSession();
588+
String sessionId = toSave.getId();
589+
this.repository.save(toSave);
590+
591+
this.repository.deleteById(sessionId);
592+
593+
toSave.changeSessionId();
594+
this.repository.save(toSave);
595+
596+
assertThat(this.repository.findById(toSave.getId())).isNull();
597+
assertThat(this.repository.findById(sessionId)).isNull();
598+
}
599+
584600
private String getSecurityName() {
585601
return this.context.getAuthentication().getName();
586602
}

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisOperationsSessionRepository.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import org.springframework.context.ApplicationEvent;
3030
import org.springframework.context.ApplicationEventPublisher;
31+
import org.springframework.core.NestedExceptionUtils;
32+
import org.springframework.dao.NonTransientDataAccessException;
3133
import org.springframework.data.redis.connection.Message;
3234
import org.springframework.data.redis.connection.MessageListener;
3335
import org.springframework.data.redis.core.BoundHashOperations;
@@ -814,8 +816,16 @@ private void saveChangeSessionId(String sessionId) {
814816
originalSessionIdKey, sessionIdKey);
815817
String originalExpiredKey = getExpiredKey(this.originalSessionId);
816818
String expiredKey = getExpiredKey(sessionId);
817-
RedisOperationsSessionRepository.this.sessionRedisOperations.rename(
818-
originalExpiredKey, expiredKey);
819+
try {
820+
RedisOperationsSessionRepository.this.sessionRedisOperations.rename(
821+
originalExpiredKey, expiredKey);
822+
}
823+
catch (NonTransientDataAccessException ex) {
824+
if (!"ERR no such key".equals(NestedExceptionUtils
825+
.getMostSpecificCause(ex).getMessage())) {
826+
throw ex;
827+
}
828+
}
819829
}
820830
this.originalSessionId = sessionId;
821831
}

0 commit comments

Comments
 (0)