【问题标题】:ColdFusion 8 - Application.cfc interferenceColdFusion 8 - Application.cfc 干扰
【发布时间】:2013-06-13 16:08:41
【问题描述】:

我刚刚遇到了一个客户网站的奇怪问题,我的实时域正在使用我的开发域应用程序设置。我以前没有遇到过这个问题,在Application.dsn 周围删除cflock(以及其他设置)解决了这个问题。

如前所述,我有一个实时站点 *www.* 和开发站点 *dev.*,我的开发站点位于实时站点 /dev/ 的子文件夹中,并且有自己的 Application.cfc

我的第一个问题是,如果我在 dev 文件夹中的应用程序范围中有变量,这会覆盖上面文件夹中的应用程序范围变量吗?它会认为它是相同的范围吗?如果是这样,那么这可能会解释问题所在,如果不是,那么我很难过。

我的第二个问题是,我应该如何更正 application.cfc 中的 cflock 应用程序范围变量?我应该这样做吗?

这是我的 Application.cfc,非常感谢您的建议:

<cfcomponent output="true">

<cfimport taglib="taglib" prefix="func">

<!---

        Check staging directory exists

--->

<cfset THIS.env = "staging">

<!---

        Set application vars

--->

<cfset THIS.applicationTimeout = createTimeSpan(0,0,0,0)>
<cfset THIS.sessionManagement="Yes">
<cfset THIS.clientManagement = true>
<cfset THIS.clientStorage = "cookie">
<cfset THIS.loginStorage = "cookie">
<cfset THIS.setDomainCookies = false>
<cfset THIS.setClientCookies = true>
<cfset THIS.scriptProtect = true>
<cfset THIS.secureJSON = true> <!--- Added 12.06.13 --->

<!---

        Check environment
        Set application name

--->

<cfif THIS.env EQ "staging">
    <cfset THIS.applicationName = "devenv">
    <cfset THIS.dsn = "devenv">
<cfelse>
    <cfset THIS.applicationName = "liveenv">
    <cfset THIS.dsn = "liveenv">
</cfif>

<cfif #cgi.HTTP_HOST# NEQ "localhost">
    <cfset THIS.dirpath = "http://#cgi.http_host#">
    <cfset THIS.componentPath = "cfcs.">
<cfelse>
    <cfset urlString = #mid(cgi.PATH_INFO, 2, 200)#>
    <cfset THIS.localhostFolderName = #spanexcluding(urlString, "/")#>
    <cfset THIS.dirpath = "http://localhost/#THIS.localhostFolderName#">
    <cfset THIS.componentPath = "#THIS.localhostFolderName#.cfcs.">
</cfif>
<cfset THIS.name = THIS.applicationName>
<cfset THIS.sessiontimeout = createtimespan(0,0,20,0)>
<cfset THIS.setClientCookies = true>
<cfset THIS.visitor = true>


<cffunction name="onApplicationStart" returntype="void">

    <cfset APPLICATION.name = THIS.applicationName>
    <cfset APPLICATION.dsn = THIS.dsn>
    <cfset APPLICATION.DSN = THIS.dsn>
    <cfset APPLICATION.dirpath = THIS.dirpath>
    <cfset APPLICATION.componentPath = THIS.componentPath>
    <cfif #cgi.HTTP_HOST# EQ "localhost">
        <cfset APPLICATION.localhostFolderName = THIS.localhostFolderName>
    </cfif>

    <!--- USED FOR PATHS AND URLS --->  

    <!--- Property image upload paths ---->
    <cfset APPLICATION.paths = StructNew()>

    <!---

            Check environment
            Set local root

    --->

    <cfif THIS.env EQ "staging">

        <cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\dev\">

    <cfelse>

        <cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\">

    </cfif>


    <cfset APPLICATION.paths.logs = APPLICATION.paths.localRoot & "logs\">
    <cfset APPLICATION.paths.logFile = APPLICATION.paths.logs & "site_log.txt">       
    <cfset APPLICATION.paths.property = StructNew()>
    <cfset APPLICATION.paths.property.image = APPLICATION.paths.localRoot & "images\property\">
    <cfset APPLICATION.paths.property.large = APPLICATION.paths.property.image & "large\">
    <cfset APPLICATION.paths.property.thumb = APPLICATION.paths.property.image & "thumbs\">
    <cfset APPLICATION.paths.property.cmsThumb = APPLICATION.paths.property.image & "thumbs\cms\">
    <cfset APPLICATION.paths.property.pdf = APPLICATION.paths.localRoot & "pdf\">
    <cfset APPLICATION.paths.property.pdfGenerated = APPLICATION.paths.property.pdf & "generated\">
    <cfset APPLICATION.newsUploadPath = APPLICATION.paths.localRoot & "images\news\">
    <cfset APPLICATION.articlesUploadPath = APPLICATION.paths.localRoot & "images\articles\">

    <cfset APPLICATION.articlesThumbsDir = "../images/articles/thumbs/">
    <cfset APPLICATION.articlesContentDir = "../images/articles/assets/">
    <cfset APPLICATION.articlesAssetsDir = "../articles/assets/">

    <!--- Site URLS ---->
    <cfset APPLICATION.urls = StructNew()>
    <cfset APPLICATION.urls.root = "http://" & CGI.server_name & "/">
    <cfset APPLICATION.urls.com = "com">
    <cfset APPLICATION.urls.tagLib = APPLICATION.urls.root & "taglib/">
    <cfset APPLICATION.urls.cms.tagLib = "http://" & CGI.server_name & ":" & CGI.server_port & "/admin/tagLib/">
    <cfset APPLICATION.RowsPerPage = 10>

    <!--- Property URLS --->
    <cfset APPLICATION.urls.property.pdf = APPLICATION.urls.root & "pdf/">
    <cfset APPLICATION.urls.property.image = APPLICATION.urls.root & "images/property/">
    <cfset APPLICATION.urls.property.large = APPLICATION.urls.root & "images/property/large/">
    <cfset APPLICATION.urls.property.thumb = APPLICATION.urls.root & "images/property/thumbs/">
    <cfset APPLICATION.urls.property.cmsThumb = APPLICATION.urls.root & "images/property/thumbs/cms/">
    <cfset APPLICATION.urls.news.image = APPLICATION.urls.root & "images/news/">
    <cfset APPLICATION.urls.articles.image = APPLICATION.urls.root & "images/articles/">

    <cflock scope="Application" timeout="5" type="Exclusive">

        <cfscript>

            /* Commonly used objects and queries */

            // DAOs 
            APPLICATION.propertyDAO = CreateObject("component", "cfcs.dataobjects.propertyDAO").init(APPLICATION.dsn);

            APPLICATION.propertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_imageDAO").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_imageDAO").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyLocationDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_locationDAO").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyTypeDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_typeDAO").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyTenureDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureDAO").init(APPLICATION.dsn);     
            APPLICATION.propertyGroupDAO = CreateObject("component", "cfcs.dataobjects.property_groupDAO").init(APPLICATION.dsn);     

            // Gateways
            APPLICATION.propertyGateway = CreateObject("component", "cfcs.dataobjects.propertyGateway").init(APPLICATION.dsn);
            APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn); 
            APPLICATION.propertyToPropertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_imageGateway").init(APPLICATION.dsn);
            APPLICATION.propertyLocationGateway = CreateObject("component", "cfcs.dataobjects.property_locationGateway").init(APPLICATION.dsn);
            APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn);
            APPLICATION.propertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
            APPLICATION.propertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_tenureGateway").init(APPLICATION.dsn);
            APPLICATION.propertyToPropertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureGateway").init(APPLICATION.dsn);
            APPLICATION.partnerGateway = CreateObject("component", "cfcs.dataobjects.partnerGateway").init(APPLICATION.dsn);

            // Business Objects
            APPLICATION.propertyBO = CreateObject("component", "cfcs.businessobjects.propertyBO").init(APPLICATION.dsn);

            // Common queries
            APPLICATION.qPartners = APPLICATION.partnerGateway.getAllRecords();    
            APPLICATION.qPropertyTypes = APPLICATION.propertyTypeGateway.getAllRecords(); 
            APPLICATION.qPropertyTenures = APPLICATION.propertyTenureGateway.getAllRecords(); 
            APPLICATION.qPropertyMinMaxSize = APPLICATION.propertyGateway.getMinMaxSize();
            APPLICATION.qPropertyLocations = APPLICATION.propertyLocationGateway.getAllRecords();

        </cfscript>
    </cflock>
</cffunction>
<cffunction name="onSessionStart" returntype="void">
    <cflock scope="Session" timeout="5" type="Exclusive">       
        <cfscript>
            SESSION.propertySearchCriteria = CreateObject("component", "cfcs.beans.property_search_criteria").init();
            SESSION.propertySearch = CreateObject("component", "cfcs.beans.property_search").init(SESSION.propertySearchCriteria);
        </cfscript>    
    </cflock>
</cffunction>

【问题讨论】:

  • 他们是否一直使用相同的应用程序名称?如果应用程序名称相同,那么它们将使用相同的范围。他们不应该使用不同的名称。
  • 当我设置 live / dev 环境变量测试时,它们可能暂时共享相同的应用程序名称。那肯定会解释这个问题。在 Application.cfc 中设置实时/开发测试的最佳方法是什么?
  • 只要它们具有不同的应用程序名称,那么它们将被视为完全独立的应用程序。所以我觉得你的逻辑没问题。至于锁定该代码块,我认为这也可以。我在我的应用程序中也这样做。
  • 我也是,但从技术上讲,您不必将应用程序变量锁定在 OnApplicationStart 中,除非您打算手动调用该方法。当 CF 调用它时,应用程序范围被自动锁定。但是,如果手动调用OnApplicationStart,CF 不会锁定应用范围。
  • 感谢@Miguel-F 的提示

标签: coldfusion cfml coldfusion-8 application.cfc cfimport


【解决方案1】:

为什么要根据应用程序所处的环境更改应用程序名称?这是没有意义的。该应用程序仍然是同一个应用程序,无论它是在生产中,还是您正在暂存或开发它。

同样,与源代码相同。您的实时网站中有您的开发网站吗?这有什么意义?

我不知道您的应用程序变量发生了什么,但它们绑定到应用程序名称 (this.name),而不是 Application.cfc 在目录结构中的位置。

我怀疑你的零 applicationTimeout 在这里没有多大帮助。这基本上意味着您的应用程序实际上永远不会持续存在......它将超时每个请求。这也没有意义。

您不需要&lt;cflock&gt; onApplicationStart() 中的那个代码。 ColdFusion 将只允许一个请求运行onApplicationStart():所有其他请求将排队等待它完成,此时应用程序已启动,因此排队的请求甚至不会尝试运行它。

我认为你的问题很可能是由于你糟糕的应用程序框架设计造成的,而且你正在做的奇怪的事情是让 dev 和 prod 在同一个源代码树中(这违背了所有逻辑)。

把你的源代码整理出来,问题就不会出现了。

【讨论】:

  • ColdFusion 将只允许一个请求运行 onApplicationStart() (这不是这里的主要问题,但是..)我的理解是只有当它被自动调用时才是真的在应用程序启动的正常过程中。如果您手动调用它,例如进行重置,它不会自动单线程。
  • 确实如此。而“这是我之前制作的”:cfmlblog.adamcameron.me/2012/07/…。我不想通过把它扔到上面来混淆事情。
  • 感谢@AdamCameron 的详细回答 - 为了回答您的问题,我根据应用程序所处的环境更改了应用程序名称,这样我就可以轻松地在实时和开发之间切换,而不必保留两个 Application.cfc 文件,显然现在是个坏主意,所以将回到拥有两个 App.cfc 的情况。由于托管限制,dev 位于同一文件夹树中。
【解决方案2】:

您可以使用条件逻辑来设置您的应用程序名称。

if ( YouAreHappy and YouKnowIt ) {
This.name = "ClapYourHands";
}
else {
This.name = "StompYourFeet"; 
}

在现实生活中,您的 if 条件将与实际环境和测试环境不同。 cgi.http_host 是一个不错的候选者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多