views:

135

answers:

2

Hello,

I am wondering if I need to lock some code created in the application scope. If I create an object say userDAO.cfc in application scope so its available to all pages. Then if I have a method in that object say getUserInfo(userID) that would be called in different parts of the applications, do I need to lock this method?

Thanks!!

+4  A: 

Short answer: probably not.

If that object is created in the application scope from within your Application.cfc's OnApplicationStart() method, and it never changes, and also you make sure to var all of your variables for all your functions, then you won't need to lock access to it.

In this case, OnApplicationStart() does the locking for you, and won't let anyone proceed until that method is created. This is good because it will only allow the item to be created one time and will make sure it exists before anyone else uses it.

If you instantiate the object from anywhere else, like the top of some random page, then yes, you will need to lock it here, and everywhere that references it.

If the object will ever change into another object, or if it keeps any state, like private or public variables (variables.* or this.*) that change into other things, you may want to lock it externally, or you could try locking it inside the cfc on a per-variable basis.

Finally, if you don't var all your variables (use Mike Schierberl's varScoper!), then you will be changing the object's state. You should never really come to this point, but it is a case for locking.

Big exceptions for the whole thing are if you are using CF 5 or below, in which case any shared scope access MUST be locked, and if you are using CF 6 or 6.1, in which case there is no Application.cfc.

Nathan Strutz
Well, ColdFusion 5 precludes the possibility of using a CFC, but otherwise you're right.
Al Everett
Good explanation.
Ben Doom