Package org.springframework.session.jdbc
Class JdbcIndexedSessionRepository
java.lang.Object
org.springframework.session.jdbc.JdbcIndexedSessionRepository
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>,org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
public class JdbcIndexedSessionRepository
extends Object
implements org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
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
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.JdbcSessionvoiddeleteById(String id) voiddestroy()org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSessionfindByIndexNameAndIndexValue(String indexName, String indexValue) voidsave(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) 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(org.springframework.session.FlushMode flushMode) Set the flush mode.voidsetGetSessionQuery(String getSessionQuery) Set the custom SQL query used to retrieve the session.voidsetIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.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(org.springframework.session.SaveMode saveMode) Set the save mode.voidsetSessionIdGenerator(org.springframework.session.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
public void setIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.Session> indexResolver) 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
public void setFlushMode(org.springframework.session.FlushMode flushMode) Set the flush mode. Default isFlushMode.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
-
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()- Specified by:
createSessionin interfaceorg.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
-
save
public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) - Specified by:
savein interfaceorg.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
-
findById
public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(String id) - Specified by:
findByIdin interfaceorg.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
-
deleteById
- Specified by:
deleteByIdin interfaceorg.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:
findByIndexNameAndIndexValuein interfaceorg.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
-
cleanUpExpiredSessions
public void cleanUpExpiredSessions() -
setSessionIdGenerator
public void setSessionIdGenerator(org.springframework.session.SessionIdGenerator sessionIdGenerator) Set theSessionIdGeneratorto use to generate session ids.- Parameters:
sessionIdGenerator- theSessionIdGeneratorto use- Since:
- 3.2
-
setDefaultMaxInactiveInterval(Duration)