1

我有一个关于 NHibernate 交易的问题。据我所知,有两种方法可以从中ITransaction检索ISession

选项1

ISession.BeginTransaction();

选项 2

ISession.Transaction;

如果我像这样连续执行它们

ITransaction trans = session.BeginTransaction();
ITransaction trans2 = session.Transaction;

trans平等的trans2吗?

4

1 回答 1

0

NHibernate 源码@https ://github.com/nhibernate

来自 Isession @ NHibernate.Impl.SessionImpl 的会话实现

public ITransaction BeginTransaction()
{
    using (new SessionIdLoggingContext(SessionId))
    {
        if (rootSession != null)
        {
            // Todo : should seriously consider not allowing a txn to begin from a child session
            //      can always route the request to the root session...
            log.Warn("Transaction started on non-root session");
        }

        CheckAndUpdateSessionStatus();
        return connectionManager.BeginTransaction();
    }
}

public ITransaction Transaction
{
    get { return connectionManager.Transaction; }
}

来自 NHibernate.Adonet.ConnectionManager 类

public ITransaction BeginTransaction()
{
    Transaction.Begin();
    return transaction;
}

public ITransaction Transaction
{
     get
     {
         if (transaction == null)
         {
              transaction = session.Factory.TransactionFactory.CreateTransaction(session);
         }

         return transaction;
     }
}
于 2012-09-15T07:30:22.737 回答