About TransactionScope

K

kikapu

Hi to all folks,

i am trying to understand the use of System.Transactions in general and
TransactionScope particularly.

What am i allowed to do in a using statement that wraps a TransactionScope
object,
so this to be rollback in case of an exception or when i do not call
scope.complete ??
I mean, can i set an object's variables and then un-done these sets
automatically
when transaction rolls back ??

For example, the following code does not work, x.var remains 2 after the
suing statement,
even if i throw an exception after var set:

class Program
{
static void Main(string[] args)
{
X x = new X();
x.var = 1;
using (TransactionScope scope = new
TransactionScope(TransactionScopeOption.Suppress))
{
x.var = 2;
//scope.Complete();
}
Console.WriteLine(x.var);
}
}
class X
{
public int var;
}
}


I am sure that i am missing something here, can anyone help ??

Kikapu
 
N

Nicholas Paldino [.NET/C# MVP]

kikapu,

The TransactionScope class will only work on resources that register
themselves with the transaction mananger. Right now, the most immediate
example of a resource like this is a database. It doesn't automatically
just detect changes to anything in the scope and then roll them back if
Complete is not called.

However, it is possible to have local variables which are rolled back in
the event of an abort of a transaction. Check out the article by Juval Lowy
titled "Volatile Resource Managers in .NET Bring Transactions to the Common
Type", as it provides a mechanism by which to do this:

http://msdn.microsoft.com/msdnmag/issues/05/12/Transactions/

Of course, you shouldn't do this for ALL of your variables (that will be
a huge performance hit), but for the ones that you need rolled back in the
case of an abort.

Hope this helps.
 
K

kikapu

Nicholas,

thanks for the reply!

i print now and will read this article that you send me.
Can you please give me a direction on how i can (a simple example)
register a resource with a transaction manager ??

Thanks again!


Nicholas Paldino said:
kikapu,

The TransactionScope class will only work on resources that register
themselves with the transaction mananger. Right now, the most immediate
example of a resource like this is a database. It doesn't automatically
just detect changes to anything in the scope and then roll them back if
Complete is not called.

However, it is possible to have local variables which are rolled back
in the event of an abort of a transaction. Check out the article by Juval
Lowy titled "Volatile Resource Managers in .NET Bring Transactions to the
Common Type", as it provides a mechanism by which to do this:

http://msdn.microsoft.com/msdnmag/issues/05/12/Transactions/

Of course, you shouldn't do this for ALL of your variables (that will
be a huge performance hit), but for the ones that you need rolled back in
the case of an abort.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

kikapu said:
Hi to all folks,

i am trying to understand the use of System.Transactions in general and
TransactionScope particularly.

What am i allowed to do in a using statement that wraps a
TransactionScope object,
so this to be rollback in case of an exception or when i do not call
scope.complete ??
I mean, can i set an object's variables and then un-done these sets
automatically
when transaction rolls back ??

For example, the following code does not work, x.var remains 2 after the
suing statement,
even if i throw an exception after var set:

class Program
{
static void Main(string[] args)
{
X x = new X();
x.var = 1;
using (TransactionScope scope = new
TransactionScope(TransactionScopeOption.Suppress))
{
x.var = 2;
//scope.Complete();
}
Console.WriteLine(x.var);
}
}
class X
{
public int var;
}
}


I am sure that i am missing something here, can anyone help ??

Kikapu
 
N

Nicholas Paldino [.NET/C# MVP]

kikapu,

The source code that comes with the article is a pretty good example in
itself.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

kikapu said:
Nicholas,

thanks for the reply!

i print now and will read this article that you send me.
Can you please give me a direction on how i can (a simple example)
register a resource with a transaction manager ??

Thanks again!


Nicholas Paldino said:
kikapu,

The TransactionScope class will only work on resources that register
themselves with the transaction mananger. Right now, the most immediate
example of a resource like this is a database. It doesn't automatically
just detect changes to anything in the scope and then roll them back if
Complete is not called.

However, it is possible to have local variables which are rolled back
in the event of an abort of a transaction. Check out the article by
Juval Lowy titled "Volatile Resource Managers in .NET Bring Transactions
to the Common Type", as it provides a mechanism by which to do this:

http://msdn.microsoft.com/msdnmag/issues/05/12/Transactions/

Of course, you shouldn't do this for ALL of your variables (that will
be a huge performance hit), but for the ones that you need rolled back in
the case of an abort.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

kikapu said:
Hi to all folks,

i am trying to understand the use of System.Transactions in general and
TransactionScope particularly.

What am i allowed to do in a using statement that wraps a
TransactionScope object,
so this to be rollback in case of an exception or when i do not call
scope.complete ??
I mean, can i set an object's variables and then un-done these sets
automatically
when transaction rolls back ??

For example, the following code does not work, x.var remains 2 after the
suing statement,
even if i throw an exception after var set:

class Program
{
static void Main(string[] args)
{
X x = new X();
x.var = 1;
using (TransactionScope scope = new
TransactionScope(TransactionScopeOption.Suppress))
{
x.var = 2;
//scope.Complete();
}
Console.WriteLine(x.var);
}
}
class X
{
public int var;
}
}


I am sure that i am missing something here, can anyone help ??

Kikapu
 

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