Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
May 02, 2011 May 02, 2011

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

731
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2011 May 02, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 02, 2011 May 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 02, 2011 May 02, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 02, 2011 May 02, 2011
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources