@@ -177,6 +177,35 @@ public void CanRollbackTransaction()
177
177
AssertNoPersons ( ) ;
178
178
}
179
179
180
+ [ Test ]
181
+ public void CanRollbackTransactionFromScope ( )
182
+ {
183
+ using ( new TransactionScope ( ) )
184
+ using ( var s = sessions . OpenSession ( ) )
185
+ {
186
+ ForceEscalationToDistributedTx . Escalate ( ) ;
187
+ s . Save ( new Person { CreatedAt = DateTime . Today } ) ;
188
+ // No Complete call for triggering rollback.
189
+ }
190
+
191
+ AssertNoPersons ( ) ;
192
+ }
193
+
194
+ [ Test ]
195
+ public void CanRollbackTransactionFromScopeWithFlush ( )
196
+ {
197
+ using ( new TransactionScope ( ) )
198
+ using ( var s = sessions . OpenSession ( ) )
199
+ {
200
+ ForceEscalationToDistributedTx . Escalate ( ) ;
201
+ s . Save ( new Person { CreatedAt = DateTime . Today } ) ;
202
+ s . Flush ( ) ;
203
+ // No Complete call for triggering rollback.
204
+ }
205
+
206
+ AssertNoPersons ( ) ;
207
+ }
208
+
180
209
[ Test ]
181
210
public void CanRollbackTransactionWithFlush ( )
182
211
{
@@ -270,6 +299,43 @@ public void RollbackOutsideNhWithFlush()
270
299
AssertNoPersons ( ) ;
271
300
}
272
301
302
+ [ Test ]
303
+ [ Description ( "rollback inside nh-session-scope should not commit save and the transaction should be aborted." ) ]
304
+ public void TransactionInsertWithRollBackFromScope ( )
305
+ {
306
+ using ( 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 ( ) ;
313
+ person . CreatedAt = DateTime . Now ;
314
+ }
315
+ // No Complete call for triggering rollback.
316
+ }
317
+ AssertNoPersons ( ) ;
318
+ }
319
+
320
+ [ Test ]
321
+ [ Description ( "rollback inside nh-session-scope should not commit save and the transaction should be aborted." ) ]
322
+ public void TransactionInsertWithRollBackFromScopeWithFlush ( )
323
+ {
324
+ using ( new TransactionScope ( ) )
325
+ {
326
+ using ( var s = sessions . OpenSession ( ) )
327
+ {
328
+ var person = new Person { CreatedAt = DateTime . Now } ;
329
+ s . Save ( person ) ;
330
+ ForceEscalationToDistributedTx . Escalate ( ) ;
331
+ person . CreatedAt = DateTime . Now ;
332
+ s . Flush ( ) ;
333
+ }
334
+ // No Complete call for triggering rollback.
335
+ }
336
+ AssertNoPersons ( ) ;
337
+ }
338
+
273
339
[ Test ]
274
340
[ Description ( "rollback inside nh-session-scope should not commit save and the transaction should be aborted." ) ]
275
341
public void TransactionInsertWithRollBackTask ( )
@@ -330,6 +396,43 @@ public void TransactionInsertWithRollBackTaskWithFlush()
330
396
[ Test ]
331
397
[ Description ( @"Two session in two txscope
332
398
(without an explicit NH transaction and without an explicit flush)
399
+ and with a rollback in the second dtc and a rollback outside nh-session-scope." ) ]
400
+ public void TransactionInsertLoadWithRollBackFromScope ( )
401
+ {
402
+ object savedId ;
403
+ var createdAt = DateTime . Today ;
404
+ using ( var txscope = new TransactionScope ( ) )
405
+ {
406
+ using ( var s = sessions . OpenSession ( ) )
407
+ {
408
+ var person = new Person { CreatedAt = createdAt } ;
409
+ savedId = s . Save ( person ) ;
410
+ }
411
+ txscope . Complete ( ) ;
412
+ }
413
+
414
+ using ( new TransactionScope ( ) )
415
+ {
416
+ using ( var s = sessions . OpenSession ( ) )
417
+ {
418
+ var person = s . Get < Person > ( savedId ) ;
419
+ person . CreatedAt = createdAt . AddMonths ( - 1 ) ;
420
+ }
421
+ ForceEscalationToDistributedTx . Escalate ( ) ;
422
+
423
+ // No Complete call for triggering rollback.
424
+ }
425
+
426
+ using ( var s = sessions . OpenSession ( ) )
427
+ using ( s . BeginTransaction ( ) )
428
+ {
429
+ Assert . AreEqual ( createdAt , s . Get < Person > ( savedId ) . CreatedAt , "Entity update was not rollback-ed." ) ;
430
+ }
431
+ }
432
+
433
+ [ Test ]
434
+ [ Description ( @"Two session in two txscope
435
+ (without an explicit NH transaction and without an explicit flush)
333
436
and with a rollback in the second dtc and a ForceRollback outside nh-session-scope." ) ]
334
437
public void TransactionInsertLoadWithRollBackTask ( )
335
438
{
@@ -374,6 +477,45 @@ public void TransactionInsertLoadWithRollBackTask()
374
477
}
375
478
}
376
479
480
+ [ Test ]
481
+ [ Description ( @"Two session in two txscope
482
+ (without an explicit NH transaction and without an explicit flush)
483
+ and with a rollback in the second dtc and a rollback outside nh-session-scope." ) ]
484
+ public void TransactionInsertLoadFlushWithRollBackFromScope ( )
485
+ {
486
+ object savedId ;
487
+ var createdAt = DateTime . Today ;
488
+ using ( var txscope = new TransactionScope ( ) )
489
+ {
490
+ using ( var s = sessions . OpenSession ( ) )
491
+ {
492
+ var person = new Person { CreatedAt = createdAt } ;
493
+ savedId = s . Save ( person ) ;
494
+ s . Flush ( ) ;
495
+ }
496
+ txscope . Complete ( ) ;
497
+ }
498
+
499
+ using ( new TransactionScope ( ) )
500
+ {
501
+ using ( var s = sessions . OpenSession ( ) )
502
+ {
503
+ var person = s . Get < Person > ( savedId ) ;
504
+ person . CreatedAt = createdAt . AddMonths ( - 1 ) ;
505
+ s . Flush ( ) ;
506
+ }
507
+ ForceEscalationToDistributedTx . Escalate ( ) ;
508
+
509
+ // No Complete call for triggering rollback.
510
+ }
511
+
512
+ using ( var s = sessions . OpenSession ( ) )
513
+ using ( s . BeginTransaction ( ) )
514
+ {
515
+ Assert . AreEqual ( createdAt , s . Get < Person > ( savedId ) . CreatedAt , "Entity update was not rollback-ed." ) ;
516
+ }
517
+ }
518
+
377
519
[ Test ]
378
520
[ Description ( @"Two session in two txscope
379
521
(without an explicit NH transaction and with an explicit flush)
0 commit comments