The following code throws a TransactionAbortedException
with message "The transaction has aborted" and an inner TransactionPromotionException
with message "Failure while attempting to promote transaction":
using ( TransactionScope transactionScope = new TransactionScope() )
{
try
{
using ( MyDataContext context = new MyDataContext() )
{
Guid accountID = new Guid( R开发者_StackOverflowequest.QueryString[ "aid" ] );
Account account = ( from a in context.Accounts where a.UniqueID.Equals( accountID ) select a ).SingleOrDefault();
IQueryable < My_Data_Access_Layer.Login > loginList = from l in context.Logins where l.AccountID == account.AccountID select l;
foreach ( My_Data_Access_Layer.Login login in loginList )
{
MembershipUser membershipUser = Membership.GetUser( login.UniqueID );
}
[... lots of DeleteAllOnSubmit() calls]
context.SubmitChanges();
transactionScope.Complete();
}
}
catch ( Exception E )
{
[... reports the exception ...]
}
}
The error occurs at the call to Membership.GetUser()
.
My Connection String is:
<add name="MyConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True"
providerName="System.Data.SqlClient" />
Everything I've read tells me that TransactionScope
should just get magically applied to the Membership calls. The user exists (I'd expect a null return otherwise.)
The TransactionScope
class masks exceptions. Most likely what's happening is that something inside that scope is failing (throwing an exception), and the TransactionAbortedException
is simply a side-effect that occurs when control exits the using
block.
Try wrapping everything inside the TransactionScope
in a try-catch
block, with a rethrow inside the catch
, and set a breakpoint there; you should be able to see what the real error is.
One other thing, TransactionScope.Complete
should be the last statement executed before the end of the using
block containing the TransactionScope
. In this case you should probably be alright, since you're not actually doing any work afterward, but putting the call to Complete
inside an inner scope tends to make for more bug-prone code.
Update:
Now that we know what the inner exception is (failure promoting transaction), it's more clear what's going on.
The problem is that inside the TransactionScope
, you are actually opening up another database connection with GetUser
. The membership provider doesn't know how to re-use the DataContext
you already have open; it has to open its own connection, and when the TransactionScope
sees this, it tries to promote to a distributed transaction.
It's failing because you probably have MSDTC disabled on either the web server, the database server, or both.
There's no way to avoid the distributed transaction if you are going to be opening two separate connections, so there are really a few ways around this issue:
Move the
GetUser
calls outside theTransactionScope
. That is, "read" the users first from the membership provider into a list, then start the transaction when you actually need to start making modifications.Remove the
GetUser
calls altogether and read the user information directly from the database, on the sameDataContext
or at least the same connection.Enable DTC on all servers participating in the transaction (performance will be impacted when a transaction promotes).
I think that option #1 is going to be the best in this scenario; it's very unlikely that the data you need to read from the membership provider will be changed between the time you read it and the time you begin the transaction.
At one level, it is correct; the transaction is always aborted (you aren't calling Complete()). Is that the exact code?
Additionally, having the DataContext
outside the TransactionScope
makes me suspect that it might be doing some odd things since the transaction isn't there when the data-context first gets created. Have you tried (both of):
- reversing the creation order, so the
TransactionScope
spans theDataContext
- calling
Complete
?
using ( TransactionScope transactionScope = new TransactionScope() )
using ( MyDataContext context = new MyDataContext() )
{
/* ... */
transactionScope.Complete();
}
精彩评论