Coldfusion - 组件未定义

Coldfusion - Component undefined

我在我的 ColdFusion 应用程序中相当广泛地使用组件,并且 运行 经常出现我的组件未定义的错误,尽管它们肯定是已定义的。

我正在 Application.cfc 文件中定义组件。

 <cfset cfcList = "component1,component2,component3">
 <cfloop list="#cfcList#" index="local.thisCfcName">
      <cfset application.cfc[local.thisCfcName] = createObject(
        "component",
        "#application.cfcPath##local.thisCfcName#"
      )>
  </cfloop>

我能够确定的是:在我的 onRequestStart 函数中,我在遇到 'init' url 变量时重新启动应用程序:

<cfif structKeyExists(url, "init")>
  <cfset onApplicationStart()>
</cfif>

似乎在应用程序重新初始化时,任何其他尝试访问该组件的用户都会收到以下错误(失败的组件可能有所不同)。

[diag]=Element CFC.COMPONENT1 is undefined in a Java object of type class [Ljava.lang.String; referenced as ''

有没有我做错的地方可以解决这个问题?我每天多次收到这些错误,并且必须有解决方案,但我无法找到有关此特定问题的任何信息。

要回答您的问题,您几乎可以肯定 运行 race conditions. CF only locks the application scope when the application first starts

Runs when ColdFusion receives the first request for a page in the application

之后,如果您显式调用 OnApplicationStart()

... ColdFusion does not start the application; it does execute the method code, but does not lock the Application scope while the method executes.

因此任何其他 thread/request 尝试从应用程序范围读取数据都可能会出错,因为第一个线程仍在同时修改范围。您需要一个 exclusive application cflock 来防止任何其他线程在修改完成之前访问范围。

顺便说一句,虽然它不会完全消除竞争条件的可能性,但使用单独的变量进行初始化并仅在完成时分配,会减少 window 发生冲突的机会:

 <cfloop ...> 
     <cfset local.someVariable[key] = ....>
 </cfloop>

 <cfset application.someVariable = local.someVariable>

说了这么多,我同意你绝对不想想要如此频繁地刷新应用程序范围。