1

I need to execute a series of file system and database operations in a .Net web application where if a failure occurs everything is rolled back. The operations are:

  1. Upload multipage tiff file to server using HttpPostedFile
  2. Insert record of posted file to database
  3. Process and save individual images in multipage tiff as individual files on server
  4. Insert record of individual images to database

Can I wrap these operations in a single transaction using the available classes in the 3.5 framework? Should I just use try catch blocks and rollback operations manually?

kyletme
  • 441
  • 4
  • 17
  • When using TransactionScope the following error occurs: The transaction manager has disabled its support for remote/network transactions. This error is addressed in [http://stackoverflow.com/questions/10130767/the-transaction-manager-has-disabled-its-support-for-remote-network-transactions](http://stackoverflow.com/questions/10130767/the-transaction-manager-has-disabled-its-support-for-remote-network-transactions) – kyletme Jul 06 '12 at 04:09

2 Answers2

1

Can I wrap these operations in a single transaction using the available classes in the 3.5 framework?

No, and not with ANY other technology as you have a weak link.

1.Upload multipage tiff file to server using HttpPostedFile

2.Insert record of posted file to database

3.Process and save individual images in multipage tiff as individual files on server

4.Insert record of individual images to database

2-4 can be done easily in a transaction on a modern windows - NTFS can participate in a transaction.

Your problem is 1. 1 is impossible because - attention - HTTP HAS NO CONCEPT OF A TRANSACTION. If the upload happens from your server you COULD have upload instructions in an Upload CRM (Compensating Resource Manager) but you need to be able to delete them via HTTP too in case of a rollback and (perfectly, but optional) have them uploaded but not visible until you commit. This is IF THE API SUPPORTS IT - trivial to write (CRM in .NET is quite easy), otherwise you have a dead end there or take a compromise.

If you talk to upload TO your server then processing in a transaction, then technically to handle multi image uploads properly you have to decide where transactional control resides. The upload MUST have a proper API to start a transaction, do the uploads with a transaction token, then commit when all uploads are done (because only then processing can start). This is trivial to program - and does not even use TPM for most cases as you can roll logical transactions in your processing. but if you wan, again a CRM can handle the necessary changes in case of a rollback.

AlimItTech
  • 172
  • 1
  • 9
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • You are correct. I should have been more clear in my initial question. I am only trying to wrap steps 2 through 4 in a transaction once the uploaded file (step 1) has completed. However, when I wrap the operations from steps 2 through 4 in a TransactionScope I still get the error: The transaction manager has disabled its support for remote/network transactions. – kyletme Jul 06 '12 at 04:42
  • 1
    Well, then go, read the documentation and CONFIGURE THE DTC CORRECTLY. This is a totally different question than the one you ask here. By default, it is disabled for network (which may be needed if SQL Server is remote, for example), but that is a standard configuration change. Feel free to open a question for that - where it belongs, on Serverfault.com (as it is a configuration issue, not a programming one). – TomTom Jul 06 '12 at 06:25
0

Try something like the following:

using (TransactionScope scope = new TransactionScope())
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        try
        { 
            // Do all work here...  
        }
        catch (Exception ex)
        {
           // Delete files
           // LogError(ex);
        }                   
    }
    scope.Complete();
}
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • what about rolling back saved files? would you just delete them or what? – codingbiz Jul 06 '12 at 03:35
  • 2
    Yes, he'd have to cater for that separately... you could have a try-catch within the transaction and delete them if any of the DB operations failed. – IrishChieftain Jul 06 '12 at 03:37
  • I did look at TransactionScope but I have concerns regarding the IO operations based on [this](http://stackoverflow.com/questions/2273419/c-sharp-system-transactions-transactionscope) post – kyletme Jul 06 '12 at 03:43
  • 2
    I would keep it simple and create some tests to prove that the files are deleted in the event of one of the DB operations failing. – IrishChieftain Jul 06 '12 at 03:45