In this particular case, yes, it is thread safe because arrays are passed by value and not by reference (in other words, a totally separate copy of your item array is returned to the calling code). However, in theory if it were any other data type (struct, CFC instance, etc.) then you would still be dealing with something kept in the session scope and race conditions would then be a possibility. That said, as long as you exclusively lock the WRITES to the data stored in the session scope, you should be able to read from that data without any problems.

Basically, if I have something that I know in advance is going to be kept in a persistent scope and I also know that external code may be changing the data using public setters, I'll have the CFC itself manage the locking internally. On the other hand, if I know that I won't allow external calling code to modify the data (maybe the setter method's access modifier is set to package), I'll use a facade and force the external calling code to go through the facade to change the data. In that case, the facade would handle the locking.

Hope that helps,

Brian


On 7/1/05, Peter H <[EMAIL PROTECTED]> wrote:
Hi Guyz,

I've implemented a session based shopping cart with the following
components.

1) CartItem - ProductId, Name, Color, Cost etc with Getter and Setter
methods

2) Cart - An array of CartItem with AddCartItem, GetCartItems,
RemoveCartItem, etc methods

3) CartManager - Session facade that controls access to the Cart stored in
Session


CartManager has the following method to allow me to retrieve an Array of
CartItems from the session.

<cffunction name="GetCartItems" access="public" returntype="array"
output="false">
   <cflock scope="Session" timeout="10" type="readonly">
       <cfreturn Session.Cart.GetCartItems() />
    </cflock>
</cffunction>

So my question is this. If I code Items = CartManager.GetCartItems() is it
safe to use the Array of CartItems now that I am no longer inside the lock?

For example, can I safely call Items[1].GetColor() or
Items[1].SetColor(color)?

Cheers, Pete (aka lad4bear)




----------------------------------------------------------
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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

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).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at www.mail-archive.com/[email protected]

Reply via email to