-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add APIs for supporting transaction savepoints. * Support is implemented at the EF level only, no System.Data support yet (this should come soon). * Use savepoints in the update pipeline when a user-managed transaction is used, to roll back to before SaveChanges in case of exception. Part of #20176
- Loading branch information
Showing
17 changed files
with
762 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/EFCore.SqlServer/Storage/Internal/SqlServerTransaction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Data.Common; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal | ||
{ | ||
public class SqlServerTransaction : RelationalTransaction, IDbContextTransaction | ||
{ | ||
private readonly DbTransaction _dbTransaction; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public SqlServerTransaction( | ||
[NotNull] IRelationalConnection connection, | ||
[NotNull] DbTransaction transaction, | ||
Guid transactionId, | ||
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, | ||
bool transactionOwned) | ||
: base(connection, transaction, transactionId, logger, transactionOwned) | ||
=> _dbTransaction = transaction; | ||
|
||
/// <inheritdoc /> | ||
public virtual void Save(string savepointName) | ||
{ | ||
using var command = Connection.DbConnection.CreateCommand(); | ||
command.Transaction = _dbTransaction; | ||
command.CommandText = "SAVE TRANSACTION " + savepointName; | ||
command.ExecuteNonQuery(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual async Task SaveAsync(string savepointName, CancellationToken cancellationToken = default) | ||
{ | ||
using var command = Connection.DbConnection.CreateCommand(); | ||
command.Transaction = _dbTransaction; | ||
command.CommandText = "SAVE TRANSACTION " + savepointName; | ||
await command.ExecuteNonQueryAsync(cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual void Rollback(string savepointName) | ||
{ | ||
using var command = Connection.DbConnection.CreateCommand(); | ||
command.Transaction = _dbTransaction; | ||
command.CommandText = "ROLLBACK TRANSACTION " + savepointName; | ||
command.ExecuteNonQuery(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual async Task RollbackAsync(string savepointName, CancellationToken cancellationToken = default) | ||
{ | ||
using var command = Connection.DbConnection.CreateCommand(); | ||
command.Transaction = _dbTransaction; | ||
command.CommandText = "ROLLBACK TRANSACTION " + savepointName; | ||
await command.ExecuteNonQueryAsync(cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual bool AreSavepointsSupported => true; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/EFCore.SqlServer/Storage/Internal/SqlServerTransactionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Data.Common; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal | ||
{ | ||
public class SqlServerTransactionFactory : IRelationalTransactionFactory | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual RelationalTransaction Create( | ||
IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> logger, bool transactionOwned) | ||
=> new SqlServerTransaction(connection, transaction, transactionId, logger, transactionOwned); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.