25

In Tomcat there is a well known configuration option in conf/context.xml to disable session persistence:

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<Manager pathname="" />

When uncommented as shown here, the applied implementation of org.apache.catalina.Manager (e.g. org.apache.catalina.session.StandardManager) does not have a pathname to tell it where to store sessions to the disk, and thus it does not write session files to disk (e.g. on shutdown), which is what we want.

In other words, this disables the standard Tomcat feature to sustain sessions through server restart.

How can the same be achieved in Spring Boot with embedded Tomcat?

Perhaps the Manager object can somehow be obtained to set the property pathname to null?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jamesbond007
  • 598
  • 1
  • 5
  • 13

4 Answers4

18

You can use a TomcatContextCustomizer to access the manager and apply the necessary configuration:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addContextCustomizers(new TomcatContextCustomizer() {

        @Override
        public void customize(Context context) {
            if (context.getManager() instanceof StandardManager) {
                ((StandardManager) context.getManager()).setPathname("");
            }
        }
    });
    return tomcat;
}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
11

This behavior can be customized via application.properties:

server.servlet.session.persistent=false # Whether to persist session data between restarts.

Session persistence is disabled by default in Spring Boot 2.x.

Thomas
  • 2,231
  • 1
  • 19
  • 27
  • 3
    The [v2.1.4 docs](https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/html/common-application-properties.html) support what you're saying about this being disabled by default, however, my session deserialization errors disappeared only after explicitly setting this property to false. It appears the docs are out of step with the code. – chaserb Sep 11 '20 at 23:46
  • Same here, I had to set this explicitly to disable session persistence. It does not seem to be disabled by default. – Christoph Jul 07 '22 at 10:10
2

... and this for Spring Boot 2.0.x

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addContextCustomizers(new TomcatContextCustomizer() {

        @Override
        public void customize(Context context) {
            if (context.getManager() instanceof StandardManager) {
                ((StandardManager) context.getManager()).setPathname("");
            }
        }
    });
    return tomcat;
}
mtbadi39
  • 448
  • 6
  • 18
1

Or like this, in case you are using application.yml:

# Whether to persist session data between restarts.
server:
  servlet:
    session:
      persistent: false
ocarlsen
  • 1,312
  • 2
  • 16
  • 20