Skip to content

Commit 4bb2bd6

Browse files
committed
JDBC session with negative timeout should never expire
Closes gh-1847
1 parent 0e5dd18 commit 4bb2bd6

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

spring-session-jdbc/src/integration-test/java/org/springframework/session/jdbc/AbstractJdbcIndexedSessionRepositoryITests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,18 @@ void cleanupInactiveSessionsUsingSessionDefinedInterval() {
586586
assertThat(this.repository.findById(session.getId())).isNull();
587587
}
588588

589+
@Test
590+
void cleanupExpiredSessionsWhenMaxInactiveIntervalNegativeThenSessionNotDeleted() {
591+
JdbcSession session = this.repository.createSession();
592+
session.setMaxInactiveInterval(Duration.ofSeconds(-1));
593+
session.setLastAccessedTime(Instant.now().minusSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS + 1));
594+
595+
this.repository.save(session);
596+
this.repository.cleanUpExpiredSessions();
597+
598+
assertThat(this.repository.findById(session.getId())).isNotNull();
599+
}
600+
589601
@Test
590602
void changeSessionIdWhenOnlyChangeId() {
591603
String attrName = "changeSessionId";

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public class JdbcIndexedSessionRepository
185185
// @formatter:off
186186
private static final String DELETE_SESSION_QUERY = ""
187187
+ "DELETE FROM %TABLE_NAME% "
188-
+ "WHERE SESSION_ID = ?";
188+
+ "WHERE SESSION_ID = ? "
189+
+ "AND MAX_INACTIVE_INTERVAL >= 0";
189190
// @formatter:on
190191

191192
// @formatter:off
@@ -701,6 +702,9 @@ void clearChangeFlags() {
701702
}
702703

703704
Instant getExpiryTime() {
705+
if (getMaxInactiveInterval().isNegative()) {
706+
return Instant.ofEpochMilli(Long.MAX_VALUE);
707+
}
704708
return getLastAccessedTime().plus(getMaxInactiveInterval());
705709
}
706710

0 commit comments

Comments
 (0)