Understanding Application And Session Timeouts In ColdFusion

Posted July 29, 2009 at 5:02 PM

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

 Launch code in new window » Download code as text file »

  • <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

 Launch code in new window » Download code as text file »

  • <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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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 »
7,572 Comments

@KRD,

No problem :)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »