From 957092f044df34513798dddd95c8a5bb071c9853 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 11 Dec 2022 14:03:56 +0800 Subject: [PATCH] Add db.Ado.BeginTranAsync --- .../Abstract/AdoProvider/AdoProvider.cs | 60 +++++++++++++++++++ Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs | 3 + .../Realization/MySql/MySqlProvider.cs | 21 ------- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 29dfba112..6b873664a 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -180,6 +180,20 @@ namespace SqlSugar } } + public virtual async Task CheckConnectionAsync() + { + if (this.Connection.State != ConnectionState.Open) + { + try + { + await (this.Connection as DbConnection).OpenAsync(); + } + catch (Exception ex) + { + Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message + $"DbType=\"{this.Context.CurrentConnectionConfig.DbType}\";ConfigId=\"{this.Context.CurrentConnectionConfig.ConfigId}\""); + } + } + } #endregion #region Transaction @@ -197,6 +211,12 @@ namespace SqlSugar if (this.Transaction == null) this.Transaction = this.Connection.BeginTransaction(); } + public virtual async Task BeginTranAsync() + { + await CheckConnectionAsync(); + if (this.Transaction == null) + this.Transaction =await (this.Connection as DbConnection).BeginTransactionAsync(); + } public virtual void BeginTran(IsolationLevel iso) { CheckConnection(); @@ -212,6 +232,16 @@ namespace SqlSugar if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Close(); } } + + public virtual async Task RollbackTranAsync() + { + if (this.Transaction != null) + { + await (this.Transaction as DbTransaction).RollbackAsync(); + this.Transaction = null; + if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) await this.CloseAsync(); + } + } public virtual void CommitTran() { if (this.Transaction != null) @@ -221,6 +251,15 @@ namespace SqlSugar if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) this.Close(); } } + public virtual async Task CommitTranAsync() + { + if (this.Transaction != null) + { + await (this.Transaction as DbTransaction).CommitAsync(); + this.Transaction = null; + if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection) await this.CloseAsync(); + } + } #endregion #region abstract @@ -1309,6 +1348,27 @@ namespace SqlSugar #endregion #region Helper + public async Task CloseAsync() + { + if (this.Transaction != null) + { + this.Transaction = null; + } + if (this.Connection != null && this.Connection.State == ConnectionState.Open) + { + await (this.Connection as DbConnection).CloseAsync(); + } + if (this.IsMasterSlaveSeparation && this.SlaveConnections.HasValue()) + { + foreach (var slaveConnection in this.SlaveConnections) + { + if (slaveConnection != null && slaveConnection.State == ConnectionState.Open) + { + await (slaveConnection as DbConnection).CloseAsync(); + } + } + } + } protected virtual void SugarCatch(Exception ex, string sql, SugarParameter[] parameters) { diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs index 10c33d0e0..1da6556b8 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs @@ -171,11 +171,14 @@ namespace SqlSugar void CheckConnection(); void BeginTran(); + Task BeginTranAsync(); void BeginTran(IsolationLevel iso); void BeginTran(string transactionName); void BeginTran(IsolationLevel iso, string transactionName); void RollbackTran(); + Task RollbackTranAsync(); void CommitTran(); + Task CommitTranAsync(); DbResult UseTran(Action action, Action errorCallBack = null); DbResult UseTran(Func action, Action errorCallBack = null); diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs index 4214d01af..f49f3b842 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs @@ -150,27 +150,6 @@ namespace SqlSugar } #region async - public async Task CloseAsync() - { - if (this.Transaction != null) - { - this.Transaction = null; - } - if (this.Connection != null && this.Connection.State == ConnectionState.Open) - { - await (this.Connection as MySqlConnection).CloseAsync(); - } - if (this.IsMasterSlaveSeparation && this.SlaveConnections.HasValue()) - { - foreach (var slaveConnection in this.SlaveConnections) - { - if (slaveConnection != null && slaveConnection.State == ConnectionState.Open) - { - await (slaveConnection as MySqlConnection).CloseAsync(); - } - } - } - } public async Task GetCommandAsync(string sql, SugarParameter[] parameters) { MySqlCommand sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection);