Skip to main content
Participant
May 2, 2011
Question

cancelling file upload in case of failed db transaction and vice versa...

  • May 2, 2011
  • 2 replies
  • 763 views

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

    This topic has been closed for replies.

    2 replies

    DanNOLHGAAuthor
    Participant
    May 2, 2011

    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.

    Inspiring
    May 2, 2011

    In this specific case, I'm not sure if it's even possible to get the file's metadata before you upload it.

    Owainnorth
    Inspiring
    May 3, 2011

    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.

    Inspiring
    May 2, 2011

    Don't attempt the query unless the upload is successful.  Use try/catch on the query.  Delete the file in the catch block.