As most ColdFusion developers know, you cannot reference ‘application’ scoped variables directly in Application.cfc’s onSessionEnd() method. Instead you need to use the ApplicationScope parameter. Notes from the docs:
You must use the ApplicationScope parameter to access the Application scope. You cannot reference the Application scope directly; for example, use Arguments.ApplicationScope.myVariable, not Application.myVariable. Use a named lock when you reference variables in the Application scope, as shown in the example.
I ran into this today when working on some code a colleague wrote which was firing a method in an application scoped component, the code was as follows:
Obviously this method is being executed when the users session has expired. The use of arguments.AppScope means I can access the ‘myCFC’ component which is stored in application scope.
Now to the actual problem…inside the cfc was a method which was logging information in the database, but the datasource value was coming from application scope.
Obviously arguments should be passed to components instead of being accessed directly, but that’s another story
I would have thought this would work fine, but no luck. It was a little bit of a pain to debug as you can’t see the results of onSessionEnd() being run.
The fix was simply to pass in the required variables as arguments to the method:
Either way, be careful when doing this particularly if the method is being called from multiple application points and one of those could be onSessionEnd().
“Obviously arguments should be passed to components instead of being accessed directly, but that’s another story”
That is another story, and I don’t mean to sound harsh, but you might have wanted to fix the whole component while you were at it. A simple init() function which sets the dsn in the variables scope when the component is instantiated would have done the trick and ensured any other calls would not have the same issue.
todd sharp
September 28th, 2007
@Todd - Not harsh at all Todd, I appreciate the feedback. In hindsight that would have been the better approach, definitely!
I must admit that sometimes when pressed for time (HUGE deadline on this one) and looking at someone elses code I can get caught up looking at the actual problem and not stepping back and looking at the bigger picture.
That is something else to work on
Michael Sharman
September 29th, 2007