nhibernate and transactionscope transaction aborted spontaniously

落爺英雄遲暮 提交于 2020-01-23 20:28:25

问题


I am working with Nhibernate and TransactionScope in a large application. The application is supposed to modify 3 databases and support a distribuited transaction across them. This is the code that I call each time I want to query or execute some sql on the database within the distribuited transaction:

IDbConnection connection = new SqlConnection(connectionString);
connection.Open();
 ISession session = SessionFactory.OpenSession(connection);

And this is the code called whenever after the necessary operations are executed

IDbConnection sqlConnection = session.Connection;
if (sqlConnection != null && sqlConnection.State == ConnectionState.Open)
   sqlConnection.Close();
session.Dispose();

When I execute update-insert-delete statements I also wrap the code inside this:

using (var transaction = session.BeginTransaction())
{  //code here

transaction.Commit();}

I think this is pretty standard stuff. Now ... I do these operations inside this using block:

using (TransactionScope scope = new TransactionScope())
{
//code here
scope.Complete();
}

My problem is that my transaction is being aborted sporadically about 6 seconds after it is being started(Transaction.Current.TransactionInformation.Status becomes Aborted).

This is the trace from my Distribuited Transaction Coordinator:

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"

Does anyone know what I am doing wrong and how I can keep my transaction from being aborted? Thank you in advance.

Update 1: If I open 2 sessions twoards 2 of the databases immediately after instantiating the transactionscope object then I get the error above. If I just open sessions towards one database and I open a session twoards one of the others later on I get the following error:

The PROMOTE TRANSACTION request failed because there is no local transaction active.

This happens when connection.Open() is reached and a connection is opening towards the second database.


回答1:


No idea if this will help across multiple databases I have not tested this code in that scenario. Here is an example of how I handle transactions.

 public interface ISessionManager : IDisposable
{
    ISession Session { get; set; }
    ISession GetSession();
}

public class SessionManager : ISessionManager
{
    private readonly ISessionFactory _sessionFactory;
    private TransactionScope _scope;
    public SessionManager(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
    }

    #region ISessionManager Members

    public ISession Session { get; set; }

    public ISession GetSession()
    {
        if (Session == null)
        {
            Session = _sessionFactory.OpenSession();
            if (!CurrentSessionContext.HasBind(_sessionFactory))
            {
                _scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted});
                Session.BeginTransaction(IsolationLevel.ReadCommitted);
                CurrentSessionContext.Bind(Session);
            }
        }

        Session = _sessionFactory.GetCurrentSession();
        Session.FlushMode = FlushMode.Never;
        return Session;
    }


    public void Dispose()
    {
        if (CurrentSessionContext.HasBind(_sessionFactory))
        {
            CurrentSessionContext.Unbind(_sessionFactory);
        }
        try
        {
            Session.Transaction.Commit();
            _scope.Complete();
            _scope.Dispose();
            Session.Flush();
        }
        catch (Exception)
        {
            Session.Transaction.Rollback();
            throw;
        }
        finally
        {
            Session.Close();
            Session.Dispose();
        }
    }

    #endregion
}


来源:https://stackoverflow.com/questions/10195925/nhibernate-and-transactionscope-transaction-aborted-spontaniously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!