-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteEnlistment.cs
More file actions
executable file
·248 lines (206 loc) · 6.84 KB
/
Copy pathSQLiteEnlistment.cs
File metadata and controls
executable file
·248 lines (206 loc) · 6.84 KB
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
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
#if !PLATFORM_COMPACTFRAMEWORK
namespace System.Data.SQLite
{
using System.Globalization;
using System.Transactions;
internal sealed class SQLiteEnlistment : IDisposable, IEnlistmentNotification
{
internal SQLiteTransaction _transaction;
internal Transaction _scope;
internal bool _disposeConnection;
internal SQLiteEnlistment(
SQLiteConnection cnn,
Transaction scope,
System.Data.IsolationLevel defaultIsolationLevel,
bool throwOnUnavailable,
bool throwOnUnsupported
)
{
_transaction = cnn.BeginTransaction(GetSystemDataIsolationLevel(
cnn, scope, defaultIsolationLevel, throwOnUnavailable,
throwOnUnsupported));
_scope = scope;
_scope.EnlistVolatile(this, EnlistmentOptions.None);
}
///////////////////////////////////////////////////////////////////////////
#region Private Methods
private System.Data.IsolationLevel GetSystemDataIsolationLevel(
SQLiteConnection connection,
Transaction transaction,
System.Data.IsolationLevel defaultIsolationLevel,
bool throwOnUnavailable,
bool throwOnUnsupported
)
{
if (transaction == null)
{
//
// NOTE: If neither the transaction nor connection isolation
// level is available, throw an exception if instructed
// by the caller.
//
if (connection != null)
return connection.GetDefaultIsolationLevel();
if (throwOnUnavailable)
{
throw new InvalidOperationException(
"isolation level is unavailable");
}
return defaultIsolationLevel;
}
IsolationLevel isolationLevel = transaction.IsolationLevel;
//
// TODO: Are these isolation level mappings actually correct?
//
switch (isolationLevel)
{
case IsolationLevel.Unspecified:
return System.Data.IsolationLevel.Unspecified;
case IsolationLevel.Chaos:
return System.Data.IsolationLevel.Chaos;
case IsolationLevel.ReadUncommitted:
return System.Data.IsolationLevel.ReadUncommitted;
case IsolationLevel.ReadCommitted:
return System.Data.IsolationLevel.ReadCommitted;
case IsolationLevel.RepeatableRead:
return System.Data.IsolationLevel.RepeatableRead;
case IsolationLevel.Serializable:
return System.Data.IsolationLevel.Serializable;
case IsolationLevel.Snapshot:
return System.Data.IsolationLevel.Snapshot;
}
//
// NOTE: When in "strict" mode, throw an exception if the isolation
// level is not recognized; otherwise, fallback to the default
// isolation level specified by the caller.
//
if (throwOnUnsupported)
{
throw new InvalidOperationException(
HelperMethods.StringFormat(CultureInfo.CurrentCulture,
"unsupported isolation level {0}", isolationLevel));
}
return defaultIsolationLevel;
}
///////////////////////////////////////////////////////////////////////////
private void Cleanup(SQLiteConnection cnn)
{
if (_disposeConnection)
cnn.Dispose();
_transaction = null;
_scope = null;
}
#endregion
///////////////////////////////////////////////////////////////////////////
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
///////////////////////////////////////////////////////////////////////////
#region IDisposable "Pattern" Members
private bool disposed;
private void CheckDisposed() /* throw */
{
#if THROW_ON_DISPOSED
if (disposed)
throw new ObjectDisposedException(typeof(SQLiteEnlistment).Name);
#endif
}
///////////////////////////////////////////////////////////////////////////
private /* protected virtual */ void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
////////////////////////////////////
// dispose managed resources here...
////////////////////////////////////
if (_transaction != null)
{
_transaction.Dispose();
_transaction = null;
}
if (_scope != null)
{
// _scope.Dispose(); // NOTE: Not "owned" by us.
_scope = null;
}
}
//////////////////////////////////////
// release unmanaged resources here...
//////////////////////////////////////
disposed = true;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////
#region Destructor
~SQLiteEnlistment()
{
Dispose(false);
}
#endregion
///////////////////////////////////////////////////////////////////////////
#region IEnlistmentNotification Members
public void Commit(Enlistment enlistment)
{
CheckDisposed();
SQLiteConnection cnn = _transaction.Connection;
cnn._enlistment = null;
try
{
_transaction.IsValid(true);
_transaction.Connection._transactionLevel = 1;
_transaction.Commit();
enlistment.Done();
}
finally
{
Cleanup(cnn);
}
}
///////////////////////////////////////////////////////////////////////////
public void InDoubt(Enlistment enlistment)
{
CheckDisposed();
enlistment.Done();
}
///////////////////////////////////////////////////////////////////////////
public void Prepare(PreparingEnlistment preparingEnlistment)
{
CheckDisposed();
if (_transaction.IsValid(false) == false)
preparingEnlistment.ForceRollback();
else
preparingEnlistment.Prepared();
}
///////////////////////////////////////////////////////////////////////////
public void Rollback(Enlistment enlistment)
{
CheckDisposed();
SQLiteConnection cnn = _transaction.Connection;
cnn._enlistment = null;
try
{
_transaction.Rollback();
enlistment.Done();
}
finally
{
Cleanup(cnn);
}
}
#endregion
}
}
#endif // !PLATFORM_COMPACT_FRAMEWORK