Why is a ColdFusion SESSION variable “undefined” after being referenced a few lines before?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 09:38:30

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21143449/why-is-a-coldfusion-session-variable-undefined-after-being-referenced-a-few-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!