From: jeby on
I have been looking up a lot of information on locking shared-scope variables
lately, because I dont really do it at all and I probably should. My question
revolves around something else I had posted about a couple of days ago about
creating objects in the session scope.

I have user.cfc that holds information such as username. etc... I have
methods such as getFirstName(), getLastName(), and setUser(userID). These
variables are references as #Session.User.getFirstName()# as expected.

My question is, since this is a session variable... should it be locked before
access?

From: Ian Skinner on
jeby wrote:
> My question is, since this is a session variable... should it be locked before
> access?
>

Probably not very often. You should not have to do a lock when you are
just reading values. And you would only need to do a lock when changing
a value in your object if you are concerned about race conditions.
Which for session variables are relatively rare.

If you where dealing with an object stored in the Application or Server
scope then there would be a much greater chance for race conditions to
occur. Thus you would generally be more concerned about locking
modifying code for these types of objects.

In early versions of ColdFusion there where memory leak problems with
these persisted scopes and the work around was to always lock every
thing. This issue has long been resolved, but the old cannon of locking
all the time still persists.

But there can be serious performance, scalability and throughput issues
to over locking your application. So the better pratice is to only lock
when there is a reason to lock.