@@ -26,7 +26,7 @@ protected override string MappingsAssembly
26
26
=> "NHibernate.Test" ;
27
27
28
28
protected override bool AppliesTo ( Dialect . Dialect dialect )
29
- => dialect . SupportsDistributedTransactions ;
29
+ => true || dialect . SupportsDistributedTransactions ;
30
30
31
31
protected override void CreateSchema ( )
32
32
{
@@ -176,6 +176,42 @@ public void CanRollbackTransaction()
176
176
AssertNoPersons ( ) ;
177
177
}
178
178
179
+ [ Test ]
180
+ public void CanRollbackTransactionWithFlush ( )
181
+ {
182
+ var tx = new TransactionScope ( ) ;
183
+ var disposeCalled = false ;
184
+ try
185
+ {
186
+ using ( var s = sessions . OpenSession ( ) )
187
+ {
188
+ ForceEscalationToDistributedTx . Escalate ( true ) ; //will rollback tx
189
+ s . Save ( new Person { CreatedAt = DateTime . Today } ) ;
190
+ s . Flush ( ) ;
191
+ tx . Complete ( ) ;
192
+ }
193
+ disposeCalled = true ;
194
+ Assert . Throws < TransactionAbortedException > ( tx . Dispose , "Scope disposal has not rollback and throw." ) ;
195
+ }
196
+ finally
197
+ {
198
+ if ( ! disposeCalled )
199
+ {
200
+ try
201
+ {
202
+ tx . Dispose ( ) ;
203
+ }
204
+ catch
205
+ {
206
+ // Ignore, if disposed has not been called, another exception has occurred in the try and
207
+ // we should avoid overriding it by the disposal failure.
208
+ }
209
+ }
210
+ }
211
+
212
+ AssertNoPersons ( ) ;
213
+ }
214
+
179
215
[ Test ]
180
216
[ Description ( "Another action inside the transaction do the rollBack outside nh-session-scope." ) ]
181
217
public void RollbackOutsideNh ( )
@@ -204,6 +240,35 @@ public void RollbackOutsideNh()
204
240
AssertNoPersons ( ) ;
205
241
}
206
242
243
+ [ Test ]
244
+ [ Description ( "Another action inside the transaction do the rollBack outside nh-session-scope." ) ]
245
+ public void RollbackOutsideNhWithFlush ( )
246
+ {
247
+ try
248
+ {
249
+ using ( var txscope = new TransactionScope ( ) )
250
+ {
251
+ using ( var s = sessions . OpenSession ( ) )
252
+ {
253
+ var person = new Person { CreatedAt = DateTime . Now } ;
254
+ s . Save ( person ) ;
255
+ s . Flush ( ) ;
256
+ }
257
+ ForceEscalationToDistributedTx . Escalate ( true ) ; //will rollback tx
258
+
259
+ txscope . Complete ( ) ;
260
+ }
261
+
262
+ Assert . Fail ( "Scope disposal has not rollback and throw." ) ;
263
+ }
264
+ catch ( TransactionAbortedException )
265
+ {
266
+ _log . Debug ( "Transaction aborted." ) ;
267
+ }
268
+
269
+ AssertNoPersons ( ) ;
270
+ }
271
+
207
272
[ Test ]
208
273
[ Description ( "rollback inside nh-session-scope should not commit save and the transaction should be aborted." ) ]
209
274
public void TransactionInsertWithRollBackTask ( )
@@ -232,6 +297,35 @@ public void TransactionInsertWithRollBackTask()
232
297
AssertNoPersons ( ) ;
233
298
}
234
299
300
+ [ Test ]
301
+ [ Description ( "rollback inside nh-session-scope should not commit save and the transaction should be aborted." ) ]
302
+ public void TransactionInsertWithRollBackTaskWithFlush ( )
303
+ {
304
+ try
305
+ {
306
+ using ( var txscope = new TransactionScope ( ) )
307
+ {
308
+ using ( var s = sessions . OpenSession ( ) )
309
+ {
310
+ var person = new Person { CreatedAt = DateTime . Now } ;
311
+ s . Save ( person ) ;
312
+ ForceEscalationToDistributedTx . Escalate ( true ) ; //will rollback tx
313
+ person . CreatedAt = DateTime . Now ;
314
+ s . Flush ( ) ;
315
+ }
316
+ txscope . Complete ( ) ;
317
+ }
318
+
319
+ Assert . Fail ( "Scope disposal has not rollback and throw." ) ;
320
+ }
321
+ catch ( TransactionAbortedException )
322
+ {
323
+ _log . Debug ( "Transaction aborted." ) ;
324
+ }
325
+
326
+ AssertNoPersons ( ) ;
327
+ }
328
+
235
329
[ Test ]
236
330
[ Description ( @"Two session in two txscope
237
331
(without an explicit NH transaction and without an explicit flush)
@@ -405,6 +499,46 @@ public void CanDeleteItemInDtc()
405
499
AssertNoPersons ( ) ;
406
500
}
407
501
502
+ [ Test ]
503
+ public void CanDeleteItemInDtcWithFlush ( )
504
+ {
505
+ object id ;
506
+ using ( var tx = new TransactionScope ( ) )
507
+ {
508
+ using ( var s = sessions . OpenSession ( ) )
509
+ {
510
+ id = s . Save ( new Person { CreatedAt = DateTime . Today } ) ;
511
+
512
+ ForceEscalationToDistributedTx . Escalate ( ) ;
513
+
514
+ s . Flush ( ) ;
515
+
516
+ tx . Complete ( ) ;
517
+ }
518
+ }
519
+
520
+ using ( var s = sessions . OpenSession ( ) )
521
+ using ( s . BeginTransaction ( ) )
522
+ {
523
+ Assert . AreEqual ( 1 , s . Query < Person > ( ) . Count ( ) , "Entity not found in database." ) ;
524
+ }
525
+
526
+ using ( var tx = new TransactionScope ( ) )
527
+ {
528
+ using ( var s = sessions . OpenSession ( ) )
529
+ {
530
+ ForceEscalationToDistributedTx . Escalate ( ) ;
531
+
532
+ s . Delete ( s . Get < Person > ( id ) ) ;
533
+ s . Flush ( ) ;
534
+
535
+ tx . Complete ( ) ;
536
+ }
537
+ }
538
+
539
+ AssertNoPersons ( ) ;
540
+ }
541
+
408
542
[ Test ]
409
543
[ Description ( "Open/Close a session inside a TransactionScope fails." ) ]
410
544
public void NH1744 ( )
@@ -443,6 +577,25 @@ public void CanUseSessionOutsideOfScopeAfterScope()
443
577
}
444
578
}
445
579
580
+ [ Test ]
581
+ public void CanUseSessionOutsideOfScopeAfterScopeWithFlush ( )
582
+ {
583
+ using ( var s = sessions . WithOptions ( ) . ConnectionReleaseMode ( ConnectionReleaseMode . OnClose ) . OpenSession ( ) )
584
+ {
585
+ using ( var tx = new TransactionScope ( ) )
586
+ {
587
+ s . Save ( new Person { CreatedAt = DateTime . Today } ) ;
588
+
589
+ ForceEscalationToDistributedTx . Escalate ( ) ;
590
+
591
+ s . Flush ( ) ;
592
+ tx . Complete ( ) ;
593
+ }
594
+
595
+ Assert . AreEqual ( 1 , s . Query < Person > ( ) . Count ( ) , "Entity not found in database." ) ;
596
+ }
597
+ }
598
+
446
599
private void AssertNoPersons ( )
447
600
{
448
601
using ( var s = sessions . OpenSession ( ) )
0 commit comments