public class JdbcIndexedSessionRepository
extends java.lang.Object
implements org.springframework.session.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_IX1 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.
|
| 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() |
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) |
void |
save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session) |
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(org.springframework.session.FlushMode flushMode)
Set the flush mode.
|
void |
setGetSessionQuery(java.lang.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(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(org.springframework.session.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.
|
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(org.springframework.session.IndexResolver<org.springframework.session.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(org.springframework.session.FlushMode flushMode)
FlushMode.ON_SAVE.flushMode - the flush modepublic void setSaveMode(org.springframework.session.SaveMode saveMode)
saveMode - the save modepublic org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession createSession()
createSession in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>public void save(org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession session)
save in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>public org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession findById(java.lang.String id)
findById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>public void deleteById(java.lang.String id)
deleteById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>public java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession> findByIndexNameAndIndexValue(java.lang.String indexName,
java.lang.String indexValue)
findByIndexNameAndIndexValue in interface org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession>public void cleanUpExpiredSessions()