Class JdbcIndexedSessionRepository

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

public class JdbcIndexedSessionRepository extends Object implements org.springframework.session.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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default name of database table used by Spring Session to store sessions.

    Fields inherited from interface org.springframework.session.FindByIndexNameSessionRepository

    PRINCIPAL_NAME_INDEX_NAME
  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession
     
    void
     
    org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession
     
    Map<String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    findByIndexNameAndIndexValue(String indexName, String indexValue)
     
    void
    save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
     
    void
    setConversionService(org.springframework.core.convert.ConversionService conversionService)
    Sets the ConversionService to use.
    void
    setCreateSessionAttributeQuery(String createSessionAttributeQuery)
    Set the custom SQL query used to create the session attribute.
    void
    setCreateSessionQuery(String createSessionQuery)
    Set the custom SQL query used to create the session.
    void
    setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
    Set the maximum inactive interval in seconds between requests before newly created sessions will be invalidated.
    void
    setDeleteSessionAttributeQuery(String deleteSessionAttributeQuery)
    Set the custom SQL query used to delete the session attribute.
    void
    setDeleteSessionQuery(String deleteSessionQuery)
    Set the custom SQL query used to delete the session.
    void
    setDeleteSessionsByExpiryTimeQuery(String deleteSessionsByExpiryTimeQuery)
    Set the custom SQL query used to delete the sessions by last access time.
    void
    setFlushMode(org.springframework.session.FlushMode flushMode)
    Set the flush mode.
    void
    setGetSessionQuery(String getSessionQuery)
    Set the custom SQL query used to retrieve the session.
    void
    setIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.Session> indexResolver)
    Set the IndexResolver to use.
    void
    setListSessionsByPrincipalNameQuery(String listSessionsByPrincipalNameQuery)
    Set the custom SQL query used to retrieve the sessions by principal name.
    void
    setLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler)
     
    void
    setSaveMode(org.springframework.session.SaveMode saveMode)
    Set the save mode.
    void
    setTableName(String tableName)
    Set the name of database table used to store sessions.
    void
    setUpdateSessionAttributeQuery(String updateSessionAttributeQuery)
    Set the custom SQL query used to update the session attribute.
    void
    setUpdateSessionQuery(String updateSessionQuery)
    Set the custom SQL query used to update the session.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.session.FindByIndexNameSessionRepository

    findByPrincipalName
  • 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(org.springframework.session.IndexResolver<org.springframework.session.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(org.springframework.session.FlushMode flushMode)
      Set the flush mode. Default is FlushMode.ON_SAVE.
      Parameters:
      flushMode - the flush mode
    • setSaveMode

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

      public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()
      Specified by:
      createSession in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    • save

      public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
      Specified by:
      save in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    • findById

      public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(String id)
      Specified by:
      findById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    • deleteById

      public void deleteById(String id)
      Specified by:
      deleteById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    • findByIndexNameAndIndexValue

      public Map<String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
      Specified by:
      findByIndexNameAndIndexValue in interface org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
    • cleanUpExpiredSessions

      public void cleanUpExpiredSessions()