我正在一个大型应用程序中使用 Nhibernate 和 TransactionScope。该应用程序应该修改 3 个数据库并支持它们之间的分布式事务。这是我每次想在分布式事务中查询或执行数据库上的某些 sql 时调用的代码:
IDbConnection connection = new SqlConnection(connectionString);
connection.Open();
ISession session = SessionFactory.OpenSession(connection);
这是执行必要操作后调用的代码
IDbConnection sqlConnection = session.Connection;
if (sqlConnection != null && sqlConnection.State == ConnectionState.Open)
sqlConnection.Close();
session.Dispose();
当我执行 update-insert-delete 语句时,我还将代码包装在其中:
using (var transaction = session.BeginTransaction())
{ //code here
transaction.Commit();}
我认为这是非常标准的东西。现在......我在这个 using 块内执行这些操作:
using (TransactionScope scope = new TransactionScope())
{
//code here
scope.Complete();
}
我的问题是我的事务在启动后大约 6 秒偶尔被中止(Transaction.Current.TransactionInformation.Status 变为中止)。
这是来自我的分布式事务协调器的跟踪:
pid=6296 ;tid=13300 ;time=04/17/2012-19:34:29.430 ;seq=1 ;eventid=TRANSACTION_MANAGER_STARTED_2 ;;"TM Identifier='(null) '" ;"MS DTC started with the following settings: Security Configuration (OFF = 0 and ON = 1): Network Administration of Transactions = 0, Network Clients = 1, Inbound Transactions = 1, Outbound Transactions = 1, Transaction Internet Protocol (TIP) = 0, XA Transactions = 0, MSDTC RPC Security = Mutual Authentication Required, Account = NT AUTHORITY\NetworkService, Firewall Exclusion = 0, Transaction Bridge Installed = 0, Filtering duplicate events = 1."
pid=6296 ;tid=13300 ;time=04/17/2012-19:34:29.430 ;seq=2 ;eventid=TRACE_SETTINGS ;;"TM Identifier='(null) '" ;"Trace Configuration (OFF = 0 and ON = 1): Tracing Of DTC = 1, Tracing Of Transactions = 1, Tracing Of Aborted Transactions = 1, Tracing Of Long-Lived Transactions = 1, Tracing Of All Transactions = 0, Max Limit on Memory Buffers = 0."
pid=6296 ;tid=11372 ;time=04/17/2012-19:35:19.496 ;seq=3 ;eventid=CHECKPOINTING_STOPPED ;;"TM Identifier='(null) '" ;"MSDTC is suspending the checkpointing of transactions due to lack of activity"
pid=6296 ;tid=11372 ;time=04/17/2012-19:35:19.496 ;seq=4 ;eventid=TRACING_STOPPED ;;"TM Identifier='(null) '" ;"MSDTC is suspending the tracing of long - lived transactions due to lack of activity"
pid=6296 ;tid=10520 ;time=04/17/2012-19:36:31.191 ;seq=5 ;eventid=TRACING_STARTED ;;"TM Identifier='(null) '" ;"MSDTC is resuming the tracing of long - lived transactions"
pid=6296 ;tid=10520 ;time=04/17/2012-19:36:31.212 ;seq=6 ;eventid=TRANSACTION_BEGUN ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"transaction has begun, description :'user_transaction'"
pid=6296 ;tid=10520 ;time=04/17/2012-19:36:31.212 ;seq=7 ;eventid=RM_ENLISTED_IN_TRANSACTION ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"resource manager #1001 enlisted as transaction enlistment #1. RM guid = 'b9290b2d-9e1d-43ed-a5f3-e417f9b17906'"
pid=6296 ;tid=8016 ;time=04/17/2012-19:36:36.141 ;seq=8 ;eventid=RECEIVED_ABORT_REQUEST_FROM_BEGINNER ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"received request to abort the transaction from beginner"
pid=6296 ;tid=8016 ;time=04/17/2012-19:36:36.141 ;seq=9 ;eventid=TRANSACTION_ABORTING ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"transaction is aborting"
pid=6296 ;tid=8016 ;time=04/17/2012-19:36:36.141 ;seq=10 ;eventid=RM_ISSUED_ABORT ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"abort request issued to resource manager #1001 for transaction enlistment #1"
pid=6296 ;tid=8016 ;time=04/17/2012-19:36:36.219 ;seq=11 ;eventid=RM_ACKNOWLEDGED_ABORT ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"received acknowledgement of abort request from the resource manager #1001 for transaction enlistment #1"
pid=6296 ;tid=8016 ;time=04/17/2012-19:36:36.219 ;seq=12 ;eventid=TRANSACTION_ABORTED ;tx_guid=5c61419a-eec2-49c1-aaa1-007645a72e32 ;"TM Identifier='(null) '" ;"transaction has been aborted"
有谁知道我做错了什么以及如何防止我的交易被中止?先感谢您。
更新 1: 如果我在实例化 transactionscope 对象后立即打开 2 个会话和 2 个数据库,那么我会收到上述错误。如果我只是打开一个数据库的会话,然后我打开一个会话对其他数据库之一,我会收到以下错误:
The PROMOTE TRANSACTION request failed because there is no local transaction active.
当达到 connection.Open() 并且正在向第二个数据库打开连接时,就会发生这种情况。