-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transaction handler to shared components. (#17)
- Loading branch information
1 parent
f19a1fe
commit 640c9bb
Showing
10 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Health.Abstractions/Exceptions/TransactionFailedException.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,15 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Health.Abstractions.Exceptions | ||
{ | ||
public class TransactionFailedException : MicrosoftHealthException | ||
{ | ||
public TransactionFailedException() | ||
: base(Resources.TransactionProcessingException) | ||
{ | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.Health.Abstractions/Features/Transactions/ITransactionHandler.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,14 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
|
||
namespace Microsoft.Health.Abstractions.Features.Transactions | ||
{ | ||
public interface ITransactionHandler : IDisposable | ||
{ | ||
ITransactionScope BeginTransaction(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.Health.Abstractions/Features/Transactions/ITransactionScope.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,14 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
|
||
namespace Microsoft.Health.Abstractions.Features.Transactions | ||
{ | ||
public interface ITransactionScope : IDisposable | ||
{ | ||
void Complete(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
88 changes: 88 additions & 0 deletions
88
src/Microsoft.Health.SqlServer/Features/Client/SqlConnectionWrapper.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,88 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using EnsureThat; | ||
using Microsoft.Health.SqlServer.Configs; | ||
using Microsoft.Health.SqlServer.Features.Storage; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Client | ||
{ | ||
public class SqlConnectionWrapper : IDisposable | ||
{ | ||
private readonly bool _enlistInTransactionIfPresent; | ||
private readonly SqlTransactionHandler _sqlTransactionHandler; | ||
|
||
public SqlConnectionWrapper(SqlServerDataStoreConfiguration configuration, SqlTransactionHandler sqlTransactionHandler, bool enlistInTransactionIfPresent) | ||
{ | ||
EnsureArg.IsNotNull(configuration, nameof(configuration)); | ||
EnsureArg.IsNotNull(sqlTransactionHandler, nameof(sqlTransactionHandler)); | ||
|
||
_sqlTransactionHandler = sqlTransactionHandler; | ||
_enlistInTransactionIfPresent = enlistInTransactionIfPresent; | ||
|
||
if (_enlistInTransactionIfPresent && sqlTransactionHandler.SqlTransactionScope?.SqlConnection != null) | ||
{ | ||
SqlConnection = sqlTransactionHandler.SqlTransactionScope.SqlConnection; | ||
} | ||
else | ||
{ | ||
SqlConnection = new SqlConnection(configuration.ConnectionString); | ||
} | ||
|
||
if (_enlistInTransactionIfPresent && sqlTransactionHandler.SqlTransactionScope != null && sqlTransactionHandler.SqlTransactionScope.SqlConnection == null) | ||
{ | ||
sqlTransactionHandler.SqlTransactionScope.SqlConnection = SqlConnection; | ||
} | ||
|
||
if (SqlConnection.State != ConnectionState.Open) | ||
{ | ||
SqlConnection.Open(); | ||
} | ||
|
||
if (enlistInTransactionIfPresent && sqlTransactionHandler.SqlTransactionScope != null) | ||
{ | ||
SqlTransaction = sqlTransactionHandler.SqlTransactionScope.SqlTransaction ?? SqlConnection.BeginTransaction(); | ||
|
||
if (sqlTransactionHandler.SqlTransactionScope.SqlTransaction == null) | ||
{ | ||
sqlTransactionHandler.SqlTransactionScope.SqlTransaction = SqlTransaction; | ||
} | ||
} | ||
} | ||
|
||
public SqlConnection SqlConnection { get; } | ||
|
||
public SqlTransaction SqlTransaction { get; } | ||
|
||
public SqlCommand CreateSqlCommand() | ||
{ | ||
SqlCommand sqlCommand = SqlConnection.CreateCommand(); | ||
sqlCommand.Transaction = SqlTransaction; | ||
|
||
return sqlCommand; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
if (!_enlistInTransactionIfPresent || _sqlTransactionHandler.SqlTransactionScope == null) | ||
{ | ||
SqlConnection?.Dispose(); | ||
SqlTransaction?.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Microsoft.Health.SqlServer/Features/Client/SqlConnectionWrapperFactory.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,31 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using EnsureThat; | ||
using Microsoft.Health.SqlServer.Configs; | ||
using Microsoft.Health.SqlServer.Features.Storage; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Client | ||
{ | ||
public class SqlConnectionWrapperFactory | ||
{ | ||
private readonly SqlServerDataStoreConfiguration _configuration; | ||
private readonly SqlTransactionHandler _sqlTransactionHandler; | ||
|
||
public SqlConnectionWrapperFactory(SqlServerDataStoreConfiguration configuration, SqlTransactionHandler sqlTransactionHandler) | ||
{ | ||
EnsureArg.IsNotNull(configuration, nameof(configuration)); | ||
EnsureArg.IsNotNull(sqlTransactionHandler, nameof(sqlTransactionHandler)); | ||
|
||
_configuration = configuration; | ||
_sqlTransactionHandler = sqlTransactionHandler; | ||
} | ||
|
||
public SqlConnectionWrapper ObtainSqlConnectionWrapper(bool enlistInTransaction = false) | ||
{ | ||
return new SqlConnectionWrapper(_configuration, _sqlTransactionHandler, enlistInTransaction); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Microsoft.Health.SqlServer/Features/Storage/SqlTransactionHandler.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,47 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.Health.Abstractions.Exceptions; | ||
using Microsoft.Health.Abstractions.Features.Transactions; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Storage | ||
{ | ||
public class SqlTransactionHandler : ITransactionHandler | ||
{ | ||
public SqlTransactionScope SqlTransactionScope { get; private set; } | ||
|
||
public ITransactionScope BeginTransaction() | ||
{ | ||
Debug.Assert(SqlTransactionScope == null, "The existing SQL transaction scope should be completed before starting a new transaction."); | ||
|
||
if (SqlTransactionScope != null) | ||
{ | ||
throw new TransactionFailedException(); | ||
} | ||
|
||
SqlTransactionScope = new SqlTransactionScope(this); | ||
|
||
return SqlTransactionScope; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
SqlTransactionScope?.Dispose(); | ||
|
||
SqlTransactionScope = null; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Microsoft.Health.SqlServer/Features/Storage/SqlTransactionScope.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,61 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Data.SqlClient; | ||
using EnsureThat; | ||
using Microsoft.Health.Abstractions.Features.Transactions; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Storage | ||
{ | ||
public class SqlTransactionScope : ITransactionScope | ||
{ | ||
private bool _isDisposed; | ||
private readonly SqlTransactionHandler _sqlTransactionHandler; | ||
|
||
public SqlTransactionScope(SqlTransactionHandler sqlTransactionHandler) | ||
{ | ||
EnsureArg.IsNotNull(sqlTransactionHandler, nameof(SqlTransactionHandler)); | ||
|
||
_sqlTransactionHandler = sqlTransactionHandler; | ||
} | ||
|
||
public SqlConnection SqlConnection { get; set; } | ||
|
||
public SqlTransaction SqlTransaction { get; set; } | ||
|
||
public void Complete() | ||
{ | ||
SqlTransaction?.Commit(); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
if (_isDisposed) | ||
{ | ||
return; | ||
} | ||
|
||
SqlConnection?.Dispose(); | ||
SqlTransaction?.Dispose(); | ||
|
||
SqlConnection = null; | ||
SqlTransaction = null; | ||
|
||
_isDisposed = true; | ||
|
||
_sqlTransactionHandler.Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
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