• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Cold Fusion/sharing violation help: weird cffile error (sharing violation)

I am executing a fairly big sql query followed by a few smaller queries and then spitting the result to a comma-delimited (.csv) file. I tweaked the query yesterday (as per the customer) and the query runs longer and I now get an error processing cffile. It says there is a sharing violation. I go ahead and open the incomplete csv file and each time there is a different number of records that make it to this file (sometimes 673 records, sometimes 663, sometimes 670, etc.., and a couple of times it has completly ran - all the way thru with no errors!).  and I know no one else is using this file. I delete the file and run my program again and I still get the same problem. any suggestions? any help would greatly be appreciative.
1 Solution
Try putting a cflock around the write command. If CF tries to access the file while it is still open for writing, there could be an error.
Try enclosing your <cfquery> and <cffile> tags in a <cflock> e.g.
<cflock timeout="60" type="EXCLUSIVE">


This will insure that everthing happens single threaded, and also allows you to put a timeout, so that if things drag on for longer than expect, you don't tie up the server.

The downside is that your query, will in fact be tieing up the server for the period of your lock.  (I don't know if this is acceptable or not).  If you find this solves the problem, then you can play around with where you place the lock in order to minimize the portion of the code that is locked and the period that it is locked.

I'm assuming that you're currently looping through the results of query, and appending the results one line at a time?

The best method to solve this problem is to stop opening and closing the file. What you'll want to do is to create a variable, and append all the records to that variable, and THEN add the variable to the CSV file. Make sure you append a return charater between each row in your variable so when you finally write it to the file, it will not all be on the same line. I think it's Chr(10) or Chr(13).

Don't bother locking the query, this won't do anything. To lock a query, you need to tell SQL to lock it either though CFTRANSACTION or in your SQL statement itself. But your problem isn't with the SQL, it with the time the file is open I'm guessing.

I'm also not sure that putting the lock statements around the CFFILE satement really have any meaning either. I believe that CFLOCK only works for variables (like client,session and application).

Anyhow, by writing the results all at once rather than looping, it should be faster and the server will only lock the file once (this is done automatically when the file is opened).
adecaturAuthor Commented:
What you suggested worked!  Thank you TallerMike.  

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now