Sunday, August 21, 2011

NHibernate Session's Transaction cannot detect other transactions' commit status

[TestMethod]
public void Nh_SessionTransaction_cannot_detect_transaction_commitness()
{
    // Arrange
    NHibernate.ISession sess = NhModelsMapper.GetSession(connectionString);
    Assert.IsFalse(sess.Transaction.IsActive);
    
    using(var tx = sess.BeginTransaction())
    {
        Assert.IsTrue(sess.Transaction.IsActive);
        Assert.IsTrue(tx.IsActive);

        tx.Commit();

        Assert.IsFalse(sess.Transaction.WasCommitted);
        Assert.IsTrue(tx.WasCommitted);

        Assert.IsFalse(sess.Transaction.IsActive);
        Assert.IsFalse(tx.IsActive);

    }

}

Further unit tests:
[TestMethod]
public void Nh_SessionTransaction_can_detect_its_own_transaction_commitness_only()
{
    // Arrange
    NHibernate.ISession sess = NhModelsMapper.GetSession(connectionString);
    Assert.IsFalse(sess.Transaction.IsActive);

    sess.Transaction.Begin();
    Assert.IsTrue(sess.Transaction.IsActive);
    
    Assert.IsFalse(sess.Transaction.WasCommitted);
    
}

No comments:

Post a Comment