Class JdbcIndexedSessionRepository

java.lang.Object
org.springframework.session.jdbc.JdbcIndexedSessionRepository
All Implemented Interfaces:
FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>, SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
Direct Known Subclasses:
JdbcOperationsSessionRepository

public class JdbcIndexedSessionRepository extends Object implements FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
A SessionRepository implementation that uses Spring's JdbcOperations to store sessions in a relational database. This implementation does not support publishing of session events.

An example of how to create a new instance can be seen below:

 JdbcTemplate jdbcTemplate = new JdbcTemplate();

 // ... configure jdbcTemplate ...

 TransactionTemplate transactionTemplate = new TransactionTemplate();

 // ... configure transactionTemplate ...

 JdbcIndexedSessionRepository sessionRepository =
         new JdbcIndexedSessionRepository(jdbcTemplate, transactionTemplate);
 
For additional information on how to create and configure JdbcTemplate and TransactionTemplate, refer to the Spring Framework Reference Documentation.

By default, this implementation uses SPRING_SESSION and SPRING_SESSION_ATTRIBUTES tables to store sessions. Note that the table name can be customized using the setTableName(String) method. In that case the table used to store attributes will be named using the provided table name, suffixed with _ATTRIBUTES. Depending on your database, the table definition can be described as below:

 CREATE TABLE SPRING_SESSION (
   PRIMARY_ID CHAR(36) NOT NULL,
   SESSION_ID CHAR(36) NOT NULL,
   CREATION_TIME BIGINT NOT NULL,
   LAST_ACCESS_TIME BIGINT NOT NULL,
   MAX_INACTIVE_INTERVAL INT NOT NULL,
   EXPIRY_TIME BIGINT NOT NULL,
   PRINCIPAL_NAME VARCHAR(100),
   CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
 );

 CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
 CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
 CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);

 CREATE TABLE SPRING_SESSION_ATTRIBUTES (
  SESSION_PRIMARY_ID CHAR(36) NOT NULL,
  ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
  ATTRIBUTE_BYTES BYTEA NOT NULL,
  CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
  CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
 );

 CREATE INDEX SPRING_SESSION_ATTRIBUTES_IX1 ON SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID);
 
Due to the differences between the various database vendors, especially when it comes to storing binary data, make sure to use SQL script specific to your database. Scripts for most major database vendors are packaged as org/springframework/session/jdbc/schema-*.sql, where * is the target database type.
Since:
2.2.0
  • Field Details

    • DEFAULT_TABLE_NAME

      public static final String DEFAULT_TABLE_NAME
      The default name of database table used by Spring Session to store sessions.
      See Also:
  • Constructor Details

    • JdbcIndexedSessionRepository

      public JdbcIndexedSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.support.TransactionOperations transactionOperations)
      Create a new JdbcIndexedSessionRepository instance which uses the provided JdbcOperations and TransactionOperations to manage sessions.
      Parameters:
      jdbcOperations - the JdbcOperations to use
      transactionOperations - the TransactionOperations to use
  • Method Details

    • setTableName

      public void setTableName(String tableName)
      Set the name of database table used to store sessions.
      Parameters:
      tableName - the database table name
    • setCreateSessionQuery

      public void setCreateSessionQuery(String createSessionQuery)
      Set the custom SQL query used to create the session.
      Parameters:
      createSessionQuery - the SQL query string
    • setCreateSessionAttributeQuery

      public void setCreateSessionAttributeQuery(String createSessionAttributeQuery)
      Set the custom SQL query used to create the session attribute.
      Parameters:
      createSessionAttributeQuery - the SQL query string
    • setGetSessionQuery

      public void setGetSessionQuery(String getSessionQuery)
      Set the custom SQL query used to retrieve the session.
      Parameters:
      getSessionQuery - the SQL query string
    • setUpdateSessionQuery

      public void setUpdateSessionQuery(String updateSessionQuery)
      Set the custom SQL query used to update the session.
      Parameters:
      updateSessionQuery - the SQL query string
    • setUpdateSessionAttributeQuery

      public void setUpdateSessionAttributeQuery(String updateSessionAttributeQuery)
      Set the custom SQL query used to update the session attribute.
      Parameters:
      updateSessionAttributeQuery - the SQL query string
    • setDeleteSessionAttributeQuery

      public void setDeleteSessionAttributeQuery(String deleteSessionAttributeQuery)
      Set the custom SQL query used to delete the session attribute.
      Parameters:
      deleteSessionAttributeQuery - the SQL query string
    • setDeleteSessionQuery

      public void setDeleteSessionQuery(String deleteSessionQuery)
      Set the custom SQL query used to delete the session.
      Parameters:
      deleteSessionQuery - the SQL query string
    • setListSessionsByPrincipalNameQuery

      public void setListSessionsByPrincipalNameQuery(String listSessionsByPrincipalNameQuery)
      Set the custom SQL query used to retrieve the sessions by principal name.
      Parameters:
      listSessionsByPrincipalNameQuery - the SQL query string
    • setDeleteSessionsByExpiryTimeQuery

      public void setDeleteSessionsByExpiryTimeQuery(String deleteSessionsByExpiryTimeQuery)
      Set the custom SQL query used to delete the sessions by last access time.
      Parameters:
      deleteSessionsByExpiryTimeQuery - the SQL query string
    • setDefaultMaxInactiveInterval

      public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
      Set the maximum inactive interval in seconds between requests before newly created sessions will be invalidated. A negative time indicates that the session will never timeout. The default is 1800 (30 minutes).
      Parameters:
      defaultMaxInactiveInterval - the maximum inactive interval in seconds
    • setIndexResolver

      public void setIndexResolver(IndexResolver<Session> indexResolver)
      Set the IndexResolver to use.
      Parameters:
      indexResolver - the index resolver
    • setLobHandler

      public void setLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler)
    • setConversionService

      public void setConversionService(org.springframework.core.convert.ConversionService conversionService)
      Sets the ConversionService to use.
      Parameters:
      conversionService - the converter to set
    • setFlushMode

      public void setFlushMode(FlushMode flushMode)
      Set the flush mode. Default is FlushMode.ON_SAVE.
      Parameters:
      flushMode - the flush mode
    • setSaveMode

      public void setSaveMode(SaveMode saveMode)
      Set the save mode.
      Parameters:
      saveMode - the save mode
    • createSession

      public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()
      Description copied from interface: SessionRepository
      Creates a new Session that is capable of being persisted by this SessionRepository.

      This allows optimizations and customizations in how the Session is persisted. For example, the implementation returned might keep track of the changes ensuring that only the delta needs to be persisted on a save.

      Specified by:
      createSession in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
      Returns:
      a new Session that is capable of being persisted by this SessionRepository
    • save

      public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
      Description copied from interface: SessionRepository
      Ensures the Session created by SessionRepository.createSession() is saved.

      Some implementations may choose to save as the Session is updated by returning a Session that immediately persists any changes. In this case, this method may not actually do anything.

      Specified by:
      save in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
      Parameters:
      session - the Session to save
    • findById

      public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(String id)
      Description copied from interface: SessionRepository
      Gets the Session by the Session.getId() or null if no Session is found.
      Specified by:
      findById in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
      Parameters:
      id - the Session.getId() to lookup
      Returns:
      the Session by the Session.getId() or null if no Session is found.
    • deleteById

      public void deleteById(String id)
      Description copied from interface: SessionRepository
      Deletes the Session with the given Session.getId() or does nothing if the Session is not found.
      Specified by:
      deleteById in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
      Parameters:
      id - the Session.getId() to delete
    • findByIndexNameAndIndexValue

      public Map<String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
      Description copied from interface: FindByIndexNameSessionRepository
      Find a Map of the session id to the Session of all sessions that contain the specified index name index value.
      Specified by:
      findByIndexNameAndIndexValue in interface FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
      Parameters:
      indexName - the name of the index (i.e. FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME)
      indexValue - the value of the index to search for.
      Returns:
      a Map (never null) of the session id to the Session of all sessions that contain the specified index name and index value. If no results are found, an empty Map is returned.
    • cleanUpExpiredSessions

      public void cleanUpExpiredSessions()