public class JdbcIndexedSessionRepository extends java.lang.Object implements FindByIndexNameSessionRepository<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.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_TABLE_NAME
The default name of database table used by Spring Session to store sessions.
|
PRINCIPAL_NAME_INDEX_NAME
Constructor and 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. |
Modifier and Type | Method and Description |
---|---|
void |
cleanUpExpiredSessions() |
org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession |
createSession()
Creates a new
Session that is capable of being persisted by this
SessionRepository . |
void |
deleteById(java.lang.String id)
|
org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession |
findById(java.lang.String id)
|
java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> |
findByIndexNameAndIndexValue(java.lang.String indexName,
java.lang.String indexValue)
Find a
Map of the session id to the Session of all sessions that
contain the specified index name index value. |
void |
save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
Ensures the
Session created by
SessionRepository.createSession() is saved. |
void |
setConversionService(org.springframework.core.convert.ConversionService conversionService)
Sets the
ConversionService to use. |
void |
setCreateSessionAttributeQuery(java.lang.String createSessionAttributeQuery)
Set the custom SQL query used to create the session attribute.
|
void |
setCreateSessionQuery(java.lang.String createSessionQuery)
Set the custom SQL query used to create the session.
|
void |
setDefaultMaxInactiveInterval(java.lang.Integer defaultMaxInactiveInterval)
Set the maximum inactive interval in seconds between requests before newly created
sessions will be invalidated.
|
void |
setDeleteSessionAttributeQuery(java.lang.String deleteSessionAttributeQuery)
Set the custom SQL query used to delete the session attribute.
|
void |
setDeleteSessionQuery(java.lang.String deleteSessionQuery)
Set the custom SQL query used to delete the session.
|
void |
setDeleteSessionsByExpiryTimeQuery(java.lang.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(java.lang.String getSessionQuery)
Set the custom SQL query used to retrieve the session.
|
void |
setIndexResolver(IndexResolver<Session> indexResolver)
Set the
IndexResolver to use. |
void |
setListSessionsByPrincipalNameQuery(java.lang.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(java.lang.String tableName)
Set the name of database table used to store sessions.
|
void |
setUpdateSessionAttributeQuery(java.lang.String updateSessionAttributeQuery)
Set the custom SQL query used to update the session attribute.
|
void |
setUpdateSessionQuery(java.lang.String updateSessionQuery)
Set the custom SQL query used to update the session.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
findByPrincipalName
public static final java.lang.String DEFAULT_TABLE_NAME
public JdbcIndexedSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.support.TransactionOperations transactionOperations)
JdbcIndexedSessionRepository
instance which uses the provided
JdbcOperations
and TransactionOperations
to manage sessions.jdbcOperations
- the JdbcOperations
to usetransactionOperations
- the TransactionOperations
to usepublic void setTableName(java.lang.String tableName)
tableName
- the database table namepublic void setCreateSessionQuery(java.lang.String createSessionQuery)
createSessionQuery
- the SQL query stringpublic void setCreateSessionAttributeQuery(java.lang.String createSessionAttributeQuery)
createSessionAttributeQuery
- the SQL query stringpublic void setGetSessionQuery(java.lang.String getSessionQuery)
getSessionQuery
- the SQL query stringpublic void setUpdateSessionQuery(java.lang.String updateSessionQuery)
updateSessionQuery
- the SQL query stringpublic void setUpdateSessionAttributeQuery(java.lang.String updateSessionAttributeQuery)
updateSessionAttributeQuery
- the SQL query stringpublic void setDeleteSessionAttributeQuery(java.lang.String deleteSessionAttributeQuery)
deleteSessionAttributeQuery
- the SQL query stringpublic void setDeleteSessionQuery(java.lang.String deleteSessionQuery)
deleteSessionQuery
- the SQL query stringpublic void setListSessionsByPrincipalNameQuery(java.lang.String listSessionsByPrincipalNameQuery)
listSessionsByPrincipalNameQuery
- the SQL query stringpublic void setDeleteSessionsByExpiryTimeQuery(java.lang.String deleteSessionsByExpiryTimeQuery)
deleteSessionsByExpiryTimeQuery
- the SQL query stringpublic void setDefaultMaxInactiveInterval(java.lang.Integer defaultMaxInactiveInterval)
defaultMaxInactiveInterval
- the maximum inactive interval in secondspublic void setIndexResolver(IndexResolver<Session> indexResolver)
IndexResolver
to use.indexResolver
- the index resolverpublic void setLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler)
public void setConversionService(org.springframework.core.convert.ConversionService conversionService)
ConversionService
to use.conversionService
- the converter to setpublic void setFlushMode(FlushMode flushMode)
FlushMode.ON_SAVE
.flushMode
- the flush modepublic void setSaveMode(SaveMode saveMode)
saveMode
- the save modepublic org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()
SessionRepository
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.
createSession
in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
Session
that is capable of being persisted by this
SessionRepository
public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
SessionRepository
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.
save
in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
session
- the Session
to savepublic org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(java.lang.String id)
SessionRepository
findById
in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
id
- the Session.getId()
to lookupSession
by the Session.getId()
or null if no
Session
is found.public void deleteById(java.lang.String id)
SessionRepository
deleteById
in interface SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
id
- the Session.getId()
to deletepublic java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> findByIndexNameAndIndexValue(java.lang.String indexName, java.lang.String indexValue)
FindByIndexNameSessionRepository
Map
of the session id to the Session
of all sessions that
contain the specified index name index value.findByIndexNameAndIndexValue
in interface FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>
indexName
- the name of the index (i.e.
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
)indexValue
- the value of the index to search for.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.public void cleanUpExpiredSessions()