5
votes

Running ColdFusion 8.01 Standard on Windows2003/IIS6

Application.cfc:

<cfcomponent output="false">
    <cfscript>
        THIS.SessionManagement = "Yes";
        THIS.SessionTimeout = CreateTimeSpan(0, 3, 0, 0);
        THIS.ApplicationTimeout = CreateTimeSpan(0, 8, 0, 0);
    </cfscript>

    <cffunction name="onRequestStart" returnType="Boolean" output="false">
        <cfargument name="targetPage" type="string" required="true">

        <cfscript>
            if  (!StructKeyExists(SESSION, "User"))
                SESSION.User = CreateObject("component", "cfc.User");
        </cfscript>
    </cffunction>
</cfcomponent>

Template file Pseudo-Code Sample:

    LOCAL.qItems =
        CreateObject(
                "component",
                "cfc.Items"
                ).setUser(SESSION.User).getItems();

    for (i=1; i<=LOCAL.qItems.RECORDCOUNT; i++) {
        LOCAL.Item =
            CreateObject(
                "component",
                "cfc.Item"
                ).setUser(
                    SESSION.User
                    ).setId(LOCAL.qItems["Sku"][i]);
    }

SESSION.User is set (if not already defined) in onRequestStart() of Application.cfc. The above code runs in a template file. The second reference to SESSION.User has thrown an exception with the message Element USER is undefined in SESSION.

Why would SESSION.User be defined (doesn't throw an exception) a few lines before, and then throw this exception a few lines later (within milliseconds)?

This happens maybe once a day in different templates throughout my application.

How can I prevent this?

2
Not sure what your problem is, but you should really be init-ing your session variables in onSessionStart(), not onRequestStart(). - Adam Cameron
Maybe some users are working in your site with multiple tabs/windows, maybe one tab is doing something, but then they sign-out in another, thus destroying your session vars. Or maybe after they sign-out in one tab they go back to a previous one and attempt some kind of action. In my CF app if a user signs-out all other tabs are forced to sign out as well and return to the sign-in page. - gfrobenius

2 Answers

7
votes

It's most likely a thread safety issue with something else in your code clearing out session scope or assigning NULL to SESSION.User.

I suggest that because you don't seem to have a local declaration for i in your loop, so that code is not thread safe - and so you may have similar errors elsewhere in your code.

1
votes

I'd put this line "SESSION.User = CreateObject("component", "cfc.User");" into onSessionStart() then it will run when each users session is first initiated.