为什么 ColdFusion SESSION 变量是“未定义的"?在被引用了几行之后? [英] Why is a ColdFusion SESSION variable "undefined" after being referenced a few lines before?
问题描述
在 Windows2003/IIS6 上运行 ColdFusion 8.01 Standard
Running ColdFusion 8.01 Standard on Windows2003/IIS6
Application.cfc:
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>
模板文件伪代码示例:
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 在 Application.cfc
的 onRequestStart()
中设置(如果尚未定义).上面的代码在模板文件中运行.对 SESSION.User
的第二次引用引发了异常,并显示消息 Element USER is undefined in SESSION
.
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
.
为什么 SESSION.User 会在几行之前定义(不抛出异常),然后在几行之后(毫秒内)抛出这个异常?
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?
推荐答案
这很可能是线程安全问题,您的代码中的其他内容清除了会话范围或将 NULL 分配给 SESSION.User
.
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
的本地声明,因此该代码不是线程安全的 - 因此您的代码中的其他地方可能会出现类似的错误.
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.
这篇关于为什么 ColdFusion SESSION 变量是“未定义的"?在被引用了几行之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!