@@ -58,18 +58,12 @@ namespace DotNetCore.CAP | |||
return transaction; | |||
} | |||
public static IClientSessionHandle BeginAndJoinToTransaction(this IClientSessionHandle clientSessionHandle, | |||
public static IClientSessionHandle StartTransaction(this IMongoClient client, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
var clientSessionHandle = client.StartSession(); | |||
var capTrans = publisher.Transaction.Begin(clientSessionHandle, autoCommit); | |||
return new CapMongoDbClientSessionHandle(capTrans); | |||
} | |||
public static IClientSessionHandle StartAndJoinToTransaction(this IMongoClient client, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
var clientSessionHandle = client.StartSession(); | |||
return BeginAndJoinToTransaction(clientSessionHandle, publisher, autoCommit); | |||
} | |||
} | |||
} |
@@ -64,12 +64,34 @@ namespace DotNetCore.CAP | |||
return transaction; | |||
} | |||
public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database, | |||
public static ICapTransaction Begin(this ICapTransaction transaction, | |||
IDbTransaction dbTransaction, bool autoCommit = false) | |||
{ | |||
transaction.DbTransaction = dbTransaction; | |||
transaction.AutoCommit = autoCommit; | |||
return transaction; | |||
} | |||
public static IDbContextTransaction BeginTransaction(this DatabaseFacade database, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
var trans = database.BeginTransaction(); | |||
var capTrans = publisher.Transaction.Begin(trans, autoCommit); | |||
return new CapEFDbTransaction(capTrans); | |||
} | |||
public static ICapTransaction BeginTransaction(this IDbConnection dbConnection, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
if (dbConnection.State == ConnectionState.Closed) | |||
{ | |||
dbConnection.Open(); | |||
} | |||
var dbTransaction = dbConnection.BeginTransaction(); | |||
return publisher.Transaction.Begin(dbTransaction, autoCommit); | |||
} | |||
} | |||
} |
@@ -55,6 +55,15 @@ namespace DotNetCore.CAP | |||
public static class CapTransactionExtensions | |||
{ | |||
public static ICapTransaction Begin(this ICapTransaction transaction, | |||
IDbTransaction dbTransaction, bool autoCommit = false) | |||
{ | |||
transaction.DbTransaction = dbTransaction; | |||
transaction.AutoCommit = autoCommit; | |||
return transaction; | |||
} | |||
public static ICapTransaction Begin(this ICapTransaction transaction, | |||
IDbContextTransaction dbTransaction, bool autoCommit = false) | |||
{ | |||
@@ -64,7 +73,19 @@ namespace DotNetCore.CAP | |||
return transaction; | |||
} | |||
public static IDbContextTransaction BeginAndJoinToTransaction(this DatabaseFacade database, | |||
public static ICapTransaction BeginTransaction(this IDbConnection dbConnection, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
if (dbConnection.State == ConnectionState.Closed) | |||
{ | |||
dbConnection.Open(); | |||
} | |||
var dbTransaction = dbConnection.BeginTransaction(); | |||
return publisher.Transaction.Begin(dbTransaction, autoCommit); | |||
} | |||
public static IDbContextTransaction BeginTransaction(this DatabaseFacade database, | |||
ICapPublisher publisher, bool autoCommit = false) | |||
{ | |||
var trans = database.BeginTransaction(); | |||
@@ -19,7 +19,7 @@ namespace DotNetCore.CAP | |||
public object DbTransaction { get; set; } | |||
protected internal void AddToSent(CapPublishedMessage msg) | |||
protected internal virtual void AddToSent(CapPublishedMessage msg) | |||
{ | |||
_bufferList.Add(msg); | |||
} | |||
@@ -1,38 +0,0 @@ | |||
// 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; } | |||
} | |||
} |
@@ -1,40 +0,0 @@ | |||
// 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); | |||
} | |||
} | |||
} |