spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

JdbcIndexedSessionRepository should allow setting table name after custom queries

Open NYPD opened this issue 3 months ago • 0 comments

Expected Behavior

When creating a custom Query customizer by implementing SessionRepositoryCustomizer<JdbcIndexedSessionRepository> one should allowed to set the table name after setting the custom queries like so:

@Override
public void customize(JdbcIndexedSessionRepository sessionRepository) {
    sessionRepository.setCreateSessionAttributeQuery(CUSTOM_QUERY_1);
    sessionRepository.setUpdateSessionQuery(CUSTOM_QUERY_2);
    sessionRepository.setTableName("cool_table_name");
}

Current Behavior

However currently it is "forced" to call the setTableName first because setTableName calls the prepareQueries() method in the JdbcIndexedSessionRepository class which then resets all the queries with the default ones:

private void prepareQueries() {
    this.createSessionQuery = getQuery(CREATE_SESSION_QUERY);
    this.createSessionAttributeQuery = getQuery(CREATE_SESSION_ATTRIBUTE_QUERY);
    this.getSessionQuery = getQuery(GET_SESSION_QUERY);
    this.updateSessionQuery = getQuery(UPDATE_SESSION_QUERY);
    this.updateSessionAttributeQuery = getQuery(UPDATE_SESSION_ATTRIBUTE_QUERY);
    this.deleteSessionAttributeQuery = getQuery(DELETE_SESSION_ATTRIBUTE_QUERY);
    this.deleteSessionQuery = getQuery(DELETE_SESSION_QUERY);
    this.listSessionsByPrincipalNameQuery = getQuery(LIST_SESSIONS_BY_PRINCIPAL_NAME_QUERY);
    this.deleteSessionsByExpiryTimeQuery = getQuery(DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY);
}

Context

Obviously the simple work around is just call the setTableName first then set the custom queries, however the above issue I mentioned is not told to the user (no warning or exception) so a user might go on to believe their custom queries are working as expected since nothing tells them otherwise.

My suggestion would be to update the prepareQueries() method to simply use the private variable if it is not null like so:

      private void prepareQueries() {
            this.createSessionQuery = getQuery(this.createSessionQuery == null? CREATE_SESSION_QUERY : this.createSessionQuery);
            this.createSessionAttributeQuery = getQuery(this.createSessionQuery == null? CREATE_SESSION_ATTRIBUTE_QUERY : this.createSessionAttributeQuery);
            this.getSessionQuery = getQuery(this.createSessionQuery == null? GET_SESSION_QUERY : this.getSessionQuery);
            this.updateSessionQuery = getQuery(this.createSessionQuery == null? UPDATE_SESSION_QUERY : this.updateSessionQuery);
            this.updateSessionAttributeQuery = getQuery(this.createSessionQuery == null? UPDATE_SESSION_ATTRIBUTE_QUERY : this.updateSessionAttributeQuery);
            this.deleteSessionAttributeQuery = getQuery(this.createSessionQuery == null? DELETE_SESSION_ATTRIBUTE_QUERY : this.deleteSessionAttributeQuery);
            this.deleteSessionQuery = getQuery(this.createSessionQuery == null? DELETE_SESSION_QUERY : this.deleteSessionQuery);
            this.listSessionsByPrincipalNameQuery = getQuery(this.createSessionQuery == null? LIST_SESSIONS_BY_PRINCIPAL_NAME_QUERY : this.listSessionsByPrincipalNameQuery);
            this.deleteSessionsByExpiryTimeQuery = getQuery(this.createSessionQuery == null? DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY : this.deleteSessionsByExpiryTimeQuery);
      }

(Or something cleaner)

NYPD avatar Oct 10 '25 19:10 NYPD