Class JdbcIndexedSessionRepository
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>,SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
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
FieldsModifier and TypeFieldDescriptionstatic final StringThe default cron expression used for expired session cleanup job.static final StringThe 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
ConstructorsConstructorDescriptionJdbcIndexedSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.support.TransactionOperations transactionOperations) Create a newJdbcIndexedSessionRepositoryinstance which uses the providedJdbcOperationsandTransactionOperationsto manage sessions. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidorg.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSessionCreates a newSessionthat is capable of being persisted by thisSessionRepository.voiddeleteById(String id) voiddestroy()org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSessionfindByIndexNameAndIndexValue(String indexName, String indexValue) voidsave(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) Ensures theSessioncreated bySessionRepository.createSession()is saved.voidsetCleanupCron(String cleanupCron) Set the cleanup cron expression.voidsetConversionService(org.springframework.core.convert.ConversionService conversionService) Sets theConversionServiceto use.voidsetCreateSessionAttributeQuery(String createSessionAttributeQuery) Set the custom SQL query used to create the session attribute.voidsetCreateSessionQuery(String createSessionQuery) Set the custom SQL query used to create the session.voidsetDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval) Deprecated.voidsetDefaultMaxInactiveInterval(Duration defaultMaxInactiveInterval) Set the maximum inactive interval in seconds between requests before newly created sessions will be invalidated.voidsetDeleteSessionAttributeQuery(String deleteSessionAttributeQuery) Set the custom SQL query used to delete the session attribute.voidsetDeleteSessionQuery(String deleteSessionQuery) Set the custom SQL query used to delete the session.voidsetDeleteSessionsByExpiryTimeQuery(String deleteSessionsByExpiryTimeQuery) Set the custom SQL query used to delete the sessions by last access time.voidsetFlushMode(FlushMode flushMode) Set the flush mode.voidsetGetSessionQuery(String getSessionQuery) Set the custom SQL query used to retrieve the session.voidsetIndexResolver(IndexResolver<Session> indexResolver) Set theIndexResolverto use.voidsetListSessionsByPrincipalNameQuery(String listSessionsByPrincipalNameQuery) Set the custom SQL query used to retrieve the sessions by principal name.voidsetLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler) voidsetSaveMode(SaveMode saveMode) Set the save mode.voidsetSessionIdGenerator(SessionIdGenerator sessionIdGenerator) Set theSessionIdGeneratorto use to generate session ids.voidsetTableName(String tableName) Set the name of database table used to store sessions.voidsetUpdateSessionAttributeQuery(String updateSessionAttributeQuery) Set the custom SQL query used to update the session attribute.voidsetUpdateSessionQuery(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, waitMethods inherited from interface org.springframework.session.FindByIndexNameSessionRepository
findByPrincipalName
-
Field Details
-
DEFAULT_TABLE_NAME
The default name of database table used by Spring Session to store sessions.- See Also:
-
DEFAULT_CLEANUP_CRON
The default cron expression used for expired session cleanup job.- See Also:
-
-
Constructor Details
-
JdbcIndexedSessionRepository
public JdbcIndexedSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.support.TransactionOperations transactionOperations) Create a newJdbcIndexedSessionRepositoryinstance which uses the providedJdbcOperationsandTransactionOperationsto manage sessions.- Parameters:
jdbcOperations- theJdbcOperationsto usetransactionOperations- theTransactionOperationsto use
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
destroy
public void destroy()- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-
setTableName
Set the name of database table used to store sessions.- Parameters:
tableName- the database table name
-
setCreateSessionQuery
Set the custom SQL query used to create the session.- Parameters:
createSessionQuery- the SQL query string
-
setCreateSessionAttributeQuery
Set the custom SQL query used to create the session attribute.- Parameters:
createSessionAttributeQuery- the SQL query string
-
setGetSessionQuery
Set the custom SQL query used to retrieve the session.- Parameters:
getSessionQuery- the SQL query string
-
setUpdateSessionQuery
Set the custom SQL query used to update the session.- Parameters:
updateSessionQuery- the SQL query string
-
setUpdateSessionAttributeQuery
Set the custom SQL query used to update the session attribute.- Parameters:
updateSessionAttributeQuery- the SQL query string
-
setDeleteSessionAttributeQuery
Set the custom SQL query used to delete the session attribute.- Parameters:
deleteSessionAttributeQuery- the SQL query string
-
setDeleteSessionQuery
Set the custom SQL query used to delete the session.- Parameters:
deleteSessionQuery- the SQL query string
-
setListSessionsByPrincipalNameQuery
Set the custom SQL query used to retrieve the sessions by principal name.- Parameters:
listSessionsByPrincipalNameQuery- the SQL query string
-
setDeleteSessionsByExpiryTimeQuery
Set the custom SQL query used to delete the sessions by last access time.- Parameters:
deleteSessionsByExpiryTimeQuery- the SQL query string
-
setDefaultMaxInactiveInterval
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 time out. The default is 30 minutes.- Parameters:
defaultMaxInactiveInterval- the default maxInactiveInterval
-
setDefaultMaxInactiveInterval
@Deprecated(since="3.0.0") public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval) Deprecated.since 3.0.0, in favor ofsetDefaultMaxInactiveInterval(Duration)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 time out. The default is 1800 (30 minutes).- Parameters:
defaultMaxInactiveInterval- the default maxInactiveInterval in seconds
-
setIndexResolver
Set theIndexResolverto 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 theConversionServiceto use.- Parameters:
conversionService- the converter to set
-
setFlushMode
Set the flush mode. Default isFlushMode.ON_SAVE.- Parameters:
flushMode- the flush mode
-
setSaveMode
Set the save mode.- Parameters:
saveMode- the save mode
-
setCleanupCron
Set the cleanup cron expression.- Parameters:
cleanupCron- the cleanup cron expression- Since:
- 3.0.0
- See Also:
-
CronExpressionScheduled.CRON_DISABLED
-
createSession
public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()Description copied from interface:SessionRepositoryCreates a newSessionthat is capable of being persisted by thisSessionRepository.This allows optimizations and customizations in how the
Sessionis 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:
createSessionin interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>- Returns:
- a new
Sessionthat is capable of being persisted by thisSessionRepository
-
save
public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) Description copied from interface:SessionRepositoryEnsures theSessioncreated bySessionRepository.createSession()is saved.Some implementations may choose to save as the
Sessionis updated by returning aSessionthat immediately persists any changes. In this case, this method may not actually do anything.- Specified by:
savein interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>- Parameters:
session- theSessionto save
-
findById
public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(String id) Description copied from interface:SessionRepository- Specified by:
findByIdin interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>- Parameters:
id- theSession.getId()to lookup- Returns:
- the
Sessionby theSession.getId()or null if noSessionis found.
-
deleteById
Description copied from interface:SessionRepository- Specified by:
deleteByIdin interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>- Parameters:
id- theSession.getId()to delete
-
findByIndexNameAndIndexValue
public Map<String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> findByIndexNameAndIndexValue(String indexName, String indexValue) Description copied from interface:FindByIndexNameSessionRepositoryFind aMapof the session id to theSessionof all sessions that contain the specified index name index value.- Specified by:
findByIndexNameAndIndexValuein interfaceFindByIndexNameSessionRepository<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(nevernull) of the session id to theSessionof all sessions that contain the specified index name and index value. If no results are found, an emptyMapis returned.
-
cleanUpExpiredSessions
public void cleanUpExpiredSessions() -
setSessionIdGenerator
Set theSessionIdGeneratorto use to generate session ids.- Parameters:
sessionIdGenerator- theSessionIdGeneratorto use- Since:
- 3.2
-
setDefaultMaxInactiveInterval(Duration)