3

I have this configuration in my application.yml

server:
    contextPath: /appname
    session:
        timeout: 7200  # 2 hours in seconds

This works fine when i run in Intellij IDEA, but when i deploy the build .war file to a tomcat instance this is ignored. Is this a bug or is it not expected to work like this?

Also i seem to be unable to locate a specification of what can be written in application.yml. Anyone know where this can be found?

How about the application.groovy config file? Cant seem to locate a specification for this?

My environment:

  • Grails version: 3.2.8
  • Gradle version: 3.4.1
  • Intellij IDEA version: 2017.1.2
  • Tomcat version: 8.0.26 JDK Version: 1.8.0_45
Clijsters
  • 4,031
  • 1
  • 27
  • 37
user3728821
  • 35
  • 1
  • 5
  • http://stackoverflow.com/questions/43215649/grails-3-session-timeout-not-working-when-deployed-to-tomcat seems like a similar question. Basically application.yml unsure of a specification, depends on plugin used and so forth. In short appilcation.groovy can be created in the same path/folder as application.yml. Your application will read both files and you can configure it in the yml style or in the older .groovy style which was `some.element.key=value` – V H Apr 27 '17 at 18:31
  • This is indeed the same problem. I will test if it works if i move it to the .groovy file instead. I an still thinking that this is a bug. Any thoughts on that? – user3728821 Apr 28 '17 at 07:27
  • the answer on that page states you have to actually configure tomcat for that - so doubt it's a bug – V H Apr 28 '17 at 16:08
  • This is something that have allways been configurable in config.groovy (in grails v2.x) and i would think that if this have been removed, it should be stated in the migrationguide. This is why i would suspect it being a bug. – user3728821 Apr 29 '17 at 11:14
  • Yes but grails 2 was not using spring boot, entirely new technology in grails 3. You should read the comments at bottom of answer which point to this thread http://stackoverflow.com/questions/28103852/spring-boot-session-timeout – V H Apr 29 '17 at 11:17
  • I do realise that i can program a solution to this. It just seems a tad too excessive to have to do an implementation of this. I just got around to experiment with setting it in the application.groovy configfile instead and it seems to work. I have tried to set server.session.timeout = 50 and a timeout occurs after 50 secs. It just seems odd that the two application.xx configfiles dosnt offer same functionality. – user3728821 Apr 29 '17 at 11:27

3 Answers3

2

When you deploy a Grails 3 app to a standalone tomcat application you should not use springboot server.session.timeout configuration property. That it is only for an embeedded server.

Spring boots - sever.session.timeout - Embedded Server configuration

To configure a session timeout in a SpringBoot app (Grails 3 app is built on top of SpringBoot app) deployed into a standalone tomcat you have two choices:

A) Timeout for every app deployed in that tomcat instance.

You could edit the session timeout directly in tomcat configuration files:

$TOMCAT_HOME/conf/web.xml

Look out for the block:

<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly   -->
<!-- created sessions by modifying the value below.   -->

 <session-config>
     <session-timeout>30</session-timeout>
 </session-config>

B) You can add a web.xml file in your Grails 3 app, with the timeout you need per app.

Create a file in the path 'src/main/webapp/WEB-INF/web.xml' with the content:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <session-config>
     <session-timeout>30</session-timeout>
 </session-config>
</web-app>
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
0

C) You can also use tomcat's HttpSession setMaxInactiveInterval(seconds) method to set in your Groovy code.

if (grailsApplication.config.getProperty("session.timeout")?.isInteger())
    // session timeout in seconds
    session.setMaxInactiveInterval(grailsApplication.config.session.timeout as int)
0

Note that with the (current latest) Grails 5.x and spring boot 2.5 the correct property name is server.servlet.session.timeout and hence the application.yml config would go like this:

server:   
    servlet:
      session:
        timeout: 3600  #seconds

Spring boot docs: https://docs.spring.io/spring-boot/docs/2.5.5/reference/html/application-properties.html#application-properties.server.server.servlet.session.timeout

Radim
  • 11
  • 2