@@ -33,7 +33,7 @@ public void Deadlock(bool first, IsolationLevel? isolationLevel, TransactionOpen
33
33
domain . WithStrategy ( ExecuteActionStrategy . HandleUniqueConstraintViolation ) . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) ) . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) ) . Execute (
34
34
session => {
35
35
_ = Interlocked . Increment ( ref Count ) ;
36
- _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
36
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
37
37
if ( first ) {
38
38
_ = session . Query . All < Foo > ( ) . Lock ( LockMode . Exclusive , LockBehavior . Wait ) . ToArray ( ) ;
39
39
if ( wait1 != null ) {
@@ -59,14 +59,13 @@ public void External(
59
59
bool first ,
60
60
IsolationLevel ? isolationLevel ,
61
61
TransactionOpenMode ? transactionOpenMode ,
62
- Action < bool , IsolationLevel ? , TransactionOpenMode ? > action )
62
+ Action < Session , bool , IsolationLevel ? , TransactionOpenMode ? > action )
63
63
{
64
64
using ( var session = domain . OpenSession ( ) )
65
- using ( var tran = isolationLevel == null ? null : session . OpenTransaction ( ) )
66
- using ( session . Activate ( ) ) {
65
+ using ( var tran = isolationLevel == null ? null : session . OpenTransaction ( ) ) {
67
66
if ( tran != null ) {
68
67
session . EnsureTransactionIsStarted ( ) ;
69
- _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
68
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
70
69
}
71
70
if ( first ) {
72
71
if ( wait1 != null && wait2 != null ) {
@@ -78,8 +77,8 @@ public void External(
78
77
_ = wait2 . Set ( ) ;
79
78
_ = wait1 . WaitOne ( ) ;
80
79
}
81
- action ( first , isolationLevel , transactionOpenMode ) ;
82
- if ( tran != null ) {
80
+ action ( session , first , isolationLevel , transactionOpenMode ) ;
81
+ if ( tran != null ) {
83
82
tran . Complete ( ) ;
84
83
}
85
84
}
@@ -92,22 +91,55 @@ public void Parent(
92
91
IExecuteActionStrategy strategy ,
93
92
Action < bool , IsolationLevel ? , TransactionOpenMode ? > action )
94
93
{
95
- domain . WithStrategy ( strategy ) . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) ) . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) ) . Execute (
96
- session => {
97
- session . EnsureTransactionIsStarted ( ) ;
98
- _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
99
- if ( first ) {
100
- if ( wait1 != null && wait2 != null ) {
101
- _ = wait1 . Set ( ) ;
102
- _ = wait2 . WaitOne ( ) ;
94
+ domain . WithStrategy ( strategy )
95
+ . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) )
96
+ . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) )
97
+ . Execute (
98
+ session => {
99
+ session . EnsureTransactionIsStarted ( ) ;
100
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
101
+ if ( first ) {
102
+ if ( wait1 != null && wait2 != null ) {
103
+ _ = wait1 . Set ( ) ;
104
+ _ = wait2 . WaitOne ( ) ;
105
+ }
103
106
}
104
- }
105
- else if ( wait1 != null && wait2 != null ) {
106
- _ = wait2 . Set ( ) ;
107
- _ = wait1 . WaitOne ( ) ;
108
- }
109
- action ( first , isolationLevel , transactionOpenMode ) ;
110
- } ) ;
107
+ else if ( wait1 != null && wait2 != null ) {
108
+ _ = wait2 . Set ( ) ;
109
+ _ = wait1 . WaitOne ( ) ;
110
+ }
111
+ action ( first , isolationLevel , transactionOpenMode ) ;
112
+ } ) ;
113
+ }
114
+
115
+ public void Parent (
116
+ Session session ,
117
+ bool first ,
118
+ IsolationLevel ? isolationLevel ,
119
+ TransactionOpenMode ? transactionOpenMode ,
120
+ IExecuteActionStrategy strategy ,
121
+ Action < bool , IsolationLevel ? , TransactionOpenMode ? > action )
122
+ {
123
+ domain . WithStrategy ( strategy )
124
+ . WithSession ( session )
125
+ . WithIsolationLevel ( isolationLevel . GetValueOrDefault ( IsolationLevel . RepeatableRead ) )
126
+ . WithTransactionOpenMode ( transactionOpenMode . GetValueOrDefault ( TransactionOpenMode . New ) )
127
+ . Execute (
128
+ session => {
129
+ session . EnsureTransactionIsStarted ( ) ;
130
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
131
+ if ( first ) {
132
+ if ( wait1 != null && wait2 != null ) {
133
+ _ = wait1 . Set ( ) ;
134
+ _ = wait2 . WaitOne ( ) ;
135
+ }
136
+ }
137
+ else if ( wait1 != null && wait2 != null ) {
138
+ _ = wait2 . Set ( ) ;
139
+ _ = wait1 . WaitOne ( ) ;
140
+ }
141
+ action ( first , isolationLevel , transactionOpenMode ) ;
142
+ } ) ;
111
143
}
112
144
113
145
public void Run (
@@ -135,7 +167,7 @@ public void UniqueConstraintViolation(
135
167
session => {
136
168
_ = Interlocked . Increment ( ref Count ) ;
137
169
session . EnsureTransactionIsStarted ( ) ;
138
- _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
170
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
139
171
var name = "test" ;
140
172
if ( ! session . Query . All < Foo > ( ) . Any ( a => a . Name == name ) ) {
141
173
if ( first ) {
@@ -150,7 +182,7 @@ public void UniqueConstraintViolation(
150
182
_ = wait1 . WaitOne ( ) ;
151
183
wait2 = null ;
152
184
}
153
- _ = new Foo ( session ) { Name = name } ;
185
+ _ = new Foo ( session ) { Name = name } ;
154
186
}
155
187
session . SaveChanges ( ) ;
156
188
} ) ;
@@ -163,19 +195,19 @@ public void UniqueConstraintViolationPrimaryKey(
163
195
session => {
164
196
_ = Interlocked . Increment ( ref Count ) ;
165
197
session . EnsureTransactionIsStarted ( ) ;
166
- _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
198
+ _ = new Bar2 ( session , DateTime . Now , Guid . NewGuid ( ) ) { Name = Guid . NewGuid ( ) . ToString ( ) } ;
167
199
var id = 10 ;
168
- if ( session . Query . SingleOrDefault < Foo > ( id ) == null ) {
200
+ if ( session . Query . SingleOrDefault < Foo > ( id ) == null ) {
169
201
var w1 = wait1 ;
170
202
var w2 = wait2 ;
171
203
if ( first ) {
172
- if ( w1 != null && w2 != null ) {
204
+ if ( w1 != null && w2 != null ) {
173
205
_ = w1 . Set ( ) ;
174
206
_ = w2 . WaitOne ( ) ;
175
207
wait1 = null ;
176
208
}
177
209
}
178
- else if ( w1 != null && w2 != null ) {
210
+ else if ( w1 != null && w2 != null ) {
179
211
_ = w2 . Set ( ) ;
180
212
_ = w1 . WaitOne ( ) ;
181
213
wait2 = null ;
@@ -278,8 +310,9 @@ public void Test()
278
310
b ,
279
311
null ,
280
312
open ,
281
- ( b1 , level1 , open1 ) =>
313
+ ( s , b1 , level1 , open1 ) =>
282
314
context . Parent (
315
+ s ,
283
316
b1 ,
284
317
IsolationLevel . Snapshot ,
285
318
open1 ,
@@ -290,30 +323,24 @@ public void Test()
290
323
291
324
//ExternalWithTransaction nested snapshot UniqueConstraint
292
325
context = new Context ( Domain ) ;
293
- var ex =
294
- Assert . Throws < AggregateException > (
295
- ( ) =>
296
- context . Run (
297
- IsolationLevel . Snapshot ,
298
- null ,
299
- ( b , level , open ) =>
300
- context . External (
301
- b ,
302
- level ,
303
- open ,
304
- ( b1 , level1 , open1 ) =>
305
- context . Parent (
306
- b1 ,
307
- level1 ,
308
- open1 ,
309
- ExecuteActionStrategy . HandleUniqueConstraintViolation ,
310
- context . UniqueConstraintViolation ) ) ) ) ;
311
- Assert . That (
312
- ex . InnerExceptions . Single ( ) ,
313
- Is . TypeOf < InvalidOperationException > ( ) . Or . TypeOf < StorageException > ( ) . With . Property ( "InnerException" ) . TypeOf
314
- < InvalidOperationException > ( ) ) ;
315
- Assert . That ( context . Count , Is . EqualTo ( 2 ) ) ;
316
- Assert . That ( Bar2Count ( ) , Is . EqualTo ( 3 ) ) ;
326
+ context . Run (
327
+ IsolationLevel . Snapshot ,
328
+ null ,
329
+ ( b , level , open ) =>
330
+ context . External (
331
+ b ,
332
+ level ,
333
+ open ,
334
+ ( s , b1 , level1 , open1 ) =>
335
+ context . Parent (
336
+ s ,
337
+ b1 ,
338
+ level1 ,
339
+ open1 ,
340
+ ExecuteActionStrategy . HandleUniqueConstraintViolation ,
341
+ context . UniqueConstraintViolation ) ) ) ;
342
+ Assert . That ( context . Count , Is . EqualTo ( 3 ) ) ;
343
+ Assert . That ( Bar2Count ( ) , Is . EqualTo ( 6 ) ) ;
317
344
318
345
//nested UniqueConstraint with auto transaction
319
346
context = new Context ( Domain ) ;
@@ -350,8 +377,8 @@ public void UniqueConstraintViolationExceptionPrimary()
350
377
public void UniqueConstraintViolationExceptionUnique ( )
351
378
{
352
379
var i = 0 ;
353
- var b = false ;
354
- ExecuteActionStrategy . HandleUniqueConstraintViolation . Error += ( sender , args ) => b = true ;
380
+ var errorNotified = false ;
381
+ ExecuteActionStrategy . HandleUniqueConstraintViolation . Error += ( sender , args ) => errorNotified = true ;
355
382
Domain . WithStrategy ( ExecuteActionStrategy . HandleUniqueConstraintViolation ) . Execute (
356
383
session => {
357
384
_ = new Foo ( session ) { Name = "test" } ;
@@ -361,11 +388,11 @@ public void UniqueConstraintViolationExceptionUnique()
361
388
}
362
389
} ) ;
363
390
Assert . That ( i , Is . EqualTo ( 5 ) ) ;
364
- Assert . That ( b , Is . True ) ;
391
+ Assert . That ( errorNotified , Is . True ) ;
365
392
}
366
393
}
367
394
}
368
395
369
396
// ReSharper restore ReturnValueOfPureMethodIsNotUsed
370
397
// ReSharper restore ConvertToConstant.Local
371
- // ReSharper restore AccessToModifiedClosure
398
+ // ReSharper restore AccessToModifiedClosure
0 commit comments