New System.Transactions Namespace

J

John Lee

Hi,

If I develop a component A and create a method DoSomething(string
connectionString) and I have another component B with a method
DoOtherThing(string connectionString)
and from inside DoSomething() the B.DoOtherThing() will be called like

public class A
{

public void DoSomething(string connectionString)
{
using (new TransactionScope())
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
//do some db change here
}

//call B
B o = new B();
o.DoOtherThing(connectionString);
}
}

If there is an exception in B.DoOtherThing, the change in A will not be
saved, right? according to the document, this should work, my question is in
COM+ world, I will have to make component A "transaction.required" and make
component B "transaction.supported" so the transaction context will actually
span two component - but in .NET 2.0, the component B totally unaware of the
context except being called inside the TransactionScope - how does this work
and how could I specifically to abort the transaction in certain condition?
Do I have to pass some transaction object to B?

Thanks very much!
John
 
K

Kevin Yu [MSFT]

Hi John,

As far as I know, in a TransactionScope, the transaction is only rolled
back when an exception is thrown. However, although an exception within the
transaction scope prevents the transaction from being committed, the
System.Transactions.TransactionScope class has no provision for rolling
back any changes your code may have made. If you need to take some action
when the transaction is rolled back, you must write your own implementation
of the System.Transactions.IEnlistmentNotification interface and explicitly
enlist it in the transaction.

Also since VS.NET 2005 hasn't been released yet, for .NET framework 2.0
questions, you can try to post your questions in the following newsgroup.
Thanks!

http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=45

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
R

Raphael Renous[MSFT]

There are some errors in the code.
1. The using statement should look like
using(TransactionScope ts = newTransactionScope())
{
.....
2. And the last statement of the using block must include
....
ts.Complete(); //If reach that point the transaction should be
marked as completed
}

By default, a COM+ component will pick up the ambient transaction set by
TransactionScope and will flow it to ComponentB. ComponenetB will be part of
that transaction.

To abort the transaction, just throw an exception and exit the 'using
TranscationScope' loop

Raphael Renous[msft]
 

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