public class JdbcOperationsSessionRepository
extends java.lang.Object
implements org.springframework.session.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 ( 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 |
|---|
JdbcOperationsSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations)
Create a new
JdbcOperationsSessionRepository instance which uses the
provided 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() |
void |
deleteById(java.lang.String id) |
org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession |
findById(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) |
void |
save(org.springframework.session.jdbc.JdbcOperationsSessionRepository.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 |
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(org.springframework.jdbc.core.JdbcOperations jdbcOperations,
org.springframework.transaction.PlatformTransactionManager transactionManager)
JdbcOperationsSessionRepository instance which uses the
provided JdbcOperations to manage sessions.
The created instance will execute all data access operations in a transaction with
propagation level of TransactionDefinition.PROPAGATION_REQUIRES_NEW.
jdbcOperations - the JdbcOperations to usetransactionManager - the PlatformTransactionManager to usepublic JdbcOperationsSessionRepository(org.springframework.jdbc.core.JdbcOperations jdbcOperations)
JdbcOperationsSessionRepository instance which uses the
provided JdbcOperations to manage sessions.
The created instance will not execute data access operations in a transaction.
jdbcOperations - the JdbcOperations 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 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()
createSession in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>public void save(org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession session)
save in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>public org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession findById(java.lang.String id)
findById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>public void deleteById(java.lang.String id)
deleteById in interface org.springframework.session.SessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>public java.util.Map<java.lang.String,org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession> findByIndexNameAndIndexValue(java.lang.String indexName,
java.lang.String indexValue)
findByIndexNameAndIndexValue in interface org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.jdbc.JdbcOperationsSessionRepository.JdbcSession>public void cleanUpExpiredSessions()