TransactionScope using

A

Aleksey Timonin

Hi guys,
I tried to use TransactionScope on to defferent TableAdapters like this:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
TableAdapter1.InsertQuery(id, name);
TableAdapter2.InsertQuery(id, customerName, customerAddress);
transScope.Complete();
}

It was failed because each table adapter using it's own connection and
TransactionScope doesn't manage with it...
So I modified my code:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
SqlConnection conn = TableAdapter1.Connection;

conn.Open();
TableAdapter1.InsertQuery(id, name);
TableAdapter2.Connection = conn;
TableAdapter2.InsertQuery(id, customerName, customerAddress);
conn.Close();
transScope.Complete();
}


Am I using TransactionScope correct? And if you see any problem in my code,
let me know.
Thanks a lot
Aleksey
 
N

Nicholas Paldino [.NET/C# MVP]

Aleskey,

What is the exception you are getting when you don't share the
connection? The connection ^should^ pick up on the ambient transaction and
enlist in it.

Then again, the provider might not even support it, but most providers
(from .NET 2.0 on, that is) should support it.
 
A

Aleksey Timonin

The exception is:
Network access for Distributed Transaction Manager (MSDTC) has been
disabled.....


Nicholas Paldino said:
Aleskey,

What is the exception you are getting when you don't share the
connection? The connection ^should^ pick up on the ambient transaction
and enlist in it.

Then again, the provider might not even support it, but most providers
(from .NET 2.0 on, that is) should support it.

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

Aleksey Timonin said:
Hi guys,
I tried to use TransactionScope on to defferent TableAdapters like this:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
TableAdapter1.InsertQuery(id, name);
TableAdapter2.InsertQuery(id, customerName, customerAddress);
transScope.Complete();
}

It was failed because each table adapter using it's own connection and
TransactionScope doesn't manage with it...
So I modified my code:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
SqlConnection conn = TableAdapter1.Connection;

conn.Open();
TableAdapter1.InsertQuery(id, name);
TableAdapter2.Connection = conn;
TableAdapter2.InsertQuery(id, customerName, customerAddress);
conn.Close();
transScope.Complete();
}


Am I using TransactionScope correct? And if you see any problem in my
code, let me know.
Thanks a lot
Aleksey
 
N

Nicholas Paldino [.NET/C# MVP]

Aleskey,

Have you configured DTC to allow distributed transactions across the
network? You need to go to Component Services (in the Administrative Tools
section) and then select the properties for your computer. You can then
configure the DTC on the MSDTC tab.

You also have to make sure that this is enabled on the other machine as
well, as it is participating in a distributed transaction.

Your code will allow you to get around the fact that it is a distributed
transaction by maintaining one connection, however, if you decide to compose
actions across different calls and want them to be included in the same
transaction, you would have to make your connection available to all of
those calls (which is a really bad idea). That's why it's a better idea to
enable the distributed transaction.

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

Aleksey Timonin said:
The exception is:
Network access for Distributed Transaction Manager (MSDTC) has been
disabled.....


Nicholas Paldino said:
Aleskey,

What is the exception you are getting when you don't share the
connection? The connection ^should^ pick up on the ambient transaction
and enlist in it.

Then again, the provider might not even support it, but most providers
(from .NET 2.0 on, that is) should support it.

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

Aleksey Timonin said:
Hi guys,
I tried to use TransactionScope on to defferent TableAdapters like this:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
TableAdapter1.InsertQuery(id, name);
TableAdapter2.InsertQuery(id, customerName, customerAddress);
transScope.Complete();
}

It was failed because each table adapter using it's own connection and
TransactionScope doesn't manage with it...
So I modified my code:

using (TransactionScope transScope = new
TransactionScope(TransactionScopeOption.Required))
{
SqlConnection conn = TableAdapter1.Connection;

conn.Open();
TableAdapter1.InsertQuery(id, name);
TableAdapter2.Connection = conn;
TableAdapter2.InsertQuery(id, customerName, customerAddress);
conn.Close();
transScope.Complete();
}


Am I using TransactionScope correct? And if you see any problem in my
code, let me know.
Thanks a lot
Aleksey
 

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