Path: redb/redb.SQLite/Data/SqliteRedbTransaction.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.SQLite/Data/SqliteRedbTransaction.cs ---- using Microsoft.Data.Sqlite; using redb.Core.Data; using System; using System.Threading.Tasks; namespace redb.SQLite.Data { /// /// SQLite implementation of IRedbTransaction using Sqlite. /// Connection-scoped: parent connection automatically uses this transaction. /// public class SqliteRedbTransaction : IRedbTransaction { private readonly SqliteTransaction _transaction; private readonly Action _onDispose; private bool _completionReported; // Reports the outcome once to the callbacks registered for this transaction (cache publication after commit). private void Complete(bool committed) { if (_completionReported) return; _completionReported = true; redb.Core.Data.TransactionCompletion.Completed(this, committed); } private bool _disposed = false; /// /// Transaction unique identifier. /// public Guid TransactionId { get; } = Guid.NewGuid(); /// /// Whether transaction is still active. /// public bool IsActive { get; private set; } = true; /// /// Transaction start time. /// public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow; /// /// Internal Sqlite transaction (for command assignment). /// internal SqliteTransaction SqliteTransaction => _transaction; /// /// Create new transaction wrapper. /// /// Underlying Sqlite transaction. /// Callback when transaction is disposed. public SqliteRedbTransaction(SqliteTransaction transaction, Action onDispose) { _transaction = transaction ?? throw new ArgumentNullException(nameof(transaction)); _onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose)); } /// /// Commit all changes. /// public async Task CommitAsync() { if (!IsActive) throw new InvalidOperationException("Transaction is not active. Already committed or rolled back."); try { await _transaction.CommitAsync(); } catch (Exception ex) { // Commit failed: the underlying sqlite3 handle may still have autocommit=0. // Speculatively rollback so the pooled SqliteConnection returns clean. // Both errors are visible via [Diag-TX-LIFECYCLE] so the source is traceable. // Console.WriteLine($"[Diag-TX-LIFECYCLE] CommitAsync FAILED: {ex.GetType().Name}: {ex.Message}. Attempting speculative rollback."); Complete(committed: false); try { await _transaction.RollbackAsync(); } catch (Exception rbEx) { // Console.WriteLine($"[Diag-TX-LIFECYCLE] Speculative rollback after failed commit ALSO FAILED: {rbEx.GetType().Name}: {rbEx.Message}. Pool may receive a dirty handle (mitigated by SqliteDataSource.EnsureCleanTransactionState on next acquire)."); } IsActive = false; _onDispose(); throw; } Complete(committed: true); IsActive = false; // Clear the connection's `_currentTransaction` slot now — any // command issued between commit and dispose must NOT bind to this // (now closed) SqliteTransaction. DisposeAsync is idempotent on // _onDispose (the callback just nulls a field). _onDispose(); } /// /// Rollback all changes. /// public async Task RollbackAsync() { if (!IsActive) throw new InvalidOperationException("Transaction is not active. Already committed or rolled back."); Complete(committed: false); try { await _transaction.RollbackAsync(); } catch (Exception ex) { // Console.WriteLine($"[Diag-TX-LIFECYCLE] RollbackAsync FAILED: {ex.GetType().Name}: {ex.Message}. Pool may receive a dirty handle (mitigated by SqliteDataSource.EnsureCleanTransactionState on next acquire)."); IsActive = false; _onDispose(); throw; } IsActive = false; _onDispose(); } /// /// Dispose transaction. /// If still active - rollback automatically. /// public async ValueTask DisposeAsync() { if (_disposed) return; _disposed = true; Complete(committed: false); // Auto-rollback if still active. The catch CANNOT throw (Dispose contract), // so leaks here are mitigated by SqliteDataSource.EnsureCleanTransactionState // which runs a speculative ROLLBACK on every pooled connection acquire. if (IsActive) { try { await _transaction.RollbackAsync(); } catch (Exception ex) { // Console.WriteLine($"[Diag-TX-LIFECYCLE] DisposeAsync auto-rollback FAILED: {ex.GetType().Name}: {ex.Message}. Pool poisoning mitigated by SqliteDataSource.EnsureCleanTransactionState on next acquire."); } IsActive = false; } // Disposing a broken transaction must never block the connection's cleanup callback: // _onDispose() clears _currentTransaction on the connection so it can return to the pool. // finally guarantees that even if _transaction.DisposeAsync() throws — and the exception // is NOT swallowed, it propagates so the fault stays observable. try { await _transaction.DisposeAsync(); } finally { _onDispose(); } } } }