On a different note than the db-based approach, I use two application
variables per lock to do the job.  Tracks who owns the locks so the lock
owner can keep editing after they place the lock themselves (or
whatever), expires them etc.  In looking at this code now I bet lock
status and ownership can be done with a single app var that's a
structure rather than with two that store simple values.

The code is below.  I just chopped it out of a working app, so there may
be some vars in there not explained immediately below.  Replace my vars
with yours, of course.  The code is commented inline and should explain
whats going on pretty well.

Call inc_lockcheck with a cfinclude at the point you want to check if
record X is locked or not.  The returned result will be either LOCKED or
UNLOCKED.  If UNLOCKED that means the current user just put a lock on it
which they themselves own.  

Cheers,
--------------------------------------
Matt Robertson, MSB Designs, Inc.
http://mysecretbase.com - retail stuff
http://foohbar.org - CF tools
--------------------------------------

Externally set variables:
-------------------------
Client.EditMarker: ID num of the record someone is trying to lock
Client.Producer: ID num of the curr user's user record
Settings.SessionLasts: Numeric value (minutes)

<!--- 
My name is inc_lockcheck.cfm
returns: variables.LockStatus = LOCKED or UNLOCKED 
--->
<cfparam name="variables.LockStatus" default="UNLOCKED" type="string">
<cflock scope="APPLICATION" type="EXCLUSIVE" timeout="10">
<cfif not StructKeyExists(application, "RECORD#client.EditMarker#")>
        <cfmodule 
                template="tag_lockmake.cfm"
                editmarker=#client.EditMarker#
                owner=#client.Producer#>
<cfelse>
        <!--- 
        if its already locked, is it someone else's lock?
        --->
        <cfif
CompareNoCase(Evaluate("application.RECORD#client.EditMarker#Owner"),cli
ent.Producer)>
                <!--- 
                This page is subject to an unexpired lock 
                belonging to someone else.  Has the lock expired?
                --->
                <cfif
DateDiff("n",Evaluate("application.RECORD#client.EditMarker#"), now())
LT Settings.SessionLasts>
                        <!--- 
                        It has not expired.
                        --->
                        <cfset variables.LockStatus="LOCKED">
                <cfelse>
                        <!-- 
                        max allowed lock time exceeded.  
                        Break the lock and let me lock it for myself. 
                        --->
                        <cfinclude template="inc_lockbreak.cfm">
                        <cfmodule 
                                template="tag_lockmake.cfm"
                                editmarker=#client.EditMarker#
                                owner=#client.Producer#>
                </cfif>
        <cfelse>
                <!--- 
                Its my lock.  update my last access time 
                --->
                <cfset "application.Record#client.EditMarker#" = now()>
        </cfif>
</cfif>
</cflock>


<!--- 
My name is tag_lockmake.cfm
A cflock was already placed around me by the calling template
--->
<cfset "application.Record#attributes.EditMarker#" = now()>
<cfset
"application.Record#attributes.EditMarker#Owner"=attributes.owner>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Get the mailserver that powers this list at http://www.coolfusion.com

Reply via email to