spring-projects / spring-projects/spring-session

JdbcIndexedSessionRepository should allow setting table name after custom queries

Open
#3,476 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
1.9k
Forks
1.2k
Avg merge
4h 27m
Merged PRs (30d)
55

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JdbcIndexedSessionRepository and its prepareQueries() method, then trace how setTableName() resets the query fields. Verify the reported customization order, where custom queries are set before the table name, and ensure the custom queries remain effective after setTableName() is called.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.