Method CommitAsync
CommitAsync(CancellationToken)
Commits the staged transaction, making the files it added and deleted permanent in the repository. Call this after the application's database has committed.
Declaration
public Task CommitAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | A cancellation token that cancels the call while it waits for another call on the transaction to finish. Once the commit has started it is not observed. |
Returns
| Type | Description |
|---|---|
| Task |
Implements
Remarks
The transaction's journal is committed first, by renaming it, which is a single atomic operation however many files it lists. The added files are committed next, and then the deletions are applied according to DeleteMode: in Immediate mode the files are deleted now and the journal is removed; in the deferred modes the committed journal stays as the delete record of its files, and a cleanup pass deletes them once it is older than the delete delay.
A failure to commit the journal is thrown; nothing else has been committed in that case and the call can be retried. A failure to commit an added file is not thrown: it is reported through TransactionCompletionFailed, as for an ordinary transaction, and the file is left for the cleaner to resolve. A deletion is never reported once the journal is committed, because the journal has committed every deletion it records: a file that cannot be deleted in Immediate mode, whatever the reason, is not a failure. The journal is kept then, and a cleanup pass deletes the file without asking; it also removes a journal that could not be removed although its files are gone. Calling this method again after it succeeded does nothing.
If the repository cannot be accessed, for example because its volume has stopped answering, the commit of the files waits for it for up to MaxAccessWaitOrRetryTime once for all of them, not once for each, counting only the time it waits. Once that time is used up, each further file is attempted once without waiting: an added file that cannot be committed is reported and left for the cleaner as above, and a file that cannot be deleted is left to the journal.
Once this method has started the commit, the transaction is never rolled back, whatever the method throws. If it is not called again, disposing the transaction leaves the files to the cleaner. After a failure to commit the journal, that depends on how far the step came: if the rename failed, the journal is still staged, and the cleaner resolves its files through the resolution callback; if the journal was renamed and could not be flushed to disk, it is committed although the call threw, and the cleaner deletes its files without asking. The added files are resolved through the callback in both cases. An exception thrown by a TransactionCompletionFailed handler propagates once the transaction is committed.
An OperationCanceledException is thrown before the commit has started: the transaction is still staged, and the call can be made again. Disposing the transaction instead does not roll it back: it leaves the record of the deletions and the added files for the cleaner, which resolves them through the resolution callback. Pass None here, since the application's database has committed, so that a cancellation does not leave to the cleaner what the commit would have finished.
On a version 1 repository, which has no journal (see FileRepoStagedTransaction), the added files are committed first and then each deletion is committed as by CommitAsync(CancellationToken); every failure is reported rather than thrown.
Exceptions
| Type | Condition |
|---|---|
| IOException | The journal could not be committed. |
| OperationCanceledException | The token was cancelled before the commit had started. Nothing has been committed. |
| InvalidOperationException | The transaction has been rolled back. |
| ObjectDisposedException | The transaction was disposed before the stage was committed. |