public class JdbcOperationsSessionRepository extends java.lang.Object implements FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.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 ... PlatformTransactionManager transactionManager = new DataSourceTransactionManager(); // ... configure transactionManager ... JdbcOperationsSessionRepository sessionRepository = new JdbcOperationsSessionRepository(jdbcTemplate, transactionManager);For additional information on how to create and configure
JdbcTemplate
and
PlatformTransactionManager
, 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 ( SESSION_ID CHAR(36), CREATION_TIME BIGINT NOT NULL, LAST_ACCESS_TIME BIGINT NOT NULL, MAX_INACTIVE_INTERVAL INT NOT NULL, PRINCIPAL_NAME VARCHAR(100), CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (SESSION_ID) ); CREATE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (LAST_ACCESS_TIME); CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (PRINCIPAL_NAME); CREATE TABLE SPRING_SESSION_ATTRIBUTES ( SESSION_ID CHAR(36), ATTRIBUTE_NAME VARCHAR(200), ATTRIBUTE_BYTES BYTEA, CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_ID, ATTRIBUTE_NAME), CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_ID) REFERENCES SPRING_SESSION(SESSION_ID) ON DELETE CASCADE ); CREATE INDEX SPRING_SESSION_ATTRIBUTES_IX1 ON SPRING_SESSION_ATTRIBUTES (SESSION_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 |
---|
JdbcOperationsSessionRepository(javax.sql.DataSource dataSource,
org.springframework.transaction.PlatformTransactionManager transactionManager)
Create a new
JdbcOperationsSessionRepository instance which uses the
default JdbcOperations to manage sessions. |
JdbcOperationsSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations,
org.springframework.transaction.PlatformTransactionManager transactionManager)
Create a new
JdbcOperationsSessionRepository instance which uses the
provided JdbcOperations to manage sessions. |
Modifier and Type | Method and Description |
---|---|
void |
cleanUpExpiredSessions() |
org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession |
createSession()
Creates a new
Session that is capable of being persisted by this
SessionRepository . |
void |
delete(java.lang.String id)
|
java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcOperationsSessionRepository.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 session attribute with the name
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME and the value of
the specified principal name. |
org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession |
getSession(java.lang.String id)
|
void |
save(org.springframework.session.jdbc.JdbcOperationsSessionRepository.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 |
setDeleteSessionsByLastAccessTimeQuery(java.lang.String deleteSessionsByLastAccessTimeQuery)
Set the custom SQL query used to delete the sessions by last access time.
|
void |
setGetSessionQuery(java.lang.String getSessionQuery)
Set the custom SQL query used to retrieve the session.
|
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 |
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 JdbcOperationsSessionRepository(javax.sql.DataSource dataSource, org.springframework.transaction.PlatformTransactionManager transactionManager)
JdbcOperationsSessionRepository
instance which uses the
default JdbcOperations
to manage sessions.dataSource
- the DataSource
to usetransactionManager
- the PlatformTransactionManager
to usepublic JdbcOperationsSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations, org.springframework.transaction.PlatformTransactionManager transactionManager)
JdbcOperationsSessionRepository
instance which uses the
provided JdbcOperations
to manage sessions.jdbcOperations
- the JdbcOperations
to usetransactionManager
- the PlatformTransactionManager
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 setDeleteSessionsByLastAccessTimeQuery(java.lang.String deleteSessionsByLastAccessTimeQuery)
deleteSessionsByLastAccessTimeQuery
- the SQL query stringpublic void setDefaultMaxInactiveInterval(java.lang.Integer defaultMaxInactiveInterval)
defaultMaxInactiveInterval
- the maximum inactive interval in secondspublic 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 org.springframework.session.jdbc.JdbcOperationsSessionRepository.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.JdbcOperationsSessionRepository.JdbcSession>
Session
that is capable of being persisted by this
SessionRepository
public void save(org.springframework.session.jdbc.JdbcOperationsSessionRepository.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.JdbcOperationsSessionRepository.JdbcSession>
session
- the Session
to savepublic org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession getSession(java.lang.String id)
SessionRepository
getSession
in interface SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>
id
- the Session.getId()
to lookupSession
by the Session.getId()
or null if no
Session
is found.public void delete(java.lang.String id)
SessionRepository
delete
in interface SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>
id
- the Session.getId()
to deletepublic java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession> findByIndexNameAndIndexValue(java.lang.String indexName, java.lang.String indexValue)
FindByIndexNameSessionRepository
Session
of all sessions that contain
the session attribute with the name
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
and the value of
the specified principal name.findByIndexNameAndIndexValue
in interface FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>
indexName
- the name of the index (i.e.
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
)indexValue
- the value of the index to search for.Session
of all sessions
that contain the session specified index name and the value of the specified index
name. If no results are found, an empty Map is returned.@Scheduled(cron="${spring.session.cleanup.cron.expression:0 * * * * *}") public void cleanUpExpiredSessions()