Understanding Application And Session Timeouts In ColdFusion

Posted July 29, 2009 at 5:02 PM by Ben Nadel

Tags: ColdFusion

Earlier today, I blogged about ColdFusion 9's new ApplicationStop() method which stops the current application, forcing OnApplicationStart() to be called on the next page request. During my exploration of this new method, I found out that the given Application was reset independently of the Session; meaning, while the Application scope was wiped out and repopulated, the existing Session scopes were left unchanged. When I observed this, I thought for sure it was a bug; but, after testing relative Application and Session timeout values, it appears to be the default behavior.

To test the inherent behavior of Application and Session timeouts, I set up a simple Application.cfc file session management:

Application.cfc

  • <cfcomponent
  • output="false"
  • hint="I define the application settings and event handlers.">
  •  
  • <!---
  • Define the application settings. Notice that our
  • Application timeout is rather small - 10 seconds,
  • while our Session timeout is larger - 5 minutes.
  • --->
  • <cfset this.name = hash( getCurrentTemplatePath() ) />
  • <cfset this.applicationTimeout = createTimeSpan( 0, 0, 0, 10 ) />
  • <cfset this.sessionManagement = true />
  • <cfset this.sessionTimeout = createTimeSpan( 0, 0, 5, 0 ) />
  •  
  • <!--- Define the request settings. --->
  • <cfsetting showdebugoutput="false" />
  •  
  •  
  • <cffunction
  • name="onApplicationStart"
  • access="public"
  • returntype="boolean"
  • output="false"
  • hint="I initialize the application.">
  •  
  • <!--- Initialize the application settings. --->
  • <cfset application.dateInitialized = now() />
  •  
  • <!--- Return true so that the page can load. --->
  • <cfreturn true />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="onSessionStart"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="I initialize the session.">
  •  
  • <!--- Initialize the session settings. --->
  • <cfset session.dateInitialized = now() />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

As you can see, the application timeout is a very small number, only 10 seconds. The session timeout, on the other hand, is much larger, at 5 minutes. Each of the scopes (Application and Session) has an event handler which defines an initialization timestamp. I am using these two timestamps to track the effects of the application timeout on the session timeout in the index file:

Index.cfm

  • <cfoutput>
  •  
  • <h1>
  • Application And Session Overview
  • </h1>
  •  
  • <p>
  • Application initialized:
  • #dateDiff(
  • "s",
  • application.dateInitialized,
  • now()
  • )#
  • seconds ago.
  • </p>
  •  
  • <p>
  • Session initialized:
  • #dateDiff(
  • "s",
  • session.dateInitialized,
  • now()
  • )#
  • seconds ago.
  • </p>
  •  
  • </cfoutput>

As you can see, this test code simply outputs the relative age (in seconds) of each scope - Application and Session - based on the DateInitialized property defined in the Application.cfc. When I run this page for the first few time, I get the following output:

Application And Session Overview

Application initialized: 4 seconds ago.
Session initialized: 4 seconds ago.

As you can see, each scope is the same age.

Then, I waited for more than 10 seconds, waiting for the Application to timeout, and refreshed the page. Here is the output I got:

Application And Session Overview

Application initialized: 0 seconds ago.
Session initialized: 107 seconds ago.

As you can see, the Application scope timed out and refreshed itself on this subsequent page request. However, the Session scope, which had a much higher timeout, did not get altered in anyway.

When I saw the behavior of ApplicationStop(), I was sure that it was a bug; however, based on the testing above, which was done in ColdFusion 8, it appears that the complete independence of the Application scope from the Session scopes is the default behavior of the ColdFusion framework. Now, I am not suggesting that you make it a habit of keeping your session timeouts higher than your application timeouts - it wouldn't make any sense; but, this proves that the ApplicationStop() behavior in ColdFusion 9 is in proper alignment with the default ColdFusion framework behavior.




Reader Comments

Jul 30, 2009 at 2:10 AM // reply »
5 Comments

After such as you said simple Application.cfc file session management I understood, that I knew very little about it


Aug 25, 2009 at 5:09 AM // reply »
1 Comments

Thanks for the article, after going through the article I could understand the difference of the scopes.


KRD
Feb 1, 2010 at 11:16 PM // reply »
2 Comments

This example fails with the following error:

Element DATEINITIALIZED is undefined in APPLICATION.


The error occurred in C:\ColdFusion8\wwwroot\PearlDomains\vdoms\storyofjesus_COM\aaa_Application_Timeout_Example\cf~index.cfm: line 11

9 : #dateDiff(
10 : "s",
11 : application.dateInitialized,
12 : now()
13 : )#


KRD
Feb 1, 2010 at 11:19 PM // reply »
2 Comments

Opps. Should have named application.cfm applications.cfc

Force of habit !


Feb 1, 2010 at 11:23 PM // reply »
11,246 Comments

@KRD,

No problem :)


Apr 28, 2010 at 11:55 AM // reply »
5 Comments

If a web application will always be in use, will one always need to use applicationTimeout?
Is it this needed in all apps? Or are there times when its not needed?


Apr 28, 2010 at 11:59 AM // reply »
11,246 Comments

@Nich,

You can't *not* use it - you don't have a choice. If you don't define it in the Application.cfc, ColdFusion will use the default value defined in the ColdFusion administrator.

There is no sense of not needing it really; if your application is busy, then your application simply doesn't time out. The defined timeout will never have a negative effect on an often-used application.


May 30, 2010 at 1:20 PM // reply »
2 Comments

Hey Ben,

Perhaps you can help me out with a couple of things...

1) My application is Flex housed in CF. I'm generating XML within my CF pages and loading then into my Flex applications... Some users are encountering and odd behavior that renders some application variables (objects) as undefined here and there. I can't duplicate it enough to understand if it's a session issue or what...

2) Should Application timeout and Session timeout be the same length? Or...


Jun 7, 2010 at 10:33 PM // reply »
11,246 Comments

@DColumbus,

First off, your application timeout is typically much larger than your session timeouts; but, it doesn't have to be. If you have a high-traffic site, you could leave your application timeout lower and it would still run fine. The whole point of the high timeout is so that the app doesn't have to load often (which would hold-up user experiences).

As far as the errors, is anything showing up in the Application log files? Or is this something showing up in the FLEX app. What I am wondering is if the error is in the FLEX side or in the CF side. I don't know too much about FLEX, so I wouldn't have much insight on that side.


Jul 23, 2010 at 9:12 PM // reply »
2 Comments

Ben,

I'm getting a lot more users on my system and the application seems to be bogging down my system.

The session timeout is set to 2 hours and the application timeout set to 1 day. If I lowered my session timeout to 15 minutes and my application to 20 minutes, will that solve my issue? Is that a reasonable timeframe?


Jul 25, 2010 at 5:19 PM // reply »
11,246 Comments

@Dcolumbus,

Changing your application timeout shouldn't make a difference since the memory usage of your application doesn't really fluctuate; meaning, there typically isn't anything cached in the application scope that is related to the individual sessions.

Sessions, on the other hand, are directly related to how much memory is used on the server since each session implies more variables used. Lowering yous session timeout should help the matter (by allowing stagnant sessions to release their memory much sooner).


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools