Auto begin NHibernate Transaction when ambient transaction detected.

Description

The new feature is a reaction to a lot of email queries asked on the Nhibernate groups.

It seems that it is a common assumption amongst different System.Transaction users to expect the NHibernate transactions to automatically begin/enlist, if an ambient transaction ( TransactionScope) is present.

The proposal is to introduce a way to have the Nhibernate transactions auto begin/enlist into the ambient transactions.

Environment

None

Activity

Show:

Frédéric Delaporte 
July 12, 2017 at 3:46 PM

Current NHibernate (v4.x) do auto-enlist within system transactions, without requiring an explicit NHibernate transaction (which works with a local transaction, which should not be mixed with a system transaction).

About the documentation, a section for system transactions has been added in PR, and should be available with NHibernate v5.

Frédéric Delaporte 
July 12, 2017 at 3:43 PM

In fact, as per Microsoft documentation, local transactions must not be mixed with system transactions (transaction scope).

As a rule, use System.Transactions in the general case, and use resource manager internal transactions only in specific cases where you are certain the transaction will not span multiple resources, and will not be composed into a larger transaction. Never mix the two.

Nesting a local transaction inside a scope still seems to work, but the opposite just throws. Better stick to recommendations. From this other Microsoft documentation:

EnlistTransaction throws an exception if the connection has already begun a transaction using the connection's BeginTransaction method.

There some work in progress about transaction scope and NHibernate. For a complete explanation of current NHibernate v4 state, refer to NH-4011. Its proposed solutions are currently not implemented as told there, better refer to this discussion for knowing what we are implementing (or this PR).

Joseph Lam 
December 13, 2010 at 6:08 AM

I think the documentation surrounding this issue is severely lacking, that it's unclear how TransactionScope is related to NHibernate transaction, and what happens if one doesn't create NHibernate transaction. Had I not been digging various forums for hours I wouldn't have known that TransactionScope doesn't eliminate the need for NH transactions.

Having to manage all TransactionScope, NHibernate Session and NHibernate Transaction is making it complicated and confusing for NHibernate users. I strongly support this request for simplification and reduce the chance of mis-use/errors.

Obsolete

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created April 21, 2010 at 11:47 AM
Updated July 12, 2017 at 3:46 PM
Resolved July 12, 2017 at 3:46 PM
Who's Looking?