Using Derrick Rapley Maryland CFUG January 8, 2002.
-
Upload
thomasine-bates -
Category
Documents
-
view
216 -
download
0
Transcript of Using Derrick Rapley Maryland CFUG January 8, 2002.
Using <CFLOCK>
Derrick Rapley
Maryland CFUG
January 8, 2002
Variables in ColdFusion
When a variable is created, it resides in memory until a request has finished processing; then it is destroyed. The data of this variable is only visible by the thread processing the request.
When memory-resident variables are created, they reside in memory beyond the life of the thread. They may be accessed by multiple threads. They are removed when a Timeout occurs, or there is code that specifically removes the the variables (I.e. StructClear(Session))
Session, Application, and Server variables are all memory-resident variables.
Session, Server, and Application variables, Oh My!
These variables are available to more than one thread being processed and will always reside in memory.
They also support complex data types such as Structures, Arrays and Queries.
Tying It All Together
Once a memory-resident variable is set in memory, the same variable can be accessed and modified by multiple threads.
If these variables are not locked, then it is possible that two threads (requests) may be able to update the variables at the same time, and possibly corrupt the variable
Anatomy of CFLOCK
NAME – identifies a lock around a specific variable, file system, or custom tag call. This attribute is exclusive with the SCOPE attribute
SCOPE – alternative to the NAME attribute with one of three values: SESSION, APPLICATION, SCOPE. This attribute is exclusive with the NAME attribute
TYPE – READONLY or EXCLUSIVE (OPTIONAL) TIMEOUT – Length of time before an error is thrown if the lock
is not successfully obtained. (REQUIRED) THROWONTIMEOUT – YES or NO; specifies whether an
error message should be displayed if a lock is not successfully obtained (OPTIONAL)
TYPE – Readonly or Exclusive??
If no type is specified, then EXCLUSIVE the the default. Exclusive locks on allow 1 thread (request) to manipulate
data contained within the lock. Exclusive locks should only be used when data is being
written. Readonly locks allow multiple threads (requests) to access
the data within a the lock, but prevents an Exclusive lock from being obtained.
A Readonly lock is obtained when an Exclusive lock is not being processed at the same time.
Readonly locks should be used when data is being read.
Using NAME Locks
<cflock name=“Location” type=“Exclusive” timeout=“15”><cfset session.state=“Maryland”></cflock>
The name attribute identifies the lock Ensures that no two blocks of code with the same name are
executed at once If different names are used to access the same data, the lock
doesn’t exist at all. Name locks are share across applications and use sessions, but
not across clustered servers
Using SCOPE Locks
<cflock scope=“Session” type=“Exclusive” timeout=“15”><cfset session.city=“Rockville”></cflock>
Locking with the scope attribute is the best way to access memory-resident variables.
Supports three scopes: SESSION, APPLICATION, and SERVER. The SESSION Scope will only locks access for the same session
only (based on a variable called Session.SessionID) The APPLICATION Scope locks access for the same application. SERVER Scope locks server-wide access
Working with Structures
When a Structure is set to a local variable, the Duplicate() function must be used to create a copy of the Structure, rather than a pointer.
<cflock scope=“Session” timeout=“no” type=“Readonly”><cfset request.UserInfo=Duplicate(session.UserInfo)></cflock>
Using the CF Administrator
No automatic checking or locking– CF does not check to see if CFLOCK is being used
Full checking– If CFLOCK is not being used, an error will be thrown. Only
useful when the “SCOPE” attribute is used. If locks are created with the “NAME” attribute, an error will be thrown
Automatic read locking– Automatically locks variables that are being read. Can cause a
decrease in performance.
Avoiding Deadlocks
Deadlocks occur when a lock is initiated on two separate templates and are not nested in the same order.
Potential Problems
ColdFusion PCode errors cfserver process crashing/stopping/restarting Unexpected shared scope variable evaluation
results Large growth in the amount of memory used by
the cfserver process Operating system instability
Other Uses of CFLOCK
When using CFFILE Using Custom tags and CFX tags that aren’t
multi-threaded safe Updating, indexing, optimizing, and searching
data in a collection.
CFLOCK Tips
Don’t lock unnecessary code.Use CFLOCK to set all memory-resident
variables locally, then use the local variables.
Don’t lock read accesses with Exclusive locks
The End
Any Questions??