469,568 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

About TransactionScope

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
Sep 20 '06 #1
3 7558
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/is.../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]
- mv*@spam.guard.caspershouse.com

"kikapu" <me******@newsgroups.nospamwrote in message
news:ew*************@TK2MSFTNGP03.phx.gbl...
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

Sep 20 '06 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:O$**************@TK2MSFTNGP03.phx.gbl...
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/is.../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]
- mv*@spam.guard.caspershouse.com

"kikapu" <me******@newsgroups.nospamwrote in message
news:ew*************@TK2MSFTNGP03.phx.gbl...
>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


Sep 20 '06 #3
kikapu,

The source code that comes with the article is a pretty good example in
itself.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"kikapu" <me******@newsgroups.nospamwrote in message
news:eF****************@TK2MSFTNGP02.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:O$**************@TK2MSFTNGP03.phx.gbl...
>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/is.../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]
- mv*@spam.guard.caspershouse.com

"kikapu" <me******@newsgroups.nospamwrote in message
news:ew*************@TK2MSFTNGP03.phx.gbl...
>>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



Sep 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mukesh | last post: by
6 posts views Thread by Water Cooler v2 | last post: by
4 posts views Thread by hardieca | last post: by
reply views Thread by RP | last post: by
3 posts views Thread by Aleksey Timonin | last post: by
2 posts views Thread by GaryDean | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.