Transactionscope doesn't roll back

L

Lubomir

..NET 3.5, Vista Bussiness 64

Hi,

I am calling an WCF service (netNamedPipeBinding, a locally running service)
from Windows form application. During the connection to the service, I am
executing a method, that contains a transaction:

using (TransactionScope scope = new TransactionScope ( ))
{
try
{
doSomething1();
doSomething2();
doSomething3();
scope.Complete();
}
catch (Exception)
{
}

}

When error occures in doSomething2(), exception is caught and the execution
will leave the TransactionScope block.

The doSomething1() is not rolled back as expected. In doSomething1() I am
creating some files on HDD and I expected these files will be automatically
deleted if transaction is not completed.

What am I missing?

Thanks,
Lubomir
 
J

jesse.houwing

Hello Lubomir,
.NET 3.5, Vista Bussiness 64

Hi,

I am calling an WCF service (netNamedPipeBinding, a locally running
service) from Windows form application. During the connection to the
service, I am executing a method, that contains a transaction:

using (TransactionScope scope = new TransactionScope ( ))
{
try
{
doSomething1();
doSomething2();
doSomething3();
scope.Complete();
}
catch (Exception)
{
}
}

When error occures in doSomething2(), exception is caught and the
execution will leave the TransactionScope block.

The doSomething1() is not rolled back as expected. In doSomething1() I
am creating some files on HDD and I expected these files will be
automatically deleted if transaction is not completed.

What am I missing?

First of all, only a number of specific operations actually enrol in a transaction,
writing to a file isn't one of them.

second of all, if you don't tell the transactionscope to roll back, it will
not roll back, unless an exception is unhandled within the scope.

As you are handling (actually swallowing) the exception with your catch block,
nothing will be rolled back. To fix this:

try
{
using (TransactionScope scope = new TransactionScope ( ))
{
doSomething1();
doSomething2();
doSomething3();
}
}
catch (Exception e)
{
///Handle exception in here
}

But as I said before, this will not roll back bytes written to a file.
 
M

Mr. Arnold

Lubomir said:
.NET 3.5, Vista Bussiness 64

Hi,

I am calling an WCF service (netNamedPipeBinding, a locally running
service)
from Windows form application. During the connection to the service, I am
executing a method, that contains a transaction:

using (TransactionScope scope = new TransactionScope ( ))
{
try
{
doSomething1();
doSomething2();
doSomething3();
scope.Complete();
}
catch (Exception)
{
}

}

When error occures in doSomething2(), exception is caught and the
execution
will leave the TransactionScope block.

The doSomething1() is not rolled back as expected. In doSomething1() I am
creating some files on HDD and I expected these files will be
automatically
deleted if transaction is not completed.

It's not going to happen. Deleting files from a HDD is not a transaction.
Transactions are used to persist CURD operations in a transactional state
across databases, as an example.

You'll need to delete the files first, and do any clean-up for files that
have been created prior to the Exception, in the Exception handling
yourself.

The file deletion of the files should be done first prior to executing code
that's in a true transactional state, like for a database.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4217 (20090704) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
It's not going to happen. Deleting files from a HDD is not a transaction.
Transactions are used to persist CURD operations in a transactional state
across databases, as an example.

Vista supports transactional file operations. I have written a wrapper
which is available from my website. It will work with both KTM and DTC.

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/#Transactions>
-> 'Transactions.zip'
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
I tried to go to this site, and I am unsuccessful.

Hmm, I just tested it and it worked fine. Maybe the site has been
temporarily down?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top