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
Modifier and TypeFieldDescriptionstatic final String
The default cron expression used for expired session cleanup job.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
ConstructorDescriptionJdbcIndexedSessionRepository
(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.support.TransactionOperations transactionOperations) Create a newJdbcIndexedSessionRepository
instance which uses the providedJdbcOperations
andTransactionOperations
to manage sessions. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession
Creates a newSession
that is capable of being persisted by thisSessionRepository
.void
deleteById
(String id) void
destroy()
org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession
findByIndexNameAndIndexValue
(String indexName, String indexValue) void
save
(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) Ensures theSession
created bySessionRepository.createSession()
is saved.void
setCleanupCron
(String cleanupCron) Set the cleanup cron expression.void
setConversionService
(org.springframework.core.convert.ConversionService conversionService) Sets theConversionService
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) Deprecated.void
setDefaultMaxInactiveInterval
(Duration 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
(FlushMode flushMode) Set the flush mode.void
setGetSessionQuery
(String getSessionQuery) Set the custom SQL query used to retrieve the session.void
setIndexResolver
(IndexResolver<Session> indexResolver) Set theIndexResolver
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
(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
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 newJdbcIndexedSessionRepository
instance which uses the providedJdbcOperations
andTransactionOperations
to manage sessions.- Parameters:
jdbcOperations
- theJdbcOperations
to usetransactionOperations
- theTransactionOperations
to use
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
destroy
public void destroy()- Specified by:
destroy
in 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 theIndexResolver
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 theConversionService
to 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:
-
CronExpression
Scheduled.CRON_DISABLED
-
createSession
public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()Description copied from interface:SessionRepository
Creates a newSession
that is capable of being persisted by thisSessionRepository
.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 interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
- Returns:
- a new
Session
that is capable of being persisted by thisSessionRepository
-
save
public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) Description copied from interface:SessionRepository
Ensures theSession
created bySessionRepository.createSession()
is saved.Some implementations may choose to save as the
Session
is updated by returning aSession
that immediately persists any changes. In this case, this method may not actually do anything.- Specified by:
save
in interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
- Parameters:
session
- theSession
to save
-
findById
public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(String id) Description copied from interface:SessionRepository
- Specified by:
findById
in interfaceSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
- Parameters:
id
- theSession.getId()
to lookup- Returns:
- the
Session
by theSession.getId()
or null if noSession
is found.
-
deleteById
Description copied from interface:SessionRepository
- Specified by:
deleteById
in 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:FindByIndexNameSessionRepository
Find aMap
of the session id to theSession
of all sessions that contain the specified index name index value.- Specified by:
findByIndexNameAndIndexValue
in 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 theSession
of all sessions that contain the specified index name and index value. If no results are found, an emptyMap
is returned.
-
cleanUpExpiredSessions
public void cleanUpExpiredSessions()
-
setDefaultMaxInactiveInterval(Duration)