Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFLOCK When to use? 2

Status
Not open for further replies.

jesse1

Programmer
Aug 29, 2008
30
US
Below is my login page andI use cflock when I create session variables. I think i used this correctly. But do I need to use CFLOCK on every query in the app or every time I just show a session variable? Also I am using Coldfusion 8

Code:
<!--- Get user's details from the database --->
<cfquery name="GetUser" datasource="teacherreacher" username="teacherreacher" password="jScottbern11"  >
SELECT	*
FROM 	stuser
WHERE	email 	= '#form.email#'
AND 	pwd 	= '#form.password#'
</cfquery>


<!--- Check if we have a winner! --->
<cfif GetUser.RecordCount eq 0>

<!--- User does not exist --->
<!--- Pass the variables back in the URL - message and UserName --->
<cfset loginpage = "index.cfm?nm=1" >
<cfset loginpage = loginpage & "&Name=" & URLEncodedFormat(#Form.email#)>
<cflocation url="#loginpage#">

<cfelseif GetUser.RecordCount gt 0 and GetUser.active eq 0>
<cfset loginpage = "index.cfm?nm=2" >
<cfset loginpage = loginpage & "&Name=" & URLEncodedFormat(#Form.email#)>
<cflocation url="#loginpage#">
<cfelse>

	<!--- Store the user id in session variables and cookies. --->
    <cflock scope="Session" timeout="20" type="exclusive">

		<cfset Session.districtid = GetUser.districtid>
		<cfset Session.parentid = GetUser.parentid>
		<cfset Session.email = GetUser.email>
		<cfset Session.sfirst = GetUser.sfirst>
		<cfset Session.slast = GetUser.slast>
		<cfset Session.password = GetUser.pwd>
    </cflock>
 

<META HTTP-EQUIV=REFRESH CONTENT="0; URL=main.cfm?pgid=1">
</cfif>
 
No you dont need to use CFLock on every query. I only use them with I am doing Session Management or when i am doing Inserts to a database, things that you would be doing at simultanious times based on users.
 
OK. But On almost every page I reference a session variable. Do I have to put locks on all queries that in some way reference a session variable?

Here is a simple example:
Code:
<cfquery name="getkid1" datasource="ddddd" username="dddd" password="dddd"  >
    SELECT	parentname
    FROM 	stparent
    where parentid=#session.parentid#
</cfquery>
 
Thanks for the link but that link is the reason I posted the questions. I should have clarified I am fairly new Cf programmer. I have a more detailed question but I wanted to get the basic concepts first.
 
With Version 8 i generally only lock Writes not Reads..
I am Confident that my code is sound and have not experienced any deadlocks over the past 3 years.
 
Do I have to put locks on all queries that in some way reference a session variable?

No. Putting aside the question of whether you actually need to lock for a moment, you do not want to lock an entire database query just to read a variable. If you did actually need to lock, it should be done outside the query.

Locking queries is generally discouraged. While I have seen it done, it is sometimes misused or implemented as a poor substitute for proper database handling. Personally, I have never found the need to lock queries. Databases have their own locking mechanisms. So transactions and validation have proved sufficient for my purposes.

As far as locking, it looks like your variables are set once at login and are then read only within the rest of the application. In which case locking is almost a moot point. If the values do not change, there is little need for locking.

If you read the article, the author says "...while trying to read and write to the same variable with 5 simultaneous threads won't crash the server, there's no guarantee in what order those threads will execute. This can cause inconsistent/inaccurate data...". However, he also says he's never seen such extreme load on CFMX servers that unlocked scope access caused incorrect results. So given that, and the fact that you are writing the variables once and reading them multiple times, IMO the need for locking is minimal.











----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top