| By Bruce Van Horn | Article Rating: |
|
| November 27, 2000 12:00 AM EST | Reads: |
7,088 |
Thanks to those of you who sent in questions recently. I hope the answers you received from me or other members of the Allaire training team were helpful. For our readers, here are a few of the questions that were submitted:
Q:I understand the importance of using CFLOCK with the Scope attribute around code that references session or application variables. Could you give me an example of why Id ever want to use CFLOCK using the Name attribute?
A: Let's start with the bottom line: CFLOCK makes sure that a block of code Isn't executed by more than one request at a time. As you know, the SCOPE attribute is used when locking code that reads or writes variables that use shared memory spaces (variables in the server, application, and session scopes). The NAME attribute is used for locking any other kind of code that could cause problems if more than one instance of that same code were executed simultaneously.
There are a lot of places you might want to do this, but here's a simple example: suppose you have a page that inserts records into an MS Access database. Immediately after you insert a record you need to know it's primary key value. In Access you need to run a subsequent query to get that value. However, if more than one person can insert records at the same time, you could get the wrong value back for your record. Using CFLOCK with the NAME attribute protects this process.
<CFLOCK NAME="LockInsert" TIMEOUT="20" TYPE="EXCLUSIVE">
<CFINSERT DATASOURCE="MyDSN" TABLENAME="Customers">
<CFQUERY NAME="GetID" DATASOURCE="MyDSN">
SELECT Max(CustID) AS NewID
FROM Customers
</CFQUERY>
</CFLOCK>
The trick to using the NAME attribute is to make sure you give each instance of CFLOCK a unique name, or CF will single thread any code that has the same name (regardless of the code being executed!).
Most of the time were thankful for the multithreaded capabilities of ColdFusion, but at times we need to make certain tasks single threaded. This is where you should use CFLOCK.
Q:How can I retrieve and display to my users a .pdf (Adobe Acrobat) file from another server using CFHTTP? I get an error that says I cant output the value of CFHTTP.FileContent.A:The reason for the error when outputting the value of CFHTTP.FileContent after retrieving a .pdf file is that the contents are binary rather than plain text (as an HTML page would be); you cant output binary data using the CFOUTPUT tag. Use CFHTTP to retrieve the file, but physically store it on your server before showing it to the user. Your CFHTTP tag should use the PATH attribute to store the retrieved file to your server, then simply create a link or CFLOCATION to that file. The example tag below will write a file called "somefile.pdf" to the specified subdirectory.
<CFHTTP URL="http://someserver.com/somefile. pdf"
METHOD="GET"
PATH="c:\inetpub\wwwroot\pdf_files"
RESOLVEURL="false"> </CFHTTP>
Q:Is there a way to programmatically flush a CFQUERY result set that's been cached before it naturally expires using the CACHEDWITHIN attribute?
A:Yes. All you need to do is run the identical query with new values in the CACHEDWITHIN attribute. For example, if you created the cached query using CACHEDWITH="#CreateTime-Span(0,0,30,0)#", youd simply run the same query using CACHEDWITH="#CreateTimeSpan(0,0,0,0)#" to clear that query from the cache.
color="blue"> Q:Ive seen references in this journal and in some forum posts to the "Request" variable scope, but the ColdFusion help files don't say much about it. Could you please tell me what it is and why I might want to use it?
"blue">A:The Request scope is a very useful but often overlooked variable scope. Use the Request scope for variables that you want to make available to all templates that make up a single user request. For example, a call to MyPage.cfm may result in several other pages being called (Application.cfm, cfincludes, and custom tags). If you created a local variable in Application.cfm, for example, that variable would be available to MyPage.cfm but it wouldnt be available to custom tags called by MyPage.cfm. However, if you set that variable into the Request scope (<CFSET Request.MyVar = "Some Value">), it would be available to any custom tags called as a result of this "request." The Request scope, however, Isn't a persistent (memory resident) variable like the Application or Session scopes. It exists only for the life of the request from that particular user. Because it's something of an "extended life" local variable, you don't have to worry, as you would for a Session variable, about using CFLOCK to protect the memory space it occupies.
Please send your questions about ColdFusion (CFML, CF Server, or CF Studio) to AskCFDJ@sys-con.com.
Published November 27, 2000 Reads 7,088
Copyright © 2000 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Bruce Van Horn
Bruce Van Horn is president of Netsite Dynamics, LLC, a certified ColdFusion developer/instructor, and a member of the CFDJ International Advisory Board.
























