从CFC中包含CFM时的并发和范围问题

时间:2012-10-24 16:42:34

标签: coldfusion coldfusion-9 cfc

我将一个组件放在我的应用程序范围中,以便在所有请求中共享它,并且它包含一个cfm模板:

<cfcomponent output="false">

    <cffunction name="run" output="false" returntype="void">

        <cfset var tmp = false/>

        <cftry>
            <cfinclude template="inc.cfm"/>
            <cfcatch>
                <cffile action="append"
                        file="#ExpandPath("error.log")#"
                        output="ERROR: #cfcatch.message#"/>
            </cfcatch>
        </cftry>

    </cffunction>

</cfcomponent>

正在包含的模板只是创建一个数组并检查数组长度应该是什么,如果不是它写入error.log文件:

<cfset tmp = [
    "one",
    "two",
    "three"
]/>
<cfif ArrayLen(tmp) neq 3>
    <cffile action="append"
            file="#ExpandPath("error.log")#"
            output="Length = #ArrayLen(tmp)#"/>
</cfif>

如果我然后在它上面运行一个加载(100个并发线程),我会在error.log文件中显示以下项目...

ERROR: element at position 3 of array variable &quot;___IMPLICITARRYSTRUCTVAR0&quot; cannot be found.
Length = 0
Length = 2

注意我在Java 1.7.0_09上使用ColdFusion 9.0.1.274733。我在相同的JRE上测试过Railo并且工作正常。


附加以下内容也会导致问题,将tmp变量更改为结构,并在variables范围内添加一个未在任何位置引用的随机项...

<cfcomponent output="false">

    <!--- 
    Some random variable that does nothing with the exception
    of being the facilitator of my eternal pain
    --->
    <cfset variables.t = {}/>

    <cffunction name="run" output="false" returntype="void">

        <cfset var tmp = {}/>

        <cftry>
            <cfinclude template="inc2.cfm"/>
            <cfcatch>
                <cffile action="append"
                        file="#ExpandPath("error.log")#"
                        output="ERROR: #cfcatch.message#"/>
            </cfcatch>
        </cftry>

    </cffunction>

</cfcomponent>

其中包含一个与第一个非常相似的模板,看起来像这样......

<cfset tmp.arr = [
    "one",
    "two",
    "three"
]/>
<cfif ArrayLen(tmp.arr) neq 3>
    <cffile action="append"
            file="#ExpandPath("error.log")#"
            output="Length = #ArrayLen(tmp.arr)#"/>
</cfif>

如果删除variables范围内的项目,则可以正常工作。如果您在模板中转储#variables##local#,那么一切都在您期望的位置。


(从评论中更新)

我之后将此问题提升为bug #3352462

3 个答案:

答案 0 :(得分:4)

这是基于彼得/你自己的评论。

有一些关于数组和结构简写概念的错误,自从CF8引入了语法以来。 Adobe修复它们的方法有点像打击鼹鼠,而不是努力将问题排序一次,并且正确。看起来你已经找到了另一个例子。看看它是否仍然存在于CF10中会很有趣,因为我知道它们在开发周期中修复了一些。

解决这个问题的唯一方法是在你看到这些问题的情况下不要使用这种表示法。

你可以为此提出一个bug,以便Adobe了解它吗?

还值得注意,但与此处的具体问题无关:Java 1.7尚不支持CF.你可能想要记住这一点。

答案 1 :(得分:0)

您可以考虑在标记周围添加一些锁定,以确保只允许一个请求修改文件。

答案 2 :(得分:0)

在你的第一个例子中,如果你将tmp声明为数组而不是布尔值会有什么不同吗?

<cfset var tmp = ArrayNew(1) />

而不是......

<cfset var tmp = false />
相关问题