用户数据有时会混淆



我正在构建一个网站,在那里我遵循MVC来管理我的代码,而不使用任何框架。我已经把我所有的查询都放在了cfc中,并在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>  

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

<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。这意味着它被设置到CFC实例的变量范围中。这意味着多个线程可以覆盖此值。正如我所展示的,通过对变量进行变量化,可以使变量成为函数的局部变量,因此对该函数的每次调用都会创建一个局部变量,从而实现线程安全。

基本上,您应该根据习惯对函数中的所有局部变量进行var。在我工作过的任何地方,这个代码都不会通过代码审查。

您实际上并没有包含相应的代码来回答这个问题。。。其将是CCD_ 1内的代码。

然而,我假设您没有在其中对所有变量进行VARed,这意味着它们进入CFC的变量范围,该范围与应用程序中的每个用户共享。

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

最新更新