Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

threading problem in cfc function

Status
Not open for further replies.

eegiffin

Programmer
Aug 10, 2007
2
US
Consider this original function in a CF component:

<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cfscript>
var account = createObject("java","com.mycompany.user.Account").init();

// gets user entered email, password from form, sets account bean email address property
account.set( arguments.event.getArgs() );

// load account properties from persistant datasource using email address
getAccountManager().loadAccount( account );

return account;
</cfscript>
</cffunction>

Many threads simultaneously compete for this function as the component is a part of (and accountManager as well) a singleton object. The problem: the initial thread was interrupted at some point in the function by another thread and the second thread overwrote the properties of the account java bean. Then, when the first thread resumed, it would have incorrect properties for the account bean.

I thought I had solved the problem by adding a cflock around the code in this function and in the loadAccount function of accountManager.cfc like so:

<cffunction name="getAccount" output="false" returntype="any">
<cfargument name="event" type="MachII.framework.Event" required="yes" />
<cflock name="getAccountLock" type="exclusive" timeout="10" throwontimeout="true">

<cfset account = createObject("java","com.mycompany.user.Account").init()>

<cfset account.set( arguments.event.getArgs() )>
// also added a cflock within this function to protect its functionality
<cfset getAccountManager().loadAccount( account )>

<cfreturn account>
</cflock>

</cffunction>

...but now I think I still have a problem as the variable 'account' is not in the var scope.

So I thought about adding <cfset var account=""> as the first line of the function (it won't let me add this line within the cflock).

My question is, if I add this line, will secondary threads, when they enter the function and execute the first line then wait for the cflock to release, wipe out the reference to the account bean while the first thread is executing?
 
The problem: the initial thread was interrupted at some point in the function by another thread and the second thread overwrote the properties of the account java bean.

Why do you say that? Each thread that executes the function should have its own copy of the var'd "account" variable.
 
The reason I believe that the cflock was necessary was based on the observable behavior of the site (the account bean was getting the wrong values set for its properties). The account bean wasn't just getting garbage values set, it was getting set to the account information for other valid users.

The variable "event" is in the request scope when it is passed into the function.
 
I could be wrong, but I still don't see how that method could cause that kind of problem. Are you properly var'ing all function variables in the referenced components/functions. Does the "event" variable involve any shared scopes?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top