diff --git a/src/libraries/System.Data.Common/ref/System.Data.Common.cs b/src/libraries/System.Data.Common/ref/System.Data.Common.cs index a2f519c8c897e..51be327e1446d 100644 --- a/src/libraries/System.Data.Common/ref/System.Data.Common.cs +++ b/src/libraries/System.Data.Common/ref/System.Data.Common.cs @@ -2441,6 +2441,13 @@ protected virtual void Dispose(bool disposing) { } public virtual System.Threading.Tasks.ValueTask DisposeAsync() { throw null; } public abstract void Rollback(); public virtual System.Threading.Tasks.Task RollbackAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual bool SupportsSavepoints { get { throw null; } } + public virtual System.Threading.Tasks.Task SaveAsync(string savepointName, System.Threading.CancellationToken cancellationToken = default) { throw null; } + public virtual System.Threading.Tasks.Task RollbackAsync(string savepointName, System.Threading.CancellationToken cancellationToken = default) { throw null; } + public virtual System.Threading.Tasks.Task ReleaseAsync(string savepointName, System.Threading.CancellationToken cancellationToken = default) { throw null; } + public virtual void Save(string savepointName) { throw null; } + public virtual void Rollback(string savepointName) { throw null; } + public virtual void Release(string savepointName) { throw null; } } public enum GroupByBehavior { diff --git a/src/libraries/System.Data.Common/src/System/Data/Common/DbTransaction.cs b/src/libraries/System.Data.Common/src/System/Data/Common/DbTransaction.cs index fe357a63413c8..8728590fedebb 100644 --- a/src/libraries/System.Data.Common/src/System/Data/Common/DbTransaction.cs +++ b/src/libraries/System.Data.Common/src/System/Data/Common/DbTransaction.cs @@ -67,5 +67,121 @@ public virtual Task RollbackAsync(CancellationToken cancellationToken = default) return Task.FromException(e); } } + + #region Savepoints + + /// + /// Gets a value that indicates whether this instance supports database savepoints. + /// If , the methods , + /// and as + /// well as their synchronous counterparts are expected to throw . + /// + /// + /// if this instance supports database savepoints; otherwise, + /// . + /// + public virtual bool SupportsSavepoints => false; + + /// + /// Creates a savepoint in the transaction. This allows all commands that are executed after the savepoint was + /// established to be rolled back, restoring the transaction state to what it was at the time of the savepoint. + /// + /// The name of the savepoint to be created. + /// + /// An optional token to cancel the asynchronous operation. The default value is . + /// + /// A representing the asynchronous operation. + public virtual Task SaveAsync(string savepointName, CancellationToken cancellationToken = default) + { + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); + } + + try + { + Save(savepointName); + return Task.CompletedTask; + } + catch (Exception e) + { + return Task.FromException(e); + } + } + + /// + /// Rolls back all commands that were executed after the specified savepoint was established. + /// + /// The name of the savepoint to roll back to. + /// + /// An optional token to cancel the asynchronous operation. The default value is . + /// + /// A representing the asynchronous operation. + public virtual Task RollbackAsync(string savepointName, CancellationToken cancellationToken = default) + { + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); + } + + try + { + Rollback(savepointName); + return Task.CompletedTask; + } + catch (Exception e) + { + return Task.FromException(e); + } + } + + /// + /// Destroys a savepoint previously defined in the current transaction. This allows the system to + /// reclaim some resources before the transaction ends. + /// + /// The name of the savepoint to release. + /// + /// An optional token to cancel the asynchronous operation. The default value is . + /// + /// A representing the asynchronous operation. + public virtual Task ReleaseAsync(string savepointName, CancellationToken cancellationToken = default) + { + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); + } + + try + { + Release(savepointName); + return Task.CompletedTask; + } + catch (Exception e) + { + return Task.FromException(e); + } + } + + /// + /// Creates a savepoint in the transaction. This allows all commands that are executed after the savepoint was + /// established to be rolled back, restoring the transaction state to what it was at the time of the savepoint. + /// + /// The name of the savepoint to be created. + public virtual void Save(string savepointName) => throw new NotSupportedException(); + + /// + /// Rolls back all commands that were executed after the specified savepoint was established. + /// + /// The name of the savepoint to roll back to. + public virtual void Rollback(string savepointName) => throw new NotSupportedException(); + + /// + /// Destroys a savepoint previously defined in the current transaction. This allows the system to + /// reclaim some resources before the transaction ends. + /// + /// The name of the savepoint to release. + public virtual void Release(string savepointName) {} + + #endregion } }