If you are doing this per request, then persisting the object in server
scope - (or better in application scope if it's application-specific) so it
doesn't have to be *instantiated* on every request - is totally fine. You
just need to make very sure you var the variables you use in the function,
and also reference arguments in the function as "arguments.argumentName" and
not argumentName. You'd call that function per request - and no locking
would be required.
In other words, if your object isn't stateful, your fine, as long as you var
scope the local variables.
What you need then is a singleton, that gets instantiated once, remains
persistent in the application scope, and that would best be double locked to
make sure there's only one, like so:
<cfif NOT StructKeyExists(application,"Copycat")>
<cflock name="CopycatLock" Timeout="10" Type="Exclusive">
<cfif NOT StructKeyExists(application,"Copycat")>
<cfset application.Copycat =
CreateObject('component','model.Copycat').init()>
</cfif>
</cflock>
</cfif>
Then within Copycat, in your doSomethingPerRequest method, var your local
variables and reference all arguments with the scoped "arguments." notation
(just to be on the safe side).
<cffunction name="doSomethingPerRequest" output="false">
<cfargument name="something" />
<cfargument name="somethingElse" />
<cfset var somethingMore = "" />
<cfset somethingMore = arguments.something & arguments.somethingElse />
<cfreturn somethingMore />
</cffunction>
Then if you call
application.copycat.doSomethingPerRequest(attributes.something,somethingElse
), all the variable references in doSomethingPerRequest will die with each
request. The only thing you might need to think thru a little more carefully
is if you are passing session or application scoped variables into the
method and manipulating them directly within the method to change their
values, but i assume that's evident.
To make absolutely sure that all your variables are var scoped, use a
dumpMe() function in your object ...
<cffunction name="dumpMe" access="public" returntype="any" output="yes">
<cfdump var="#variables#" />
</cffunction>
And look carefully to see if you missed any of the parameters you should
have set as local to the function, like the iterator in loops, or query
names. They will show up persisted within the variables scope of the object.
Obviously, you want to call dumpMe() after all of the methods in the object
have run.
<cffunction name="doSomethingPerRequest" output="false">
<cfargument name="something" />
<cfargument name="somethingElse" />
<cfset var somethingMore = "" />
<cfset var i = "" />
<cfset somethingMore = arguments.something & arguments.somethingElse />
<cfloop index="i" from="1" to="#Len(somethingMore)#">
...
</cfloop>
<cfreturn somethingMore />
</cffunction>
If you're trying to persist variables across requests, like in a multi-step
form process, then that's another story. You should use the session scope
for that. Use can probably use a simple test to ensure a singleton:
<cfif NOT StructKeyExists(session,"Copycat")>
<cfset session.Copycat =
CreateObject('component','model.Copycat').init()>
</cfif>
because it's highly unlikely you'll get concurrent requests from a single
user, and then use a setSomethingPerRequest() and a
getSomethingPerRequest(), and set the variables you want to persist between
calls to the variables scope in setSomethingPerRequest().
<cffunction name="setSomethingPerRequest" output="false">
<cfargument name="something" />
<cfargument name="somethingElse" />
<cfset variables.somethingMore = arguments.something &
arguments.somethingElse />
</cffunction>
<cffunction name="getSomethingPerRequest" output="false">
<cfreturn variables.somethingMore />
</cffunction>
I hope that makes sense.
:) nando
>-----Original Message-----
>From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
>Behalf Of Ed Griffiths
>Sent: Thursday, December 01, 2005 3:22 PM
>To: [email protected]
>Subject: [CFCDev] Storing objects in server scope
>
>
>Hi,
>
>Noob question - we're working on an application which uses a CFC method to
>copy variable values from various scopes - pass the variable name as an
>argument and the method returns the corresponding value.
>
>If we were to load this CFC into the Server scope, would we need to
>explicitly lock the method? Does CF implement some kind of implicit locking
>or internal request sequencing within the Server scope to prevent race
>conditions occurring, and if not, how would you recommend that we implement
>a suitable locking technique that wouldn't introduce a significant
>performance hit?
>
>Thanks for any advice.
>
>
>
>
>----------------------------------------------------------
>You are subscribed to cfcdev. To unsubscribe, send an email to
>[email protected] with the words 'unsubscribe cfcdev' as the
>subject of the email.
>
>CFCDev is run by CFCZone (www.cfczone.org) and supported by
>CFXHosting (www.cfxhosting.com).
>
>An archive of the CFCDev list is available at
>www.mail-archive.com/[email protected]
>
>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]