Fix JdbcOperationsSessionRepository session cleanup

Make JDBC session cleanup logic actually cleanup expired sessions
as opposed to all sessions

Fixes gh-421
This commit is contained in:
Erin Drummond
2016-03-11 11:21:49 +13:00
committed by Rob Winch
parent bd6d5bf419
commit 49f24e493d
2 changed files with 52 additions and 13 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.session.jdbc;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@@ -302,14 +301,20 @@ public class JdbcOperationsSessionRepository implements
@Scheduled(cron = "0 * * * * *")
public void cleanUpExpiredSessions() {
long now = System.currentTimeMillis();
long roundedNow = roundDownMinute(now);
int maxInactiveIntervalSeconds = (this.defaultMaxInactiveInterval != null)
? this.defaultMaxInactiveInterval
: MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
long sessionsValidFromTime = now - (maxInactiveIntervalSeconds * 1000);
if (logger.isDebugEnabled()) {
logger.debug("Cleaning up sessions expiring at " + new Date(roundedNow));
logger.debug(
"Cleaning up sessions older than " + new Date(sessionsValidFromTime));
}
int deletedCount = this.jdbcOperations
.update(getQuery(DELETE_SESSIONS_BY_LAST_ACCESS_TIME_QUERY), roundedNow);
int deletedCount = this.jdbcOperations.update(
getQuery(DELETE_SESSIONS_BY_LAST_ACCESS_TIME_QUERY),
sessionsValidFromTime);
if (logger.isDebugEnabled()) {
logger.debug("Cleaned up " + deletedCount + " expired sessions");
@@ -332,14 +337,6 @@ public class JdbcOperationsSessionRepository implements
TypeDescriptor.valueOf(byte[].class));
}
private static long roundDownMinute(long timeInMs) {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(timeInMs);
date.clear(Calendar.SECOND);
date.clear(Calendar.MILLISECOND);
return date.getTimeInMillis();
}
private static GenericConversionService createDefaultConversionService() {
GenericConversionService converter = new GenericConversionService();
converter.addConverter(ExpiringSession.class, byte[].class,