0
|
1 using System;
|
|
2 using System.Data;
|
|
3
|
|
4 using JetBrains.Annotations;
|
|
5
|
|
6 namespace BLToolkit.Data.Linq
|
|
7 {
|
|
8 public class DataContextTransaction : IDisposable
|
|
9 {
|
|
10 public DataContextTransaction([NotNull] DataContext dataContext)
|
|
11 {
|
|
12 if (dataContext == null) throw new ArgumentNullException("dataContext");
|
|
13
|
|
14 DataContext = dataContext;
|
|
15 }
|
|
16
|
|
17 public DataContext DataContext { get; set; }
|
|
18
|
|
19 int _transactionCounter;
|
|
20
|
|
21 public void BeginTransaction()
|
|
22 {
|
|
23 var db = DataContext.GetDBManager();
|
|
24
|
|
25 db.BeginTransaction();
|
|
26
|
|
27 if (_transactionCounter == 0)
|
|
28 DataContext.LockDbManagerCounter++;
|
|
29
|
|
30 _transactionCounter++;
|
|
31 }
|
|
32
|
|
33 public void BeginTransaction(IsolationLevel level)
|
|
34 {
|
|
35 var db = DataContext.GetDBManager();
|
|
36
|
|
37 db.BeginTransaction(level);
|
|
38
|
|
39 if (_transactionCounter == 0)
|
|
40 DataContext.LockDbManagerCounter++;
|
|
41
|
|
42 _transactionCounter++;
|
|
43 }
|
|
44
|
|
45 public void CommitTransaction()
|
|
46 {
|
|
47 if (_transactionCounter > 0)
|
|
48 {
|
|
49 var db = DataContext.GetDBManager();
|
|
50
|
|
51 db.CommitTransaction();
|
|
52
|
|
53 _transactionCounter--;
|
|
54
|
|
55 if (_transactionCounter == 0)
|
|
56 {
|
|
57 DataContext.LockDbManagerCounter--;
|
|
58 DataContext.ReleaseQuery();
|
|
59 }
|
|
60 }
|
|
61 }
|
|
62
|
|
63 public void RollbackTransaction()
|
|
64 {
|
|
65 if (_transactionCounter > 0)
|
|
66 {
|
|
67 var db = DataContext.GetDBManager();
|
|
68
|
|
69 db.RollbackTransaction();
|
|
70
|
|
71 _transactionCounter--;
|
|
72
|
|
73 if (_transactionCounter == 0)
|
|
74 {
|
|
75 DataContext.LockDbManagerCounter--;
|
|
76 DataContext.ReleaseQuery();
|
|
77 }
|
|
78 }
|
|
79 }
|
|
80
|
|
81 public void Dispose()
|
|
82 {
|
|
83 if (_transactionCounter > 0)
|
|
84 {
|
|
85 var db = DataContext.GetDBManager();
|
|
86
|
|
87 db.RollbackTransaction();
|
|
88
|
|
89 _transactionCounter = 0;
|
|
90
|
|
91 DataContext.LockDbManagerCounter--;
|
|
92 }
|
|
93 }
|
|
94 }
|
|
95 }
|