/
OleDbTransaction.cs
373 lines (340 loc) · 11.6 KB
/
OleDbTransaction.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Data.Common;
using System.Data.ProviderBase;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Data.OleDb
{
public sealed class OleDbTransaction : DbTransaction
{
private readonly OleDbTransaction? _parentTransaction; // strong reference to keep parent alive
private readonly System.Data.IsolationLevel _isolationLevel;
private WeakReference? _nestedTransaction; // child transactions
private WrappedTransaction? _transaction;
internal OleDbConnection _parentConnection;
private sealed class WrappedTransaction : WrappedIUnknown
{
private bool _mustComplete;
internal WrappedTransaction(UnsafeNativeMethods.ITransactionLocal transaction, int isolevel, out OleDbHResult hr) : base(transaction)
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{ }
finally
{
hr = transaction.StartTransaction(isolevel, 0, IntPtr.Zero, out _);
if (0 <= hr)
{
_mustComplete = true;
}
}
}
internal bool MustComplete
{
get { return _mustComplete; }
}
internal OleDbHResult Abort()
{
Debug.Assert(_mustComplete, "transaction already completed");
OleDbHResult hr;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
DangerousAddRef(ref mustRelease);
RuntimeHelpers.PrepareConstrainedRegions();
try
{ }
finally
{
hr = (OleDbHResult)NativeOledbWrapper.ITransactionAbort(DangerousGetHandle());
_mustComplete = false;
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
return hr;
}
internal OleDbHResult Commit()
{
Debug.Assert(_mustComplete, "transaction already completed");
OleDbHResult hr;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
DangerousAddRef(ref mustRelease);
RuntimeHelpers.PrepareConstrainedRegions();
try
{ }
finally
{
hr = (OleDbHResult)NativeOledbWrapper.ITransactionCommit(DangerousGetHandle());
if ((0 <= (int)hr) || (OleDbHResult.XACT_E_NOTRANSACTION == hr))
{
_mustComplete = false;
}
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
return hr;
}
protected override bool ReleaseHandle()
{
if (_mustComplete && (IntPtr.Zero != base.handle))
{
NativeOledbWrapper.ITransactionAbort(base.handle);
_mustComplete = false;
}
return base.ReleaseHandle();
}
}
internal OleDbTransaction(OleDbConnection connection, OleDbTransaction? transaction, IsolationLevel isolevel)
{
_parentConnection = connection;
_parentTransaction = transaction;
switch (isolevel)
{
case IsolationLevel.Unspecified: // OLE DB doesn't support this isolevel on local transactions
isolevel = IsolationLevel.ReadCommitted;
break;
case IsolationLevel.Chaos:
case IsolationLevel.ReadUncommitted:
case IsolationLevel.ReadCommitted:
case IsolationLevel.RepeatableRead:
case IsolationLevel.Serializable:
case IsolationLevel.Snapshot:
break;
default:
throw ADP.InvalidIsolationLevel(isolevel);
}
_isolationLevel = isolevel;
}
public new OleDbConnection? Connection
{
get
{
return _parentConnection;
}
}
protected override DbConnection? DbConnection
{
get
{
return Connection;
}
}
public override IsolationLevel IsolationLevel
{
get
{
if (null == _transaction)
{
throw ADP.TransactionZombied(this);
}
return _isolationLevel;
}
}
internal OleDbTransaction? Parent
{
get
{
return _parentTransaction;
}
}
public OleDbTransaction Begin(IsolationLevel isolevel)
{
if (null == _transaction)
{
throw ADP.TransactionZombied(this);
}
else if ((null != _nestedTransaction) && _nestedTransaction.IsAlive)
{
throw ADP.ParallelTransactionsNotSupported(Connection!);
}
// either the connection will be open or this will be a zombie
OleDbTransaction transaction = new OleDbTransaction(_parentConnection, this, isolevel);
_nestedTransaction = new WeakReference(transaction, false);
UnsafeNativeMethods.ITransactionLocal? wrapper = null;
try
{
wrapper = (UnsafeNativeMethods.ITransactionLocal)_transaction.ComWrapper();
transaction.BeginInternal(wrapper);
}
finally
{
if (null != wrapper)
{
Marshal.ReleaseComObject(wrapper);
}
}
return transaction;
}
public OleDbTransaction Begin()
{
return Begin(IsolationLevel.ReadCommitted);
}
internal void BeginInternal(UnsafeNativeMethods.ITransactionLocal transaction)
{
OleDbHResult hr;
_transaction = new WrappedTransaction(transaction, (int)_isolationLevel, out hr);
if (hr < 0)
{
_transaction.Dispose();
_transaction = null;
ProcessResults(hr);
}
}
public override void Commit()
{
if (null == _transaction)
{
throw ADP.TransactionZombied(this);
}
CommitInternal();
}
private void CommitInternal()
{
if (null == _transaction)
{
return;
}
if (null != _nestedTransaction)
{
OleDbTransaction? transaction = (OleDbTransaction?)_nestedTransaction.Target;
if ((null != transaction) && _nestedTransaction.IsAlive)
{
transaction.CommitInternal();
}
_nestedTransaction = null;
}
OleDbHResult hr = _transaction.Commit();
if (!_transaction.MustComplete)
{
_transaction.Dispose();
_transaction = null;
DisposeManaged();
}
if (hr < 0)
{
// if an exception is thrown, user can try to commit their transaction again
ProcessResults(hr);
}
}
/*public OleDbCommand CreateCommand() {
OleDbCommand cmd = Connection.CreateCommand();
cmd.Transaction = this;
return cmd;
}
IDbCommand IDbTransaction.CreateCommand() {
return CreateCommand();
}*/
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeManaged();
RollbackInternal(false);
}
base.Dispose(disposing);
}
private void DisposeManaged()
{
if (null != _parentTransaction)
{
_parentTransaction._nestedTransaction = null;
//_parentTransaction = null;
}
else if (null != _parentConnection)
{
_parentConnection.LocalTransaction = null;
}
_parentConnection = null!;
}
private void ProcessResults(OleDbHResult hr)
{
Exception? e = OleDbConnection.ProcessResults(hr, _parentConnection, this);
if (null != e)
{ throw e; }
}
public override void Rollback()
{
if (null == _transaction)
{
throw ADP.TransactionZombied(this);
}
DisposeManaged();
RollbackInternal(true); // no recover if this throws an exception
}
/*protected virtual*/
internal OleDbHResult RollbackInternal(bool exceptionHandling)
{
OleDbHResult hr = 0;
if (null != _transaction)
{
if (null != _nestedTransaction)
{
OleDbTransaction? transaction = (OleDbTransaction?)_nestedTransaction.Target;
if ((null != transaction) && _nestedTransaction.IsAlive)
{
hr = transaction.RollbackInternal(exceptionHandling);
if (exceptionHandling && (hr < 0))
{
SafeNativeMethods.Wrapper.ClearErrorInfo();
return hr;
}
}
_nestedTransaction = null;
}
hr = _transaction.Abort();
_transaction.Dispose();
_transaction = null;
if (hr < 0)
{
if (exceptionHandling)
{
ProcessResults(hr);
}
else
{
SafeNativeMethods.Wrapper.ClearErrorInfo();
}
}
}
return hr;
}
internal static OleDbTransaction TransactionLast(OleDbTransaction head)
{
if (null != head._nestedTransaction)
{
OleDbTransaction? current = (OleDbTransaction?)head._nestedTransaction.Target;
if ((null != current) && head._nestedTransaction.IsAlive)
{
return TransactionLast(current);
}
}
return head;
}
internal static OleDbTransaction? TransactionUpdate(OleDbTransaction? transaction)
{
if ((null != transaction) && (null == transaction._transaction))
{
return null;
}
return transaction;
}
}
}