Using cflock


Using <cflock>

Locking in ColdFusion is implemented using the <cflock> tagthe code between <cflock> and </cflock> is managed by the lock.

The <cflock> tag has two mutually exclusive attributes: name and scope (both of which identify a lock). In addition to name and scope, two other attributes need attention.

The first is the timeout attribute, which throws a lock error if a procedure exceeds the value or if two locks are contentious. Contentious locks, which are also known as deadlocks, will be reviewed later in this chapter.

The other <cflock> attribute is type, which specifies whether the lock is readonly or exclusive. exclusive locks are used to prevent other access while a lock is in place, while a readonly lock allows read operations but not writes (in other words, readonly allows processing if no exclusive lock is open). readonly locks are faster than exclusive ones.

Using the name Attribute

The NAME attribute of the <cflock> tag uniquely identifies a lock around a specific variable, file system, or custom tag call. This name is referenced in all locations where the lock needs to be obeyed. The name attribute is useful for custom tag calls that are not thread-safe, or for file access locks. Specific SESSION, APPLICATION, or SERVER variables can also be locked using the name attribute.

The following code using the name attribute to lock a <cffile> call (so as to prevent concurrent writes to the same file):

 <cflock name="fileWrite"         timeout="15"         type="exclusive">   <cffile action="write">           file="#outFile"           output="#data#"> </cflock> 

By locking this block of code, all subsequent requests (using the same lock name) will wait until the initial request finishes running the custom tag. Because the timeout attribute was set for 15 seconds, an error will be thrown if the lock exceeds this period of time.

TIP

You should use exclusive locks whenever custom tags or file access is locked. This makes the lock a single-threaded operation. All calls to the same tag should use the same lock name.


Using the scope Attribute

The scope attribute is the alternative to the name attribute. This attribute has three possible values: SESSION, APPLICATION, or SERVER. Setting one of these values through the SCOPE attribute locks all the variables of that scope at the same time. A locked SESSION scope automatically applies to a single SESSION and does not lock any other sessions.

TIP

When locking the APPLICATION or SERVER scopes it is vital to keep lock time down to the absolute minimum.


readonly Locks

The alternative to using an exclusive lock is to use a readonly lock. A readonly lock will permit two requests to run simultaneously as though no lock existed. But if an exclusive lock is active, any readonly locks will wait until that lock is released.



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net