1

我正在建立一个网站,我在其中遵循 MVC 来管理我的代码,而不使用任何框架。我已将所有查询放入 cfcs 并在我的 Application.cfm 中对其进行初始化,并将它们存储在如下应用程序变量中:

<cfset aplication.customerProfileObject=   
                createObject("component","cfc.customerprofile").init()>

为了执行任何查询操作,我创建了一个函数,然后像这样在任何地方调用它:

<cfset selectedCustomerOb =   
      application.customerProfileObject.getContactCustomerProfileDetail(session.userid)>

我不知道是什么导致了这个问题,但有时一个用户会访问另一个用户的数据。这怎么可能?它是在评估另一个用户的会话数据还是我初始化了 cfc 错误?

应用设置如下:

<cfapplication name="MyDataSourceName" 
           sessionmanagement="Yes"
           setclientcookies="yes"
           setdomaincookies="yes"
           loginstorage="session"
           sessiontimeout="#CreateTimeSpan(0, 2,0,0)#">

客户资料.cfc

<cfcomponent>
    <cffunction name="init">
        <cfreturn this> 
    </cffunction>

    <cffunction name="getContactCustomerProfileDetail" returntype="query"         
            description="Returns customer contact details by contactid" 
            access="public">
        <cfargument name="ccId" type="numeric" required="yes"> 

        <cfquery name="getContactCustomerProfileDetail" 
                  datasource="#Application.ds#" 
                  dbtype="ODBC" 
                  username="#Application.UserName#" 
                  password="#Application.Password#">
            <!-------My query here--->
        </cfquery> 

        <cfreturn getContactCustomerProfileDetail>

    </cffunction>

</cfcomponent>  
4

2 回答 2

4

正如亚当所说,你需要这样做: -

<cffunction name="getContactCustomerProfileDetail" returntype="query"         
        description="Returns customer contact details by contactid" 
        access="public">
    <cfargument name="ccId" type="numeric" required="yes">

    <cfset var getContactCustomerProfileDetail = false>

    <cfquery name="getContactCustomerProfileDetail" 
              datasource="#Application.ds#" 
              dbtype="ODBC" 
              username="#Application.UserName#" 
              password="#Application.Password#">
        <!-------My query here--->
    </cfquery> 

    <cfreturn getContactCustomerProfileDetail>

</cffunction>

您遇到问题的原因是因为您的 CFC 实例位于共享范围(应用程序)中,并且您没有对查询变量进行 var'd。这意味着它被设置到 CFC 实例的变量范围内。这意味着多个线程可以覆盖这个值。通过像我展示的那样对变量进行 varring,您可以使变量成为函数的本地变量,因此对该函数的每次调用都会创建一个本地化的并且因此是线程安全的变量。

基本上你应该习惯性地改变函数中的所有局部变量。在我工作过的任何地方,这段代码都不会通过代码审查。

于 2013-12-16T09:13:40.510 回答
3

您实际上并没有包含代码的相关位来回答这个问题......这将是getCustomerProfileDetail().

但是,我假设您没有在其中包含所有变量 VAR,这意味着它们进入 CFC 的变量范围,该范围与应用程序中的每个用户共享。

但是,正如我所说,你没有给我们正确的信息来真正准确地回答这个问题。我建议更新您的问题以包含相关代码。

于 2013-12-15T14:16:07.520 回答