Reply to topic
Coding Tip: Duplicate Your Session Scopes to Relieve Memory
JeanR
HostMySite Marketing

Joined: 28 Feb 2005
Posts: 86
Reply with quote
When you use custom tags to place exclusive locks around the session scope, it causes ColdFusion service issues, which affects all users within a shared environment. You can prevent these ColdFusion service issues by using a common fix we use to fine tune our code:

Use a variable scope to remove the memory being used by the session scope once the tag has completed it's necessary action.

Code:
<cflock timeout="45" throwontimeout="Yes" type="READONLY" scope="SESSION">
<CFSET variables.LSess=Duplicate(session)>
</cflock>


We would be replacing all references of session to variables.LSess and copying the local copy of the structure back to the session scope.

Code:
<cflock timeout="45" throwontimeout="Yes" type="EXCLUSIVE" scope="SESSION">
<CFSET session=Duplicate(variables.LSess)>
</cflock>


If you have difficulty copying over structures in your local version to the session scope, you can duplicate each key under the local session structure over to the scope. Your final code would look similar to:

Code:
<cflock timeout="45" throwontimeout="Yes" type="EXCLUSIVE" scope="SESSION">
<CFLOOP index="Key" list=#StructKeyList(variables.LSess)#>
<CFSET "Session.#Key#"=Duplicate(Evaluate("variables.LSess.#Key#"))>
</CFLOOP>
</cflock>
holmesjr


Joined: 24 May 2005
Posts: 2
Reply with quote
Can you explain why locking a session causes server-wide service issues?
ntunney


Joined: 03 Dec 2006
Posts: 1
Reply with quote
Duplicating session scope for custom tags is generally unnecessary. In the past (pre-MX), session scope was generally duplicate()ed into the request scope for such issues, but those days are gone. MX+ sessions are thread safe, and accessing the session scope outside of a lock no longer causes server issues. Unless there is some sort of race condition that could occur, the locking suggested here is overhead (especially at 45 seconds). Also as a side note, the use of evaluate() is frowned upon, unless absolutely necessary.
holmesjr


Joined: 24 May 2005
Posts: 2
Reply with quote
That was essentially what I was getting at with my question. A session lock affects only the particular session in question on CFMX and the technique suggested here is completely unnecessary.

Also, as Nick pointed out, evaluate is poor practice and is completely unwarranted, as demonstrated in an earlier post by HMS itself. Also, the loop in the example is a list loop for some bizarre reason - instead, loop over the collection directly for this kind of thing. If for some reason you need to loop over a struct to copy every key, this would be the better way to do it:

<CFLOOP item="Key" collection="#variables.LSess#">
<CFSET Session[Key] =Duplicate(variables.LSess[Key])>
</CFLOOP>
Coding Tip: Duplicate Your Session Scopes to Relieve Memory
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic