Using CFLOCK - PowerPoint PPT Presentation

About This Presentation
Title:

Using CFLOCK

Description:

The name attribute identifies the lock ... Locking with the scope attribute is the best way to access memory-resident variables. ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 16
Provided by: DE854
Category:

less

Transcript and Presenter's Notes

Title: Using CFLOCK


1
Using ltCFLOCKgt
  • Derrick Rapley
  • Maryland CFUG
  • January 8, 2002

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

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

4
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

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

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

7
Using NAME Locks
  • ltcflock nameLocation typeExclusive
    timeout15gtltcfset session.stateMarylandgtlt/c
    flockgt
  • 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 doesnt exist at all.
  • Name locks are share across applications and use
    sessions, but not across clustered servers

8
Using SCOPE Locks
  • ltcflock scopeSession typeExclusive
    timeout15gtltcfset session.cityRockvillegtlt/c
    flockgt
  • 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

9
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.
  • ltcflock scopeSession timeoutno
    typeReadonlygtltcfset request.UserInfoDuplicate
    (session.UserInfo)gtlt/cflockgt

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

11
Avoiding Deadlocks
  • Deadlocks occur when a lock is initiated on two
    separate templates and are not nested in the same
    order.

12
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

13
Other Uses of CFLOCK
  • When using CFFILE
  • Using Custom tags and CFX tags that arent
    multi-threaded safe
  • Updating, indexing, optimizing, and searching
    data in a collection.

14
CFLOCK Tips
  • Dont lock unnecessary code.
  • Use CFLOCK to set all memory-resident variables
    locally, then use the local variables.
  • Dont lock read accesses with Exclusive locks

15
The End
  • Any Questions??
Write a Comment
User Comments (0)
About PowerShow.com