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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFFILE - Best Practice

Status
Not open for further replies.

tleish

Programmer
Jan 17, 2001
619
US
I've had some scripts that read and write to txt files. Every once in a while I get an error the page, and find out that the last time the txt file was updated, it never finished the lines of text, so it threw errors in my parsing. I'm assuming this happens because the server crashed or something.

My question is, what is the best way to avoid this. Does CFTRANSACTION work with this. I've seen it suggested a few places, but I thought it only worked with database queries. Or would it be a good idea to write to a tmp file and then make a copy to the new.

Also, does CFLOCK work with CFFILE tags and when should it be used. Just on the write or append, or the read also.

Anybody have feedback on this? - tleish
 
I think you are correct about CFTRANSACTION. That is a Dbase only thing.

Yes, CFLOCK works with CFFILE and is recomended. Of course, you will nead a "ReadOnly" lock for all your reads and a "Exclusive" lock for all your writes. If you miss one or the other then you will not benifit from the proccess.

Use the "Name" attribute of the CFLOCK to "match" the filename (optional but conventions are handy)

<cflock name=&quot;myFile1&quot; type=&quot;Exclusive&quot; timeout=&quot;10&quot;>
<CFFILE action=&quot;read&quot; ... >
</cflock>
 
Hey tleish,

If the server crashes, there's obviously not much you can do but in the case of the script getting an error, I would use a <cftry> block to catch the error and then close out the file normally. You have to be careful though in the <cfcatch> block. Since you know something has gone wrong, you don't want to make any assumptions about variables and such. In my <cfcatch> blocks, I always make sure that all variables exist and verify they have valid values before doing any &quot;clean-up&quot; on them. Since you know something's gone wrong, you want to be very careful in your handler to make sure it wraps up normally.

Hope this helps,
GJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top