-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global transaction management (#9)
Introduced IGlobalTransactionManager interface and its implementation to manage global transactions. Refactored the existing VaultTransaction class and updated dependency injection to support the new global transaction management.
- Loading branch information
1 parent
202b5af
commit 3ade825
Showing
7 changed files
with
120 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MongoFlow; | ||
|
||
public interface IGlobalTransactionManager | ||
{ | ||
IVaultTransaction? CurrentTransaction { get; } | ||
Task<IVaultTransaction> BeginAsync(CancellationToken cancellationToken = default); | ||
} |
5 changes: 4 additions & 1 deletion
5
...re/Transactions/IMongoVaultTransaction.cs → src/Core/Transactions/IVaultTransaction.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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
using MongoDB.Driver; | ||
|
||
namespace MongoFlow; | ||
|
||
public interface IMongoVaultTransaction : IDisposable | ||
public interface IVaultTransaction : IDisposable | ||
{ | ||
Task CommitAsync(CancellationToken cancellationToken = default); | ||
Task RollbackAsync(CancellationToken cancellationToken = default); | ||
IClientSessionHandle Session { get; } | ||
} |
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,35 @@ | ||
using MongoDB.Driver; | ||
|
||
namespace MongoFlow; | ||
|
||
internal sealed class MongoGlobalTransaction : IVaultTransaction | ||
{ | ||
public MongoGlobalTransaction(IClientSessionHandle session) | ||
{ | ||
if (session.IsInTransaction) | ||
{ | ||
throw new InvalidOperationException("MongoVaultTransaction can only be created from an inactive transaction."); | ||
} | ||
|
||
session.StartTransaction(); | ||
|
||
Session = session; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Session.Dispose(); | ||
} | ||
|
||
public async Task CommitAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await Session.CommitTransactionAsync(cancellationToken); | ||
} | ||
|
||
public async Task RollbackAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await Session.AbortTransactionAsync(cancellationToken); | ||
} | ||
|
||
public IClientSessionHandle Session { get; } | ||
} |
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,43 @@ | ||
using MongoDB.Driver; | ||
|
||
namespace MongoFlow; | ||
|
||
internal sealed class MongoGlobalTransactionManager : IGlobalTransactionManager, IDisposable | ||
{ | ||
private readonly MongoClient _mongoClient; | ||
private readonly SemaphoreSlim _semaphore; | ||
|
||
public MongoGlobalTransactionManager(MongoClient mongoClient) | ||
{ | ||
_mongoClient = mongoClient; | ||
_semaphore = new SemaphoreSlim(1, 1); | ||
} | ||
|
||
public IVaultTransaction? CurrentTransaction { get; private set; } | ||
public async Task<IVaultTransaction> BeginAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await _semaphore.WaitAsync(cancellationToken); | ||
try | ||
{ | ||
if (CurrentTransaction != null) | ||
{ | ||
throw new InvalidOperationException("Transaction already started."); | ||
} | ||
|
||
var session = await _mongoClient.StartSessionAsync(cancellationToken: cancellationToken); | ||
CurrentTransaction = new MongoGlobalTransaction(session); | ||
return CurrentTransaction; | ||
} | ||
finally | ||
{ | ||
_semaphore.Release(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_mongoClient.Dispose(); | ||
_semaphore.Dispose(); | ||
CurrentTransaction?.Dispose(); | ||
} | ||
} |
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