Should we be also using CFLOCK around CFTRANSACTION
tags, I've never been quite clear on this?
No,
The database is much better at handling concurrency issues then CFLOCK,
which is basically just a ColdFusion-implemented semaphore with two modes:
read many/write none (aka ReadOnly), or write one/read none (aka Exclusive).
Although you could use CFLOCK to eliminate concurrency issues at a database
if you have only one CF server, the database is much better able to
intelligently figure out ways to allow simultaneous requests to occur
without problems (though deadlocks may sometimes still occur.)
In any case, CFTRANSACTION should only be used when really necessary. The
classic example of "necessary" is the bank account example. You want to
update checking with +$100, and savings with -$100. Both, or neither, must
succeed; having one update succeed without the other is not acceptable, so
you wrap the two updates in a database transaction, which ensures that
queries in a given transaction either all succeed, or all
fail. CFTRANSACTION is not handled on the CF side of things, it is really
just an interface into the database's own transactioning system.
Deadlocks can happen any time you have multiple people sharing resources at
the same time. If two people get into a taxi at the same time, but want to
go to different destinations, a deadlock occurs, and one passenger must be
chosen to be the deadlock victim (coin toss?) In ColdFusion terms, that
exception could be caught with cftry/cfcatch, and another taxi could be
attempted; or you could just let the exception pass to the error handler
page, the equivalent of the deadlock victim passenger giving up in
frustration and sitting on the curb.
How to write a probable deadlock using CFLOCK:
CFLOCK Type="ReadOnly" Scope="Application" Timeout=5
CFIF NOT isDefined("Application.SomeVar")
CFLOCK Type="Exclusive" Scope="Application" Timeout=5
CFSET Application.SomeVar = 5
/CFLOCK
/CFIF
/CFLOCK
If two threads hit the outer CFLOCK at the exact same time, and
Application.SomeVar is not defined, then both threads will wait for the
Exclusive lock to become available (which can't happen because both are in
a readonly lock) and the first request in the queue will timeout after 5
seconds (throwing an exception), which then allows the second thread to
enter the exclusive lock. (There is also a theoretical possibility that
both threads will time out if they are truly simultaneous.)
Written without probability of deadlock:
CFLOCK Type="ReadOnly" Scope="Application" Timeout=5
!--- yes, it's even necessary to use a readonly lock for isDefined() ---
CFSET isLockNeeded = NOT isDefined("Application.SomeVar")
/CFLOCK
CFIF isLockNeeded
CFLOCK Type="Exclusive" Scope="Application" Timeout=5
!--- check again; another thread may have just been here ---
CFIF NOT isDefined("Application.SomeVar")
CFSET Application.SomeVar = 5
/CFIF
/CFLOCK
/CFIF
HTH
Daryl
P.S. Had the trusty Metaphor Blender on "chop" for the taxi analogy. You
should see me puree..!
At 01:33 PM 10/3/2000 -0500, Ramphal, Ron wrote:
Daryl,
Should we be also using CFLOCK around CFTRANSACTION tags, I've never been
quite
clear on this?
Thanks,
Ron.
-Original Message-
From: Daryl Banttari [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 03, 2000 1:11 PM
To: Cold Fusion Users
Subject: Re: [KCFusion] CPU Usage
If you are using Server, Application, or Session variables, locking (using
CFLOCK) is REQUIRED. Failure to use CFLOCK around these variables can
cause all sorts of aberrant behavior, including CPU racing.
See:
http://www.allaire.com/handlers/index.cfm?id=17318method=full
http://www.allaire.com/handlers/index.cfm?ID=17196Method=Full
In particular, look at using the Request scope to replace constants such as
"Application.DSN".
The request scope is not well documented in the manuals. It is global to
the individual page request, so custom tags can use request scope variables
without going through the "caller" scope. At the end of the page request,
the variable goes away, but it does not require locking.
Also, make sure you have a recent MDAC (ODBC driver set) from
http://www.microsoft.com/data/
2.6 was just released, but 2.5sp1 should work well, too (and is well-tested.)
HTH
(BTW does the fact that I'm never able to make meetings now that I work for
Allaire count as "irony", or is it just "coincidence"?)
Daryl Banttari
Allaire Consulting
P.S. One of this month's Allaire DevCenter articles, "Query Caching in
ColdFusion", was written by yours truly:
http://www.allaire.com/handlers/index.cfm?ID=17552Method=Full
At 09:51 AM 10/3/2000 -0500, RE E wrote:
After our server has been running for a few hours