Open
Description
It should be possible to bind a session lifecycle to a System.Transactions.TransactionScope
.
An implementation of NHibernate.Context.ICurrentSessionContext
would work as follow (method GetCurrentSession
).
If first call for the current transaction
- opens a session (
sessionFactory.OpenSession()
) - register a callback for the
System.Transactions.Transaction.Current.TransactionCompleted
event, which flush the session (if transaction status isCommitted
) and close it. - add the session to a
IDictionary<string, ISession>
where the key is the transaction identifier (Transaction.Current.TransactionInformation.LocalIdentifier
) + sessionfactory hashcode and the value is the session newly created.
If not first call for the current transaction : return the session found in the IDictionary<string, ISession>
I found information about ISession and transaction scope synchronization, but I never got the expected result. I finally wrote an implementation of ICurrentSessionContext
which works as described above. Is there something wrong in the implementation described here above ?