diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d057157 Binary files /dev/null and b/.DS_Store differ diff --git a/source/Nevermore/Advanced/WriteTransaction.cs b/source/Nevermore/Advanced/WriteTransaction.cs index 2befdd7..e80c2ad 100644 --- a/source/Nevermore/Advanced/WriteTransaction.cs +++ b/source/Nevermore/Advanced/WriteTransaction.cs @@ -337,31 +337,57 @@ public async ValueTask AllocateIdAsync(string tableName, string idPrefix var key = await keyAllocator.NextIdAsync(tableName, cancellationToken).ConfigureAwait(false); return $"{idPrefix}-{key}"; } + + public void CommitIfOwned() + { + if (!OwnsSqlTransaction) return; + + if (!configuration.AllowSynchronousOperations) + throw new SynchronousOperationsDisabledException(); + + CommitTransactionAndRunHooks(); + } public void Commit() { - if (Transaction is null) - throw new InvalidOperationException("There is no current transaction, call Open/OpenAsync to start a transaction"); - if (!OwnsSqlTransaction) throw new InvalidOperationException($"{nameof(WriteTransaction)} cannot commit a transaction it does not own"); if (!configuration.AllowSynchronousOperations) throw new SynchronousOperationsDisabledException(); + CommitTransactionAndRunHooks(); + } + + void CommitTransactionAndRunHooks() + { + if (Transaction is null) + throw new InvalidOperationException("There is no current transaction, call Open/OpenAsync to start a transaction"); + configuration.Hooks.BeforeCommit(this); Transaction.Commit(); configuration.Hooks.AfterCommit(this); } - public async Task CommitAsync(CancellationToken cancellationToken = default) + public async Task CommitIfOwnedAsync(CancellationToken cancellationToken = default) { - if (Transaction is null) - throw new InvalidOperationException("There is no current transaction, call Open/OpenAsync to start a transaction"); + if (!OwnsSqlTransaction) return; + + await CommitTransactionAndRunHooksAsync(cancellationToken).ConfigureAwait(false); + } + public async Task CommitAsync(CancellationToken cancellationToken = default) + { if (!OwnsSqlTransaction) throw new InvalidOperationException($"{nameof(WriteTransaction)} cannot commit a transaction it does not own"); + await CommitTransactionAndRunHooksAsync(cancellationToken).ConfigureAwait(false); + } + async Task CommitTransactionAndRunHooksAsync(CancellationToken cancellationToken) + { + if (Transaction is null) + throw new InvalidOperationException("There is no current transaction, call Open/OpenAsync to start a transaction"); + await configuration.Hooks.BeforeCommitAsync(this).ConfigureAwait(false); await Transaction.CommitAsync(cancellationToken).ConfigureAwait(false); await configuration.Hooks.AfterCommitAsync(this).ConfigureAwait(false); diff --git a/source/Nevermore/IWriteTransaction.cs b/source/Nevermore/IWriteTransaction.cs index 45f900f..6f3da32 100644 --- a/source/Nevermore/IWriteTransaction.cs +++ b/source/Nevermore/IWriteTransaction.cs @@ -5,6 +5,8 @@ namespace Nevermore { public interface IWriteTransaction : IReadTransaction, IWriteQueryExecutor { + void CommitIfOwned(); + Task CommitIfOwnedAsync(CancellationToken cancellationToken = default); void Commit(); Task CommitAsync(CancellationToken cancellationToken = default); }