Problem with TransactionScope
Description
Environment
Windows 7
.NET Framework 4.5
Visual Studio .NET 2012
Attachments
1
depends on
Activity
Show:
Alex Zaytsev March 4, 2015 at 1:37 AM
Alex Zaytsev
March 4, 2015 at 1:37 AM
The second issue is https://nhibernate.jira.com/browse/NH-3583#icft=NH-3583
Roman Podstawa November 18, 2013 at 8:44 AM
Roman Podstawa
November 18, 2013 at 8:44 AM
What I know is that in this scenario NH is not connected to the transaction from TransactionScope. And the workaround solves this problem. I don't know NH source code so I can't say where the problem is.
Maximilian Haru Raditya November 15, 2013 at 2:50 PM
Maximilian Haru Raditya
November 15, 2013 at 2:50 PM
@Roman Podstawa So given your workaround, does it mean that in current NH code base such transaction enlistment isn't correctly done?
Roman Podstawa November 15, 2013 at 1:37 PM(edited)
Roman Podstawa
November 15, 2013 at 1:37 PM
(edited)
Here you have a workaround:
[Test]
public void TransactionScopeProblem_Workaround()
{
using (TransactionScope tx = new TransactionScope())
{
using (var session = _sessionFactory.OpenSession())
{
if (System.Transactions.Transaction.Current != null)
{
((DbConnection)session.Connection).EnlistTransaction(System.Transactions.Transaction.Current);
}
using (var t = session.BeginTransaction())
{
Worker worker = new Worker();
session.Save(worker);
t.Commit();
}
}
tx.Dispose();
}
var workerCount = _sessionFactory.OpenSession().QueryOver<Worker>().RowCount();
Assert.AreEqual(0, workerCount);
}
This test works good.
Who's Looking?
When we use TransactionScope with NHibernate we have 2 problems:
1. Connection to db is not released between transactions (between unit tests).
2. QueryOver do not see changes made before invoking it.
Full description is in ReadMe file in attached project. In this attachment we provide unit tests to reproduce those problems.