File: OptimisticConcurrence.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (423 lines) | stat: -rw-r--r-- 17,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test_NUnit;
using NUnit.Framework;
using nwind;
using Test_NUnit.Linq_101_Samples;

// test ns Linq_101_Samples
#if MYSQL
    namespace Test_NUnit_MySql.Linq_101_Samples
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP.Linq_101_Samples
#elif ORACLE
    namespace Test_NUnit_Oracle.Linq_101_Samples
#elif POSTGRES
    namespace Test_NUnit_PostgreSql.Linq_101_Samples
#elif SQLITE
    namespace Test_NUnit_Sqlite.Linq_101_Samples
#elif INGRES
    namespace Test_NUnit_Ingres.Linq_101_Samples
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict.Linq_101_Samples
#elif MSSQL
    namespace Test_NUnit_MsSql.Linq_101_Samples
#elif FIREBIRD
    namespace Test_NUnit_Firebird.Linq_101_Samples
#endif
{
    [TestFixture]
    public class OptimisticConcurrence:TestBase
    {
        [Test(Description="Get conflict information. This sample demonstrates how to retrieve the changes that lead to an optimistic concurrency exception.")]

        public void LinqToSqlOptimistic01()
        {
            Northwind db=CreateDb();

    Console.WriteLine("YOU:  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    var product = db.Products.First(p=>p.ProductID = 1);
    Console.WriteLine("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    Console.WriteLine();
    Console.WriteLine("OTHER USER: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    // Open a second connection to the database to simulate another user
    // who is going to make changes to the Products table                

    var otherUser_db = Northwind(My.Settings.NORTHWINDConnectionString1) With {.Log = db.Log};
    var otherUser_product = otherUser_db.Products.First(p=>p.ProductID = 1);
    otherUser_product.UnitPrice = 999.99D;
    otherUser_product.UnitsOnOrder = 10;
    otherUser_db.SubmitChanges();
    Console.WriteLine("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    Console.WriteLine("YOU (continued): ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
    product.UnitPrice = 777.77D;

    var conflictOccurred = False;
    try{
        db.SubmitChanges(ConflictMode.ContinueOnConflict)
    catch(ChangeConflictException c)
    {
        Console.WriteLine("* * * OPTIMISTIC CONCURRENCY EXCEPTION * * *")
        For Each aConflict In db.ChangeConflicts
            var prod = CType(aConflict.Object, Product)
            Console.WriteLine("The conflicting product has ProductID {0}", prod.ProductID)
            Console.WriteLine()
            Console.WriteLine("Conflicting members:")
            Console.WriteLine()
            For Each memConflict In aConflict.MemberConflicts
                var name = memConflict.Member.Name
                var yourUpdate = memConflict.CurrentValue.ToString()
                var original = memConflict.OriginalValue.ToString()
                var theirUpdate = memConflict.DatabaseValue.ToString()
                If (memConflict.IsModified) Then

                    Console.WriteLine("//{0}// was updated from {1} to {2} while you updated it to {3}", _
                                          name, original, theirUpdate, yourUpdate)
                Else
                    Console.WriteLine("//{0}// was updated from {1} to {2}, you did not change it.", _
                                                                        name, original, theirUpdate)
                End If
                Console.WriteLine()
            Next
            conflictOccurred = True
        Next

        Console.WriteLine()
        If (Not conflictOccurred) Then

            Console.WriteLine("* * * COMMIT SUCCESSFUL * * *")
            Console.WriteLine("Changes to Product 1 saved.")
        End If
        Console.WriteLine("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ")

        ResetProducts() // clean up
    }
}



[Test(Description="Resolve conflicts: Overwrite current values. This sample demonstrates how to automatically resolve concurrency conflicts. The //overwrite current values// option writes the new database values to the client objects.")]

public void LinqToSqlOptimistic02()
            {
            Northwind db=CreateDb();


    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    db.Log = null

    var product = db.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("You retrieve the product 1, it costs {0}", product.UnitPrice)
    Console.WriteLine("There are {0} units in stock, {1} units on order", product.UnitsInStock, product.UnitsOnOrder)
    Console.WriteLine()

    Console.WriteLine("Another user changes the price to 22.22 and UnitsInStock to 22")
    var otherUser_product = otherUser_db.Products.First(p=>p.ProductID = 1)
    otherUser_product.UnitPrice = 22.22D
    otherUser_product.UnitsInStock = 22
    otherUser_db.SubmitChanges()

    Console.WriteLine("You set the price of product 1 to 1.01 and UnitsOnOrder to 11")
    product.UnitPrice = 1.01D
    product.UnitsOnOrder = 11
    Try
        Console.WriteLine("You submit")
        Console.WriteLine()
        db.SubmitChanges()
    Catch c As ChangeConflictException
        WriteConflictDetails(db.ChangeConflicts)  // write changed objects / members to console
        Console.WriteLine()
        Console.WriteLine("Resolve by overwriting current values")
        db.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues)
        db.SubmitChanges()
    End Try

    Console.WriteLine()
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    var result = dbResult.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("Now product 1 has price={0}, UnitsInStock={1}, UnitsOnOrder={2}", _
        result.UnitPrice, result.UnitsInStock, result.UnitsOnOrder)
    Console.WriteLine()
    ResetProducts() // clean up
}



[Test(Description="Resolve conflicts: Keep current values. This sample demonstrates how to automatically resolve concurrency conflicts. The //keep current values// option changes everything to the values of this client.")]
public void LinqToSqlOptimistic03()
            {
            Northwind db=CreateDb();

    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    db.Log = null

    var Prod = db.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("You retrieve the product 1, it costs {0}", Prod.UnitPrice)
    Console.WriteLine("There are {0} units in stock, {1} units on order", Prod.UnitsInStock, Prod.UnitsOnOrder)
    Console.WriteLine()

    Console.WriteLine("Another user changes the price to 22.22 and UnitsInStock to 22")
    var otherUser_product = otherUser_db.Products.First(p=>p.ProductID = 1)
    otherUser_product.UnitPrice = 22.22D
    otherUser_product.UnitsInStock = 22
    otherUser_db.SubmitChanges()

    Console.WriteLine("You set the price of product 1 to 1.01 and UnitsOnOrder to 11")
    Prod.UnitPrice = 1.01D
    Prod.UnitsOnOrder = 11
    Try
        Console.WriteLine("You submit")
        Console.WriteLine()
        db.SubmitChanges()
    Catch c As ChangeConflictException
        WriteConflictDetails(db.ChangeConflicts) // write changed objects / members to console
        Console.WriteLine()
        Console.WriteLine("Resolve by keeping current values")
        db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues)
        db.SubmitChanges()
    End Try
    Console.WriteLine()
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    var result = dbResult.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("Now product 1 has price={0}, UnitsInStock={1}, UnitsOnOrder={2}", _
        result.UnitPrice, result.UnitsInStock, result.UnitsOnOrder)
    Console.WriteLine()
    ResetProducts() // clean up
}



[Test(Description="Resolve conflicts: Keep changes. This sample demonstrates how to automatically resolve concurrency conflicts. The //keep changes// option keeps all changes from the current user and merges changes from other users if the corresponding field was not changed by the current user.")]

public void LinqToSqlOptimistic04()
            {
            Northwind db=CreateDb();


    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    db.Log = null

    var prod = db.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("You retrieve the product 1, it costs {0}", prod.UnitPrice)
    Console.WriteLine("There are {0} units in stock, {1} units on order", prod.UnitsInStock, prod.UnitsOnOrder)
    Console.WriteLine()

    Console.WriteLine("Another user changes the price to 22.22 and UnitsInStock to 22")
    var otherUser_product = otherUser_db.Products.First(p=>p.ProductID = 1)
    otherUser_product.UnitPrice = 22.22D
    otherUser_product.UnitsInStock = 22
    otherUser_db.SubmitChanges()

    Console.WriteLine("You set the price of product 1 to 1.01 and UnitsOnOrder to 11")
    prod.UnitPrice = 1.01D
    prod.UnitsOnOrder = 11D
    Try
        Console.WriteLine("You submit")
        Console.WriteLine()
        db.SubmitChanges()
    Catch c As ChangeConflictException
        WriteConflictDetails(db.ChangeConflicts) //write changed objects / members to console
        Console.WriteLine()
        Console.WriteLine("Resolve by keeping changes")
        db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges)
        db.SubmitChanges()
    End Try
    Console.WriteLine()
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    var result = dbResult.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("Now product 1 has price={0}, UnitsInStock={1}, UnitsOnOrder={2}", _
        result.UnitPrice, result.UnitsInStock, result.UnitsOnOrder)
    Console.WriteLine()
    ResetProducts() // clean up
}



[Test(Description="Custom resolve rule. Demonstrates using MemberConflict.Resolve to write a custom resolve rule.")]

public void LinqToSqlOptimistic05()
            {
            Northwind db=CreateDb();


    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    db.Log = null

    var prod = db.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("You retrieve the product 1, it costs {0}", prod.UnitPrice)
    Console.WriteLine("There are {0} units in stock, {1} units on order", prod.UnitsInStock, prod.UnitsOnOrder)
    Console.WriteLine()

    Console.WriteLine("Another user changes the price to 22.22 and UnitsOnOrder to 2")
    var otherUser_product = otherUser_db.Products.First(p=>p.ProductID = 1)
    otherUser_product.UnitPrice = 22.22D
    otherUser_product.UnitsOnOrder = 2
    otherUser_db.SubmitChanges()

    Console.WriteLine("You set the price of product 1 to 1.01 and UnitsOnOrder to 11")
    prod.UnitPrice = 1.01D
    prod.UnitsOnOrder = 11
    var needsSubmit = True
    While needsSubmit
        Try
            Console.WriteLine("You submit")
            Console.WriteLine()
            needsSubmit = False
            db.SubmitChanges()
        Catch c As ChangeConflictException
            needsSubmit = True
            WriteConflictDetails(db.ChangeConflicts) // write changed objects / members to console
            Console.WriteLine()
            Console.WriteLine("Resolve by higher price / order")
            For Each conflict In db.ChangeConflicts
                conflict.Resolve(RefreshMode.KeepChanges)
                For Each memConflict In conflict.MemberConflicts
                    If (memConflict.Member.Name = "UnitPrice") Then
                        //always use the highest price
                        var theirPrice = CDec(memConflict.DatabaseValue)
                        var yourPrice = CDec(memConflict.CurrentValue)
                        memConflict.Resolve(Math.Max(theirPrice, yourPrice))
                    ElseIf (memConflict.Member.Name = "UnitsOnOrder") Then
                        //always use higher order
                        var theirOrder = CShort(memConflict.DatabaseValue)
                        var yourOrder = CShort(memConflict.CurrentValue)
                        memConflict.Resolve(Math.Max(theirOrder, yourOrder))
                    End If
                Next
            Next
        End Try
    End While
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    var result = dbResult.Products.First(p=>p.ProductID = 1)
    Console.WriteLine("Now product 1 has price={0}, UnitsOnOrder={1}", _
        result.UnitPrice, result.UnitsOnOrder)
    Console.WriteLine()
    ResetProducts() //clean up
}



[Test(Description="Submit with FailOnFirstConflict. Submit(FailOnFirstConflict) throws an Optimistic Concurrency Exception when the first conflict is detected. Only one exception is handled at a time, you have to submit for each conflict.")]

public void LinqToSqlOptimistic06()
            {
            Northwind db=CreateDb();


    db.Log = null
    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)

    //you load 3 products
    var prod() = db.Products.OrderBy(p=>p.ProductID).Take(3).ToArray()
    For i = 0 To 2
        Console.WriteLine("You retrieve the product {0}, it costs {1}", i + 1, prod(i).UnitPrice)
    Next
    //other user changes these products
    var otherUserProd() = otherUser_db.Products.OrderBy(p=>p.ProductID).Take(3).ToArray()
    For i = 0 To 2
        var otherPrice = (i + 1) * 111.11D
        Console.WriteLine("Other user changes the price of product {0} to {1}", i + 1, otherPrice)
        otherUserProd(i).UnitPrice = otherPrice
    Next
    otherUser_db.SubmitChanges()
    Console.WriteLine("Other user submitted changes")

    //you change your loaded products
    For i = 0 To 2
        var yourPrice = (i + 1) * 1.01D
        Console.WriteLine("You set the price of product {0} to {1}", i + 1, yourPrice)
        prod(i).UnitPrice = yourPrice
    Next

    // submit
    var needsSubmit = True
    While needsSubmit
        Try
            Console.WriteLine("======= You submit with FailOnFirstConflict =======")
            needsSubmit = False
            db.SubmitChanges(ConflictMode.FailOnFirstConflict)
        Catch c As ChangeConflictException
            For Each conflict In db.ChangeConflicts

                DescribeConflict(conflict) //write changes to console
                Console.WriteLine("Resolve conflict with KeepCurrentValues")
                conflict.Resolve(RefreshMode.KeepCurrentValues)
            Next
            needsSubmit = True
        End Try
    End While
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    For i = 0 To 2
        //Creating a temporary since this will be used in a lambda
        var tmp = i
        var result = dbResult.Products.First(p=>p.ProductID = tmp + 1)
        Console.WriteLine("Now the product {0} has price {1}", i + 1, result.UnitPrice)
    Next
    ResetProducts() //clean up
}
c




[Test(Description="Submit with ContinueOnConflict. Submit(ContinueOnConflict) collects all concurrency conflicts and throws an exception when the last conflict is detected.\r\nAll conflicts are handled in one catch statement. It is still possible that another user updated the same objects before this update, so it is possible that another Optimistic Concurrency Exception is thrown which would need to be handled again.")]

public void LinqToSqlOptimistic07()
            {
            Northwind db=CreateDb();

    db.Log = null
    var otherUser_db = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)

    // you load 3 products
    var prod() = db.Products.OrderBy(p=>p.ProductID).Take(3).ToArray()
    For i = 0 To 2
        Console.WriteLine("You retrieve the product {0}, it costs {1}", i + 1, prod(i).UnitPrice)
    Next
    // other user changes these products
    var otherUserProd() = otherUser_db.Products.OrderBy(p=>p.ProductID).Take(3).ToArray()
    For i = 0 To 2
        var otherPrice = (i + 1) * 111.11D
        Console.WriteLine("Other user changes the price of product {0} to {1}", i + 1, otherPrice)
        otherUserProd(i).UnitPrice = otherPrice
    Next
    otherUser_db.SubmitChanges()
    Console.WriteLine("Other user submitted changes")

    // you change your loaded products
    For i = 0 To 2
        var yourPrice = (i + 1) * 1.01D
        Console.WriteLine("You set the price of product {0} to {1}", i + 1, yourPrice)
        prod(i).UnitPrice = yourPrice
    Next
    // submit
    var needsSubmit = True
    While needsSubmit
        Try
            Console.WriteLine("======= You submit with ContinueOnConflict =======")
            needsSubmit = False
            db.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch c As ChangeConflictException
            For Each conflict In db.ChangeConflicts
                DescribeConflict(conflict) // write changes to console
                Console.WriteLine("Resolve conflict with KeepCurrentValues")
                conflict.Resolve(RefreshMode.KeepCurrentValues)
            Next
            needsSubmit = True
        End Try
    End While
    var dbResult = New NorthwindDataContext(My.Settings.NORTHWINDConnectionString1)
    For i = 0 To 2
        var tmp = i
        var result = dbResult.Products.First(p=>p.ProductID = tmp + 1)
        Console.WriteLine("Now the product {0} has price {1}", i + 1, result.UnitPrice)
    Next

    ResetProducts() //clean up
}


    }
}