Skip to content

Commit 91c7aa3

Browse files
NH-2176 - Fixing a trouble then hitting a road-block.
1 parent a0c062d commit 91c7aa3

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/NHibernate.Test/NHSpecificTest/DtcFailures/DtcFailuresFixture.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class DtcFailuresFixture : TestCase
1919
{
2020
private static readonly ILog log = LogManager.GetLogger(typeof(DtcFailuresFixture));
2121

22+
protected override void Configure(Configuration configuration)
23+
{
24+
configuration.SetProperty(Cfg.Environment.TransactionStrategy, "NHibernate.Test.NHSpecificTest.NH2176.CustomAdoNetTransactionFactory, NHibernate.Test");
25+
}
26+
2227
protected override IList Mappings
2328
{
2429
get { return new[] {"NHSpecificTest.DtcFailures.Mappings.hbm.xml"}; }
@@ -60,7 +65,7 @@ private void BeforeBindMapping(object sender, BindMappingEventArgs e)
6065
prop.notnullSpecified = true;
6166
}
6267

63-
[Test]
68+
[Test, Ignore("With custom transaction factory, changes not explicitly flushed are ignored, so this test can no more test anything")]
6469
public void WillNotCrashOnDtcPrepareFailure()
6570
{
6671
var tx = new TransactionScope();
@@ -87,8 +92,8 @@ public void WillNotCrashOnDtcPrepareFailure()
8792
[Test]
8893
public void Can_roll_back_transaction()
8994
{
90-
if (Dialect is FirebirdDialect)
91-
Assert.Ignore("Firebird driver does not support distributed transactions");
95+
/*if (Dialect is FirebirdDialect)
96+
Assert.Ignore("Firebird driver does not support distributed transactions");*/
9297

9398
var tx = new TransactionScope();
9499
using (ISession s = sessions.OpenSession())
@@ -113,8 +118,8 @@ public void Can_roll_back_transaction()
113118
[Description("Another action inside the transaction do the rollBack outside nh-session-scope.")]
114119
public void RollbackOutsideNh()
115120
{
116-
if (Dialect is FirebirdDialect)
117-
Assert.Ignore("Firebird driver does not support distributed transactions");
121+
/*if (Dialect is FirebirdDialect)
122+
Assert.Ignore("Firebird driver does not support distributed transactions");*/
118123

119124
try
120125
{
@@ -143,8 +148,8 @@ public void RollbackOutsideNh()
143148
[Description("rollback inside nh-session-scope should not commit save and the transaction should be aborted.")]
144149
public void TransactionInsertWithRollBackTask()
145150
{
146-
if (Dialect is FirebirdDialect)
147-
Assert.Ignore("Firebird driver does not support distributed transactions");
151+
/*if (Dialect is FirebirdDialect)
152+
Assert.Ignore("Firebird driver does not support distributed transactions");*/
148153

149154
try
150155
{

src/NHibernate.Test/NHSpecificTest/NH2176/Fixture.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
125125
// call and do not call it again if unneeded.
126126
// (And Sql/OleDb/Odbc/Oracle manage/PostgreSql/MySql/Firebird/SQLite connections
127127
// support multiple calls with the same ongoing transaction, but some others may not.)
128-
var current = System.Transactions.Transaction.Current;
128+
var current = GetCurrentTransaction();
129129
var connection = session.Connection;
130130
System.Transactions.Transaction previous;
131131
if (!_sessionsTransaction.TryGetValue(connection, out previous) || previous != current)
@@ -140,7 +140,7 @@ public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
140140
// will fail if the connection was left with a completed transaction due to this.
141141
return;
142142
}
143-
session.Connection.EnlistTransaction(System.Transactions.Transaction.Current);
143+
session.Connection.EnlistTransaction(current);
144144
}
145145
}
146146
}
@@ -149,7 +149,7 @@ public bool IsInDistributedActiveTransaction(ISessionImplementor session)
149149
{
150150
// Avoid agressive connection release while a transaction is ongoing. Allow
151151
// auto-flushes (flushes before queries on dirtied entities).
152-
return System.Transactions.Transaction.Current != null;
152+
return GetCurrentTransaction() != null;
153153
}
154154

155155
public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work,
@@ -164,5 +164,25 @@ public void ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork wo
164164
tx.Complete();
165165
}
166166
}
167+
168+
private System.Transactions.Transaction GetCurrentTransaction()
169+
{
170+
try
171+
{
172+
return System.Transactions.Transaction.Current;
173+
}
174+
catch (InvalidOperationException)
175+
{
176+
// This damn thing may yield an invalid operation exception (instead of null
177+
// or of a completed transaction) if we are between scope.Complete() and
178+
// scope.Dispose(). This happen when having completed the scope then disposing
179+
// the session and only after that disposing the scope.
180+
// Instead of testing System.Transactions.Transaction.Current here, storing in
181+
// connection manager the ambient transaction associated to the connection
182+
// (and updating it when enlisting) then checking that stored transaction would
183+
// reduce testes on Transaction.Current.
184+
return null;
185+
}
186+
}
167187
}
168188
}

0 commit comments

Comments
 (0)