Skip to content

Commit 842ad94

Browse files
committed
v0.8.2
1 parent 12cb1b6 commit 842ad94

3 files changed

Lines changed: 55 additions & 98 deletions

File tree

‎src/NuExt.System.Data.SQLite.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<RootNamespace />
99
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1010
<NoWarn>$(NoWarn);1591;NETSDK1233</NoWarn>
11-
<VersionPrefix>0.8.1</VersionPrefix>
11+
<VersionPrefix>0.8.2</VersionPrefix>
1212
</PropertyGroup>
1313

1414
<ItemGroup>
1515
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
1616
</ItemGroup>
1717

1818
<ItemGroup Condition="'$(NuExt_UseLocalProjects)' != 'true'">
19-
<PackageReference Include="NuExt.System.Data" Version="0.8.1" />
19+
<PackageReference Include="NuExt.System.Data" Version="0.8.2" />
2020
</ItemGroup>
2121

2222
<ItemGroup Condition="'$(NuExt_UseLocalProjects)' == 'true'">

‎src/System/Data/SQLite/SQLiteDbConnectionExtensions.cs‎

Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,7 @@ public static void ExecuteInTransaction(this SQLiteDbConnection connection, Acti
4141
catch (Exception ex)
4242
{
4343
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
44-
if (transaction.IsActive)
45-
{
46-
try
47-
{
48-
transaction.Rollback();
49-
}
50-
catch (Exception rollbackException)
51-
{
52-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
53-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
54-
}
55-
}
44+
transaction.TryRollback(ex);
5645
throw;
5746
}
5847
}
@@ -102,18 +91,7 @@ public static void ExecuteInTransaction<TState>(this SQLiteDbConnection connecti
10291
catch (Exception ex)
10392
{
10493
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
105-
if (transaction.IsActive)
106-
{
107-
try
108-
{
109-
transaction.Rollback();
110-
}
111-
catch (Exception rollbackException)
112-
{
113-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
114-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
115-
}
116-
}
94+
transaction.TryRollback(ex);
11795
throw;
11896
}
11997
}
@@ -162,18 +140,7 @@ public static void ExecuteInTransaction(this SQLiteDbConnection connection, Isol
162140
catch (Exception ex)
163141
{
164142
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
165-
if (transaction.IsActive)
166-
{
167-
try
168-
{
169-
transaction.Rollback();
170-
}
171-
catch (Exception rollbackException)
172-
{
173-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
174-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
175-
}
176-
}
143+
transaction.TryRollback(ex);
177144
throw;
178145
}
179146
}
@@ -224,18 +191,7 @@ public static void ExecuteInTransaction<TState>(this SQLiteDbConnection connecti
224191
catch (Exception ex)
225192
{
226193
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
227-
if (transaction.IsActive)
228-
{
229-
try
230-
{
231-
transaction.Rollback();
232-
}
233-
catch (Exception rollbackException)
234-
{
235-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
236-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
237-
}
238-
}
194+
transaction.TryRollback(ex);
239195
throw;
240196
}
241197
}
@@ -285,18 +241,7 @@ public static T ExecuteInTransaction<T>(this SQLiteDbConnection connection, Func
285241
catch (Exception ex)
286242
{
287243
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
288-
if (transaction.IsActive)
289-
{
290-
try
291-
{
292-
transaction.Rollback();
293-
}
294-
catch (Exception rollbackException)
295-
{
296-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
297-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
298-
}
299-
}
244+
transaction.TryRollback(ex);
300245
throw;
301246
}
302247
}
@@ -348,18 +293,7 @@ public static TResult ExecuteInTransaction<TState, TResult>(this SQLiteDbConnect
348293
catch (Exception ex)
349294
{
350295
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
351-
if (transaction.IsActive)
352-
{
353-
try
354-
{
355-
transaction.Rollback();
356-
}
357-
catch (Exception rollbackException)
358-
{
359-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
360-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
361-
}
362-
}
296+
transaction.TryRollback(ex);
363297
throw;
364298
}
365299
}
@@ -410,18 +344,7 @@ public static T ExecuteInTransaction<T>(this SQLiteDbConnection connection, Isol
410344
catch (Exception ex)
411345
{
412346
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
413-
if (transaction.IsActive)
414-
{
415-
try
416-
{
417-
transaction.Rollback();
418-
}
419-
catch (Exception rollbackException)
420-
{
421-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
422-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
423-
}
424-
}
347+
transaction.TryRollback(ex);
425348
throw;
426349
}
427350
}
@@ -474,18 +397,7 @@ public static TResult ExecuteInTransaction<TState, TResult>(this SQLiteDbConnect
474397
catch (Exception ex)
475398
{
476399
Debug.Assert(ex is OperationCanceledException || connection.AssertsDisabled, ex.GetType() + ": " + ex.Message);
477-
if (transaction.IsActive)
478-
{
479-
try
480-
{
481-
transaction.Rollback();
482-
}
483-
catch (Exception rollbackException)
484-
{
485-
Trace.WriteLine($"SQLite transaction rollback failed: {rollbackException}");
486-
Debug.Fail($"SQLite transaction rollback failed: {rollbackException}");
487-
}
488-
}
400+
transaction.TryRollback(ex);
489401
throw;
490402
}
491403
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Diagnostics;
2+
3+
namespace System.Data.SQLite;
4+
5+
/// <summary>
6+
/// Provides extension methods for SQLite database transactions.
7+
/// </summary>
8+
public static class SQLiteDbTransactionExtensions
9+
{
10+
/// <summary>
11+
/// Attempts to roll back the active transaction.
12+
/// </summary>
13+
/// <param name="transaction">The SQLite database transaction.</param>
14+
/// <param name="originalException">The exception that caused the rollback attempt, if any.</param>
15+
/// <returns>
16+
/// <see langword="true"/> if the transaction was rolled back;
17+
/// otherwise, <see langword="false"/>.
18+
/// </returns>
19+
/// <exception cref="ArgumentNullException"><paramref name="transaction"/> is <see langword="null"/>.</exception>
20+
public static bool TryRollback(this SQLiteDbTransaction transaction, Exception? originalException = null)
21+
{
22+
ArgumentNullException.ThrowIfNull(transaction);
23+
24+
if (!transaction.IsActive)
25+
{
26+
return false;
27+
}
28+
29+
try
30+
{
31+
transaction.Rollback();
32+
return true;
33+
}
34+
catch (Exception rollbackException)
35+
{
36+
string message = originalException is not null
37+
? $"SQLite transaction rollback failed after '{originalException.GetType()}': {rollbackException}"
38+
: $"SQLite transaction rollback failed: {rollbackException}";
39+
40+
Trace.WriteLine(message);
41+
Debug.Fail(message);
42+
return false;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)