Controlling JDBCSession Timeout

Published by Bilal Kaun on

You might have noticed that Spring Boot’s server.session.timeout does not apply if JDBCSession library is autoconfigured. Which means, the standard way to set default session timeout no longer applies.

The appropriate way to set the timeout in this case is through the @EnableJdbcHttpSession annotation’s maxInactiveIntervalInSeconds like so.

// Set the default session timeout from 1800 (the library's default) to 5 minutes (as an example)
@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = 300)

This is fine if you don’t mind moving the config magic values into the code, however all our properties are injected through a secrets and property management solution, including the server.session.timeout property. Thankfully the underlying JdbcOperationsSessionRepository class allows setting the default the maxInactiveIntervalInSeconds. So the solution is to bind to the ApplicationReady event and set variable.

@Value("${server.session.timeout:1800}") 
private Integer serverTimeout

jdbcOperationsSessionRepository.setDefaultMaxInactiveInterval(serverTimeout);

Github Hosted Example

https://gist.github.com/bilalkaun/eb6f4ab718ae208a881d70e061520514

If the embedded gist is not displaying, visit here: https://gist.github.com/bilalkaun/eb6f4ab718ae208a881d70e061520514


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *