TransactionScope 内的 Membership.GetUser() 抛出 TransactionPromotionException
以下代码抛出一个带有消息“事务已中止”的 TransactionAbortedException
和一个带有消息“尝试提升事务时失败”的内部 TransactionPromotionException
:
using ( TransactionScope transactionScope = new TransactionScope() )
{
try
{
using ( MyDataContext context = new MyDataContext() )
{
Guid accountID = new Guid( Request.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 ...]
}
}
错误发生在调用Membership.GetUser()
。
我的连接字符串是:
<add name="MyConnectionString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True"
providerName="System.Data.SqlClient" />
我阅读的所有内容都告诉我< code>TransactionScope 应该会神奇地应用于会员调用。用户存在(否则我希望返回 null。)
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( Request.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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TransactionScope
类屏蔽异常。最有可能发生的情况是该范围内的某些内容失败(引发异常),而TransactionAbortedException
只是控制退出using
块时发生的副作用。尝试将
TransactionScope
内的所有内容包装在try-catch
块中,并在catch
内重新抛出,并在那里设置断点;你应该能够看到真正的错误是什么。另一件事是,
TransactionScope.Complete
应该是包含TransactionScope
的using
块结束之前执行的最后一条语句。在这种情况下,您可能应该没问题,因为您之后实际上并没有做任何工作,但将对Complete
的调用放在内部作用域内往往会产生更容易出现错误的代码。更新:
现在我们知道了内部异常是什么(促进交易失败),更清楚发生了什么。
问题在于,在
TransactionScope
内,您实际上是使用GetUser
打开另一个数据库连接。会员资格提供者不知道如何重新使用您已经打开的DataContext
;它必须打开自己的连接,当TransactionScope
看到这一点时,它会尝试升级为分布式事务。它失败是因为您可能在 Web 服务器、数据库服务器或两者上禁用了 MSDTC。
如果您要打开两个单独的连接,则无法避免分布式事务,因此实际上有几种方法可以解决此问题:
将
GetUser
调用移至外部 em>TransactionScope
。也就是说,首先从成员资格提供程序将用户“读取”到列表中,然后在实际需要开始进行修改时启动事务。完全删除
GetUser
调用,并在同一DataContext
或至少同一连接上直接从数据库读取用户信息。在参与事务的所有服务器上启用 DTC(事务升级时性能会受到影响)。
我认为选项#1 在这种情况下是最好的;您需要从会员提供商处读取的数据在您读取数据的时间和开始交易的时间之间不太可能发生更改。
The
TransactionScope
class masks exceptions. Most likely what's happening is that something inside that scope is failing (throwing an exception), and theTransactionAbortedException
is simply a side-effect that occurs when control exits theusing
block.Try wrapping everything inside the
TransactionScope
in atry-catch
block, with a rethrow inside thecatch
, 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 theusing
block containing theTransactionScope
. In this case you should probably be alright, since you're not actually doing any work afterward, but putting the call toComplete
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 withGetUser
. The membership provider doesn't know how to re-use theDataContext
you already have open; it has to open its own connection, and when theTransactionScope
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.
一方面,这是正确的;另一方面,它也是正确的。事务总是被中止(您没有调用 Complete())。这是确切的代码吗?
此外,将
DataContext
放在TransactionScope
之外让我怀疑它可能会做一些奇怪的事情,因为当数据存在时事务不存在。 -首先创建上下文。您是否尝试过(两者):TransactionScope
跨越Complete
的DataContext
?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 theTransactionScope
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):TransactionScope
spans theDataContext
Complete
?