Question

I use the TransactionScope class in my application because I need to do some operations trnsactionally, naturally (either all of them succeeds or none).

Since this operations are not only database related, I defined my own transactional behaviour for disc and system services operations (such as transactional file moving and service stoppping) by implementing the IEnlistmentNotification interface. It works fine, no doubt in that, but when I put all this things together in one application, I got unexpected results.

Imagine the following situation

using(var ts = ...)
{
    TxServiceManager.StopServices(services);
    TxFileManager.MoveFiles(files);
    DbManager.RunChangeScripts(scripts);
    TxServiceManager.StartServices(services);
    ts.Complete();
}

Worked pretty good until I got to a situation which required rolling back all operations. It happends when starting services and I would imagine, that the transaction scope will

  1. stop services it managed to start
  2. roll back a database
  3. restore files
  4. start services

But gues what? It simly starts with the first operation in the code listing, which is services stopping that of course rolls back by starting the services. All right, services are running and then it fails on the second operation, because I cannot overwrite files used by these services when they are running.

How it comes that rollback starts with the first operation and not with the last?

Was it helpful?

Solution

Because simply (from MSDN):

You should be aware that notifications might not be sent sequentially, or in a particular order.

The order is not defined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top