Browse Source

Add RelationDbTransaction and extensions

master
Savorboard 6 years ago
parent
commit
2ec580648a
2 changed files with 78 additions and 0 deletions
  1. +38
    -0
      src/DotNetCore.CAP/Internal/RelationDbTransaction.cs
  2. +40
    -0
      src/DotNetCore.CAP/TransactionExtensions.cs

+ 38
- 0
src/DotNetCore.CAP/Internal/RelationDbTransaction.cs View File

@@ -0,0 +1,38 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Data;

namespace DotNetCore.CAP.Internal
{
internal class RelationDbTransaction : IDbTransaction
{
private readonly ICapTransaction _capTransaction;

public RelationDbTransaction(ICapTransaction capTransaction)
{
_capTransaction = capTransaction;
var dbTransaction = (IDbTransaction) capTransaction.DbTransaction;
Connection = dbTransaction.Connection;
IsolationLevel = dbTransaction.IsolationLevel;
}

public void Dispose()
{
_capTransaction.Dispose();
}

public void Commit()
{
_capTransaction.Commit();
}

public void Rollback()
{
_capTransaction.Rollback();
}

public IDbConnection Connection { get; }
public IsolationLevel IsolationLevel { get; }
}
}

+ 40
- 0
src/DotNetCore.CAP/TransactionExtensions.cs View File

@@ -0,0 +1,40 @@
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Data;
using DotNetCore.CAP.Internal;

namespace DotNetCore.CAP
{
public static class TransactionExtensions
{
public static ICapTransaction Begin(this ICapTransaction transaction,
IDbTransaction dbTransaction, bool autoCommit = false)
{

transaction.DbTransaction = dbTransaction;
transaction.AutoCommit = autoCommit;

return transaction;
}

public static IDbTransaction JoinToTransaction(this IDbTransaction dbTransaction,
ICapPublisher publisher, bool autoCommit = false)
{
dbTransaction = new RelationDbTransaction(publisher.Transaction.Begin(dbTransaction, autoCommit));
return dbTransaction;
}

public static IDbTransaction BeginAndJoinToTransaction(this IDbConnection dbConnection,
ICapPublisher publisher, bool autoCommit = false)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}

var dbTransaction = dbConnection.BeginTransaction();
return dbTransaction.JoinToTransaction(publisher, autoCommit);
}
}
}

Loading…
Cancel
Save