I have my application running with spring web mvc framework without spring boot. Now I want to use spring session JDBC to store the session to the database used by the app. All the examples I found online are using spring boot, and if not using spring boot, the datasource config they use are EmbeddedDatabase
like this:
@Bean
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("org/springframework/session/jdbc/schema-h2.sql").build();
}
I have my datasource configuration using HikariCP and I want the spring session to use this datasource config.
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
config.setJdbcUrl(env.getRequiredProperty("jdbc.url"));
config.setUsername(env.getRequiredProperty("jdbc.username"));
config.setPassword(env.getRequiredProperty("jdbc.password"));
config.setMinimumIdle(env.getRequiredProperty("jdbc.pool.minimumIdle", Integer.class));
config.setMaximumPoolSize(env.getRequiredProperty("jdbc.pool.maximumPoolSize", Integer.class));
config.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("jdbc.prop.cachePrepStmts"));
config.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("jdbc.prop.prepStmtCacheSize"));
config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("jdbc.prop.prepStmtCacheSqlLimit"));
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
How can I use my current configuration to integrate with spring session?
As I understand spring-session javaconfig-jdbc sample / doc, you "just" need to:
Annotate "your config class" (YourConfig
) with org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession
.
Name your DataSource
"dataSource". (done!;)
Provide a PlatformTransactionManager
bean, basing on dataSource
in YourConfig
.
(in a servlet environment - as yours) Introduce an AbstractHttpSessionApplicationInitializer
(in class path) referencing YourConfig
:
public class Initializer extends org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer { // <1>
public Initializer() {
super(YourConfig.class); // <2>
}
}
If you wish to install the db schema manually or with an external tool, the SQL scripts are located in spring-session.jar(!org/springframework/session/jdbc/schema-@@platform@@.sql) file or respectively in the source code repository.
These (application.)properties allow further customization:
# Session store type. [jdbc|redis|hazelcast|mongodb]
spring.session.store-type=jdbc
# Session timeout. If a duration suffix is not specified, seconds will be used.
server.servlet.session.timeout=
# Database schema initialization mode. [alwys | never | embedded]
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.(see: https://github.com/spring-projects/spring-session/tree/master/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc)
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql
# custom spring session table name (see : https://github.com/spring-projects/spring-session/issues/1230)
spring.session.jdbc.table-name=SPRING_SESSION
and the currently provided platforms are:
db2
derby
h2
hsqldb
mysql
oracle
postgresql
sqlite
sqlserver
sybase