In short....
1) You need to have locks around even the Cfif Isdefined() statements. In
order for CF to check if a function is defined it has to attempt a read of
said function.
2) When accessing a shared variable to read its value the cflock type=""
attribute should be Readonly.
3) When accessing a shared variable to create or modify its value the cflock
type="" attribute should be Exclusive.
I would modify your code like this:
<cfif isDefined("attributes.price") and
isDefined("attributes.quantity")
and isDefined("attributes.itemcode")>
<cflock timeout="1" throwontimeout="No" type="EXCLUSIVE" scope="SESSION">
<cfif NOT isDefined("session.cart")>
<cfset session.cart = structnew()>
</cfif>
</cflock>
<cfset temp = ListToArray('#attributes.price#,#attributes.quantity#')>
<cflock timeout="1" throwontimeout="No" type="EXCLUSIVE" scope="SESSION">
<cfif NOT structKeyExists(session.cart, attributes.itemcode)>
<cfset rs = structInsert(session.cart, attributes.itemcode,
temp)>
<cfelse>
<cfset temp[2] = attributes.quantity>
<cfset rs = structUpdate(session.cart, attributes.itemcode,
temp)>
</cfif>
</cfif>
</cflock>
</cfif>
I used exclusive locks because after the isdefined(), which by itself only
requires a readonly lock, you're modifying the session variables.....
|-----Original Message-----
|From: Jon Hall [mailto:[EMAIL PROTECTED]]
|Sent: Saturday, February 17, 2001 5:11 PM
|To: CF-Talk
|Subject: Locking question
|
|
|I have seen all of the warnings to make sure to use <cflock>
|around any of
|the in memory variable types in a CF application. I hear that
|every access
|of a session, or application variable should have <cflock>
|around it, or I
|could enable automatic locking on the server which will exact
|a performance
|hit though. Is there a different kind of lock I should use
|when reading a
|session or app variable than when I am writing to one of these
|variables?
|How about when I create a session or app variable?
|
|Here is a bit of code from a shoppingcart that I have written.
|It's is the
|addtocart()... Where should the cflocks go? Since I am not totally sure
|about cflock and it's uses I currently just cflock all of this tag,
|essentially single threading the entire function. This
|probably isn't too
|bad, since performance isn't a big requirement for the sites
|that I am using
|this code on. In my never ending quest for that extra
|millisecond though,
|understanding cflock is the next step.
|
|<cfif isDefined("attributes.price") and
|isDefined("attributes.quantity")
|and isDefined("attributes.itemcode")>
|
|<cfif NOT isDefined("session.cart")>
| <cfset session.cart = structnew()>
|</cfif>
|
|<cfset temp = ListToArray('#attributes.price#,#attributes.quantity#')>
|
|<cfif NOT structKeyExists(session.cart, attributes.itemcode)>
| <cfset rs = structInsert(session.cart, attributes.itemcode, temp)>
|<cfelse>
|
| <cfset temp[2] = attributes.quantity>
| <cfset rs = structUpdate(session.cart, attributes.itemcode, temp)>
|</cfif>
|
|</cfif>
|
|jon
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists