You can't use duplicate on a CFC and get a CFC.  Instead, you get some
bizarre CFC/Struct hybrid.  You have to use the CFC in the application scope
directly, along with the locks.  However, if you lock the intantiation of
the component without requiring read locks, then you can pull all the CFLOCK
tags inside the CFC (when it manipulates instance data), so your code isn't
littered with CFLOCKs.

Basically, the idea of locking the instantiation is to ensure that no
request can get past the instantiation code without having an instance
available to it, and that multiple instances will never be instantiated.
Here's some code to do it:

<cflock scope="application" type="readonly" timeout="5">
  <cfset needToInstantiate = structKeyExists(application, "def") />
</cflock>
<cfif needToInstantiate>
  <cflock scope="application" type="exclusive" timeout="5">
    <cfif structKeyExists(application, "def")>
      <cfset application.dev = request.component("Model/Help/helpDef") />
    </cfif>
  </cflock>
</cfif>

If you need to instantiate multiple application variables, that'll work too.
You only need to check the existance of one of them (ideally a
single-purpose flag variable), and set them all in the same place as the
component instantiation.
  -----Original Message-----
  From: Jamie Jackson [mailto:[EMAIL PROTECTED]
  Sent: Thursday, October 30, 2003 11:25 AM
  To: CF-Talk
  Subject: CFC from App to Request scope?

  The following doesn't seem to work: I'm trying to get my object to
  persist, but (I think) I would prefer to access the object via the
  request scope to reduce the number of locks needed. If I call a method
  on request.def, it's missing instance variables (and throwing an
  error, since they're required by the method), whereas with the
  application.def, it works fine.

  <!--- Store an instance of the helpDef object in the app scope --->
  <cflock timeout="10" throwontimeout="No" type="exclusive"
  scope="APPLICATION">
    <cfif not isdefined("application.def")>
      <!--- calling CFC with Dan Switzer's "component" -- a relative
  pathing UDF, which works fine. --->
      <cfset application.def = request.component("Model/Help/helpDef")>
    </cfif>
    <!--- I'm unsure about the app scope locking needed in CFMX, so I'll
  access the object from the app using the request scope --->
    <cfif not isdefined("request.def")>
      <cfset request.def = duplicate(application.def)>
    </cfif>
  </cflock>

  I'm fairly new to CFCs, so any tips, critiques, or debunkings of my
  assumptions are welcome.

  Thanks,
  Jamie

[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to