0

We have 2 spring-boot(3.3.0) application

web-application

  • N instances
  • each of them are scheduled spring-session(jdbc) cleanup-cron job every minutes (relying to spring.session.jdbc.cleanup-cron)

scheduler-application

  • 1 instance
  • not web application (spring.main.web-application-type=none)

these applications connect to same database.

When we change to disable cleanup-cron on web-application and to enable cleanup-cron on scheduler-application, but JdbcIndexedSessionRepository was not created.

Because SessionAutoConfiguration is annotated @ConditionalOnWebApplication and not autoconfigured.

How can initialize JdbcIndexedSessionRepository at spring.main.web-application-type=none environment?


Current work-around is

  • to add spring-boot-starter-web to dependencies and works as web-application.
  • to add spring.main.lazy-initialization=false because JdbcIndexedSessionRepository#afterPropertiesSet never called and cron not scheduled.

scheduler-application

build.gradle

implementation("org.springframework.session:spring-session-jdbc")
+ implementation("org.springframework.boot:spring-boot-starter-web")

application.properties

+ spring.main.lazy-initialization=false
spring.session.store-type=jdbc
spring.session.timeout=24h
spring.session.jdbc.initialize-schema: never

2 Answers 2

1

You can define your own instance as a @Bean in a @Configuration class:

@Bean
JdbcIndexedSessionRepository jdbcIndexedSessionRepository(JdbcOperations jdbcOperations, TransactionOperations transactionOperations) {
    JdbcIndexedSessionRepository repository = new JdbcIndexedSessionRepository(jdbcOperations, transactionOperations);
    // any additional customization that's needed
    return repository;
}
0

refer to @AndyWilkinson 's answer, solved by below configuration

@Configuration
public class SessionConfig {

  @Lazy(value = false) // only when spring.main.lazy-initialization=true
  @Bean
  JdbcIndexedSessionRepository jdbcIndexedSessionRepository(
      JdbcOperations jdbcOperations, TransactionOperations transactionOperations) {
    return new JdbcIndexedSessionRepository(jdbcOperations, transactionOperations);
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.