Copy link to clipboard
Copied
I'm sure this is a pretty common problem. When a file is uploaded to the site, I use the CFFILE tag to upload the doc and a SQL insert query to capture the doc's metadata. How can I rollback the entire transaction if one of these operations fails?
example:
<CFFILE> ... success
<CFQUERY> ... fails
result: uploaded doc with no captured metadata
<CFQUERY> ... success
<CFFILE> ... fails
result: captured metadata but no uploaded doc
Copy link to clipboard
Copied
Don't attempt the query unless the upload is successful. Use try/catch on the query. Delete the file in the catch block.
Copy link to clipboard
Copied
Thanks! Is it generally adviseable to try the file operation before the database query? I have some other places in my code for file deletions and uploads where the situation is similar.
Copy link to clipboard
Copied
In this specific case, I'm not sure if it's even possible to get the file's metadata before you upload it.
Copy link to clipboard
Copied
Dan just posted exactly what I was going to say. How do you know upload (completed) times, accurate file sizes, locations etc until it's uploaded? A file upload is far more likely to fail than a database insert, so get that done and out the way first.