H
haroldsphsu
Hi all,
I have some existing code that updates the database by creating a
SqlCommand and SqlTransaction:
public class GroupMapper
{
public static void Update(Group g)
{
// Gets SqlConnection, creates SqlCommand and calls
SqlConnection.BeginTransaction().
}
}
That works fine what I'm only updating one object at a time. But now
I need to update a bunch of objects (of different types) in a single
transaction, so I wrap the Update() calls in a TransactionScope
(within the TransactionScope, there might be calls to read the
database that doesn't require a transaction):
public void foo()
{
using (TransactionScope scope = new TransactionScope())
{
GroupMapper.Update(group);
FooMapper.Update(foo);
....
scope.Complete();
}
}
But I read in this article (http://msdn2.microsoft.com/en-us/library/
ms973865(d=printer).aspx) that:
"When using System.Transactions, applications should not directly
utilize transactional programming interfaces on resource managers--for
example the T-SQL BEGIN TRANSACTION or COMMIT TRANSACTION verbs, or
the MessageQueueTransaction() object in System.Messaging namespace,
when dealing with MSMQ. Those mechanisms would bypass the distributed
transaction management handled by System.Transactions, and combining
the use of System.Transactions with these resource manager "internal"
transactions will lead to inconsistent results. As a rule, use
System.Transactions in the general case, and use resource manager
internal transactions only in specific cases where you are certain the
transaction will not span multiple resources, and will not be composed
into a larger transaction. Never mix the two."
My transaction will not span multiple resources, so I should be ok?
The reason I'm asking this is that when running my unit tests,
occasionally, I see the following exception message:
"ExecuteReader requires the command to have a transaction when the
connection assigned to the command is in a pending local transaction.
The Transaction property of the command has not been initialized."
Which makes me wonder if I should delete all the code that creates
SqlTransaction and wrap each database update method in its own
TransactionScope.
Any ideas will be greatly appreciated!
Thanks,
Harold
I have some existing code that updates the database by creating a
SqlCommand and SqlTransaction:
public class GroupMapper
{
public static void Update(Group g)
{
// Gets SqlConnection, creates SqlCommand and calls
SqlConnection.BeginTransaction().
}
}
That works fine what I'm only updating one object at a time. But now
I need to update a bunch of objects (of different types) in a single
transaction, so I wrap the Update() calls in a TransactionScope
(within the TransactionScope, there might be calls to read the
database that doesn't require a transaction):
public void foo()
{
using (TransactionScope scope = new TransactionScope())
{
GroupMapper.Update(group);
FooMapper.Update(foo);
....
scope.Complete();
}
}
But I read in this article (http://msdn2.microsoft.com/en-us/library/
ms973865(d=printer).aspx) that:
"When using System.Transactions, applications should not directly
utilize transactional programming interfaces on resource managers--for
example the T-SQL BEGIN TRANSACTION or COMMIT TRANSACTION verbs, or
the MessageQueueTransaction() object in System.Messaging namespace,
when dealing with MSMQ. Those mechanisms would bypass the distributed
transaction management handled by System.Transactions, and combining
the use of System.Transactions with these resource manager "internal"
transactions will lead to inconsistent results. As a rule, use
System.Transactions in the general case, and use resource manager
internal transactions only in specific cases where you are certain the
transaction will not span multiple resources, and will not be composed
into a larger transaction. Never mix the two."
My transaction will not span multiple resources, so I should be ok?
The reason I'm asking this is that when running my unit tests,
occasionally, I see the following exception message:
"ExecuteReader requires the command to have a transaction when the
connection assigned to the command is in a pending local transaction.
The Transaction property of the command has not been initialized."
Which makes me wonder if I should delete all the code that creates
SqlTransaction and wrap each database update method in its own
TransactionScope.
Any ideas will be greatly appreciated!
Thanks,
Harold