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

FLOCK not CFLOCK 1

Status
Not open for further replies.

webmigit

Programmer
Joined
Aug 3, 2001
Messages
2,027
Location
US
Ok.. in perl there was good ol' fashioned flock, and I was intermediate level at perl for a while, hated it, wished for a structured language like html with perl's capabilities.. now I've got it, heh.. but back on subject, flock, as I understood it, locked a file until the flocked process was done...

Can CF do that? Is there a custom tag? I have a script that writes to/reads from as many as three text files at a time or as little as 0, but today I got a file sharing violation on the scripts second run.. It COULD be just a fluke that's one in a million, and I just happened to get it on the second run, but I wanna protect it from doing it again...

Here's the schematic, there are 30 lists to select from, each list is in a different text file.. any user can select from any of the lists and save to their own lists.. The process only changes the file and runs the 2 ton queries if the files are changed.. otherwise the script loads fast.. But each user saves to two lists, when they save to one, they automatically save to the other (sounds funky, I know, but the data is displayed in two different ways).

Anyway, it has to be done this way because the 256k connection speed dev server that's up delivered the page in 18 seconds, now with the text file writing, it delivers in two.. oddly even when the queries run.. but as the point of this post is, how can I lock the files so that users (novice exxagerates their internet use talents) can get along with the script.. how might cfcatch/cftry be able to help here? Or what can?

Thanks,
Tony Hicks
 
Hey Tony,

There's obviously a difference between locking at the OS level and locking in CF with <cflock>. If you only have to worry about reads/writes from CF, then the <cflock> should work for you. If other processes on the server are writing to the file then you could still have trouble.

Around every piece of code that writes to the file, I would put a <cflock> like this:

<cflock name=&quot;#fileName#&quot; timeout=&quot;5&quot;>

The <cflock> syntax is a little different for CF 4.5 and later but the goal is to lock with the name of the file so that different files can be written to at the same time but no to threads can write to the same file at the same time. You will want to set the timeout high enough to allow the scripts to finish writing to the file without getting a timeout. I would also reccomend catching any timeout errors just in case.

<cftry>
<cflock name=&quot;#fileName#&quot; timeout=&quot;5&quot;>
Read/Write to file...
</cflock>
<cfcatch>
File could not be accessed.
</cfcatch>
</cftry>

Hope this helps,
GJ
 
Thanks GunJack! This is why I love tek-tips..

Would something like?

<CFTRY>
...READ FILE AT 5 SECOND TIMEOUT...
<CFCATCH>
...READ FILE AT 20 SECOND TIMEOUT...
</CFCATCH>
</CFTRY>

work?

Is it better to CFINCLUDE or CFFILE read it? its just html I'm pulling in.. clean and simple...

If I understand it correctly CFFILE will read into a variable thus a heavy variable possibly.. But CFINLUDE is like a generic subroutine?

Anyway, your posts have been very helpful...

Thanks,
Tony Hicks
 
OOPS duh! I don't know how I missed your cfcatch/cftry stuff up there...

I'm gonna suggest to tek tips that authors should be able to delete/modify posts...
 
Hey Tony,

Yes, I would use cfinclude if you're just pulling in html to display. I think that would be a better route to take unless you need to do any processing (like replacing line breaks with <br>s). If you need to do any parsing and/or replacing then the cffile would be the way to go.

For the <cflock>s, I would just set your initial timeout to be the sum of the total time instead of two. I don't see any advantage to the nesting since setting the first one's timeout to the sum of the two would do the same thing and you would still have to have a <cfcatch> block on the inner ones. Setting a larger timeout doesn't mean it will take any longer, it just tells it to wait longer in the event of a lock situation.

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

Part and Inventory Search

Sponsor

Back
Top