tags:

views:

40

answers:

3

In Adobe ColdFusion, if

<cfset Application.obj = CreateObject("component","ComponentName")>
<cfset myResult = Application.obj.FunctionName()>

I'm concerned that a var declared in the function's local scope might have concurrency problems because Application.obj is stored in the Application scope.

<cffunction name="FunctionName">
<cfset var local = {}>
(pretend some long process happens here)
<cfif condition>
   <cfset local.result = True>
<cfelse>
   <cfset local.result = False>
</cfif>
<cfreturn local.result>

If two people are in that function at the same time, will the result for person 1 corrupt the result for person 2?

+2  A: 

Yes, there is the potential for a race condition in your code sample.

You will need to use a lock around

<cfset myResult = Application.obj.FunctionName()> 

to prevent the race condition.

The type of lock to use would depend really on what the long process is doing.

If you are instantiating your framework you might consider double-checked locking. (Joe Rinehart, author of Model-Glue had a great post on this but his site isn't responding atm.)

If the long process is less critical you could use a simpler lock.

Antony
IF it's in the var scope (i.e. function-local scope), then you will not have race conditions and you do not need to lock the function call
marc esher
+3  A: 

To avoid concurrency issues, instantiate the object in the onapplicatiomstart method of your application.cfc. That will ensure the object gets created only once. Second as long as the variable "condition" is also scoped to the local scope, the two calls should not interfere with each other.

Terry Ryan
Thanks Terry...
cf_PhillipSenn
+2  A: 

As long as all of the variables being accessed are locally scoped (var'd in the function they're being called from, or an argument to that function), there's no concurrency issues. If you are hitting variables.somevar or this.something (or just somevar that doesn't belong to the local scope), then you might start to run into problems.

We do a whole lot of that sort of work.

MightyE