Session management has changed in NHibernate 1.2 Beta3 in a way that seems to prevent the use of System.Transaction.TransactionScope.
I was using Beta2 and successfully embedding NHibernate code inside a TransactionScope as long as the Session was not used prior to the TransactionScope block. Example:
Code:
using (System.Transactions.TransactionScope transScope = new System.Transactions.TransactionScope())
{
session = NHibernateHelper.GetCurrentSession();
...work with the session...
session.Flush();
transScope.Complete();
}
However, Beta3 introduces a concept of AggressiveRelease in the new ConnectionManager class. This only seems to be aware of Transactions that are programmatically managed by
Code:
session.BeginTransaction()
If session operations are performed inside a TransactionScope, the AggressiveRelease determines that it should still AutoClose the session since it doesn't believe there is a current transaction. In Beta2, the IsInActiveTransaction (on SessionImpl in that code base) was only called on disconnect.
Is this change by design, or is there an intention to support TransactionScope in which case this is probably a regression bug? I hestitated filing a JIRA bug because I haven't seen explicit NHibernate documentation indicating support for .NET 2.0's System.Transactions, although it appeared to have been working fine in Beta2 and it seems that other people on this newsgroup were using it in the way I described above.
Obviously there might be more significant changes needed, but maybe you could just change the ConnectionManager.IsInActiveTransaction from:
Code:
public bool IsInActiveTransaction
{
get { return transaction != null && transaction.IsActive; }
}
to something like:
Code:
public bool IsInActiveTransaction
{
get
{
bool inTransaction = transaction != null && transaction.IsActive;
bool inTransaction |= (System.Transactions.Transaction.Current != null);
return inTransaction;
}
}
I have no idea if that is the best way to achieve the result and it introduces a compile-time dependency on System.Transactions which might not be appropriate, but it's just some food for thought.
Thanks.
Pete