1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Imports
|
28
|
|
using System;
|
29
|
|
using System.EnterpriseServices;
|
30
|
|
using System.Runtime.Remoting.Messaging;
|
31
|
|
|
32
|
|
using log4net;
|
33
|
|
#endregion
|
34
|
|
|
35
|
|
namespace IBatisNet.Common.Transaction
|
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
|
|
public class TransactionScope : IDisposable
|
76
|
|
{
|
77
|
|
#region Consts
|
78
|
|
private const string TX_SCOPE_COUNT = "_TX_SCOPE_COUNT_";
|
79
|
|
private bool _consistent = false;
|
80
|
|
#endregion
|
81
|
|
|
82
|
|
#region Fields
|
83
|
|
private bool _closed = false;
|
84
|
|
private TransactionScopeOptions _txScopeOptions;
|
85
|
|
private TransactionOptions _txOptions;
|
86
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
87
|
|
#endregion
|
88
|
|
|
89
|
|
#region Properties
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
0
|
private bool Consistent
|
100
|
|
{
|
101
|
|
set
|
102
|
|
{
|
103
|
|
_consistent = value;
|
104
|
|
}
|
105
|
|
}
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
0
|
public int TransactionScopeCount
|
111
|
|
{
|
112
|
|
get
|
113
|
|
{
|
114
|
|
object count = CallContext.GetData(TX_SCOPE_COUNT);
|
115
|
|
return (count == null) ? 0 : (int)count;
|
116
|
|
}
|
117
|
|
set
|
118
|
|
{
|
119
|
|
CallContext.SetData(TX_SCOPE_COUNT, value);
|
120
|
|
}
|
121
|
|
}
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
0
|
public static bool IsInTransaction
|
127
|
|
{
|
128
|
|
get
|
129
|
|
{
|
130
|
|
return ContextUtil.IsInTransaction;
|
131
|
|
}
|
132
|
|
}
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
0
|
public bool IsVoteCommit
|
139
|
|
{
|
140
|
|
get
|
141
|
|
{
|
142
|
|
return ContextUtil.MyTransactionVote == TransactionVote.Commit;
|
143
|
|
}
|
144
|
|
}
|
145
|
|
#endregion
|
146
|
|
|
147
|
|
#region Constructor (s) / Destructor
|
148
|
|
|
149
|
|
|
150
|
|
|
151
|
|
|
152
|
0
|
public TransactionScope()
|
153
|
|
{
|
154
|
|
_txOptions = new TransactionOptions();
|
155
|
|
_txOptions.IsolationLevel = IsolationLevel.ReadCommitted;
|
156
|
|
_txOptions.TimeOut = new TimeSpan(0,0,0,15);
|
157
|
|
|
158
|
|
_txScopeOptions = TransactionScopeOptions.Required;
|
159
|
|
|
160
|
|
EnterTransactionContext();
|
161
|
|
}
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
0
|
public TransactionScope(TransactionScopeOptions txScopeOptions)
|
171
|
|
{
|
172
|
|
_txOptions = new TransactionOptions();
|
173
|
|
_txOptions.IsolationLevel = IsolationLevel.ReadCommitted;
|
174
|
|
_txOptions.TimeOut = new TimeSpan(0,0,0,15);
|
175
|
|
|
176
|
|
_txScopeOptions = txScopeOptions;
|
177
|
|
|
178
|
|
EnterTransactionContext();
|
179
|
|
}
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
0
|
public TransactionScope(TransactionScopeOptions txScopeOptions, TransactionOptions options)
|
188
|
|
{
|
189
|
|
_txOptions = options;
|
190
|
|
_txScopeOptions = txScopeOptions;
|
191
|
|
|
192
|
|
EnterTransactionContext();
|
193
|
|
}
|
194
|
|
#endregion
|
195
|
|
|
196
|
|
#region Methods
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
0
|
private void EnterTransactionContext()
|
202
|
|
{
|
203
|
|
if (++TransactionScopeCount == 1)
|
204
|
|
{
|
205
|
|
if (_logger.IsDebugEnabled)
|
206
|
|
{
|
207
|
|
_logger.Debug("Create a new ServiceConfig in ServiceDomain.");
|
208
|
|
}
|
209
|
|
|
210
|
|
ServiceConfig config = new ServiceConfig();
|
211
|
|
|
212
|
|
config.TrackingEnabled = true;
|
213
|
|
config.TrackingAppName = "iBATIS.NET";
|
214
|
|
config.TrackingComponentName = "TransactionScope";
|
215
|
|
config.TransactionDescription = "iBATIS.NET Distributed Transaction";
|
216
|
|
config.Transaction = TransactionScopeOptions2TransactionOption( _txScopeOptions );
|
217
|
|
config.TransactionTimeout = _txOptions.TimeOut.Seconds;
|
218
|
|
config.IsolationLevel = IsolationLevel2TransactionIsolationLevel( _txOptions.IsolationLevel );
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
ServiceDomain.Enter(config);
|
224
|
|
}
|
225
|
|
|
226
|
|
_closed = false;
|
227
|
|
|
228
|
|
if (_logger.IsDebugEnabled)
|
229
|
|
{
|
230
|
|
_logger.Debug("Open TransactionScope :"+ContextUtil.ContextId);
|
231
|
|
}
|
232
|
|
}
|
233
|
|
|
234
|
|
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
|
|
239
|
|
|
240
|
|
|
241
|
0
|
private TransactionOption TransactionScopeOptions2TransactionOption(TransactionScopeOptions transactionScopeOptions)
|
242
|
|
{
|
243
|
|
TransactionOption transactionOption;
|
244
|
|
|
245
|
|
switch(transactionScopeOptions)
|
246
|
|
{
|
247
|
|
case TransactionScopeOptions.Mandatory:
|
248
|
|
throw new NotImplementedException("Will be used in Indigo.");
|
249
|
|
case TransactionScopeOptions.NotSupported:
|
250
|
|
transactionOption = TransactionOption.NotSupported;
|
251
|
|
break;
|
252
|
|
case TransactionScopeOptions.Required:
|
253
|
|
transactionOption = TransactionOption.Required;
|
254
|
|
break;
|
255
|
|
case TransactionScopeOptions.RequiresNew:
|
256
|
|
transactionOption = TransactionOption.RequiresNew;
|
257
|
|
break;
|
258
|
|
case TransactionScopeOptions.Supported:
|
259
|
|
transactionOption = TransactionOption.Supported;
|
260
|
|
break;
|
261
|
|
default:
|
262
|
|
transactionOption = TransactionOption.Required;
|
263
|
|
break;
|
264
|
|
}
|
265
|
|
return transactionOption;
|
266
|
|
}
|
267
|
|
|
268
|
|
|
269
|
|
|
270
|
|
|
271
|
|
|
272
|
|
|
273
|
0
|
private TransactionIsolationLevel IsolationLevel2TransactionIsolationLevel(IsolationLevel isolation)
|
274
|
|
{
|
275
|
|
TransactionIsolationLevel isolationLevel;
|
276
|
|
|
277
|
|
switch(isolation)
|
278
|
|
{
|
279
|
|
case IsolationLevel.ReadCommitted:
|
280
|
|
isolationLevel = TransactionIsolationLevel.ReadCommitted;
|
281
|
|
break;
|
282
|
|
case IsolationLevel.ReadUncommitted:
|
283
|
|
isolationLevel = TransactionIsolationLevel.ReadUncommitted;
|
284
|
|
break;
|
285
|
|
case IsolationLevel.RepeatableRead:
|
286
|
|
isolationLevel = TransactionIsolationLevel.RepeatableRead;
|
287
|
|
break;
|
288
|
|
case IsolationLevel.Serializable:
|
289
|
|
isolationLevel = TransactionIsolationLevel.Serializable;
|
290
|
|
break;
|
291
|
|
case IsolationLevel.Unspecified:
|
292
|
|
throw new NotImplementedException("Will be used in Indigo.");
|
293
|
|
default:
|
294
|
|
isolationLevel = TransactionIsolationLevel.ReadCommitted;
|
295
|
|
break;
|
296
|
|
}
|
297
|
|
return isolationLevel;
|
298
|
|
}
|
299
|
|
|
300
|
|
|
301
|
|
|
302
|
|
|
303
|
0
|
public void Close()
|
304
|
|
{
|
305
|
|
if (_closed == false)
|
306
|
|
{
|
307
|
|
if (_logger.IsDebugEnabled)
|
308
|
|
{
|
309
|
|
_logger.Debug("Close TransactionScope");
|
310
|
|
}
|
311
|
|
|
312
|
|
if (ContextUtil.IsInTransaction)
|
313
|
|
{
|
314
|
|
if (_consistent==true && (this.IsVoteCommit))
|
315
|
|
{
|
316
|
|
ContextUtil.EnableCommit();
|
317
|
|
}
|
318
|
|
else
|
319
|
|
{
|
320
|
|
ContextUtil.DisableCommit();
|
321
|
|
}
|
322
|
|
}
|
323
|
|
|
324
|
|
if (0 == --TransactionScopeCount)
|
325
|
|
{
|
326
|
|
if (_logger.IsDebugEnabled)
|
327
|
|
{
|
328
|
|
_logger.Debug("Leave in ServiceDomain ");
|
329
|
|
}
|
330
|
|
|
331
|
|
ServiceDomain.Leave();
|
332
|
|
}
|
333
|
|
|
334
|
|
_closed = true;
|
335
|
|
}
|
336
|
|
}
|
337
|
|
|
338
|
|
|
339
|
|
|
340
|
|
|
341
|
0
|
public void Complete()
|
342
|
|
{
|
343
|
|
this.Consistent = true;
|
344
|
|
}
|
345
|
|
#endregion
|
346
|
|
|
347
|
|
#region IDisposable Members
|
348
|
|
|
349
|
|
|
350
|
|
|
351
|
|
|
352
|
0
|
void IDisposable.Dispose()
|
353
|
|
{
|
354
|
|
Close();
|
355
|
|
}
|
356
|
|
#endregion
|
357
|
|
|
358
|
|
}
|
359
|
|
}
|
360
|
|
|