mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-05 03:17:41 +08:00
Synchronization code
This commit is contained in:
@@ -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
|
||||
@@ -264,10 +303,10 @@ namespace SqlSugar
|
||||
var result = new DbResult<bool>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
await this.BeginTranAsync();
|
||||
if (action != null)
|
||||
await action();
|
||||
this.CommitTran();
|
||||
await this.CommitTranAsync();
|
||||
result.Data = result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -275,7 +314,7 @@ namespace SqlSugar
|
||||
result.ErrorException = ex;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
await this.RollbackTranAsync();
|
||||
if (errorCallBack != null)
|
||||
{
|
||||
errorCallBack(ex);
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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<bool> UseTran(Action action, Action<Exception> errorCallBack = null);
|
||||
DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null);
|
||||
|
||||
@@ -34,7 +34,6 @@ namespace SqlSugar
|
||||
dynamic DataTableToDynamic(DataTable table);
|
||||
List<T> DataTableToList<T>(DataTable table);
|
||||
DataTable ListToDataTable<T>(List<T> list);
|
||||
DataTable ListToDataTableWithAttr<T>(List<T> list);
|
||||
Dictionary<string, object> DataTableToDictionary(DataTable table);
|
||||
List<Dictionary<string, object>> DataTableToDictionaryList(DataTable table);
|
||||
ICacheService GetReflectionInoCacheInstance();
|
||||
|
||||
@@ -11,6 +11,9 @@ namespace SqlSugar
|
||||
void BeginTran();
|
||||
void CommitTran();
|
||||
void RollbackTran();
|
||||
Task BeginTranAsync();
|
||||
Task CommitTranAsync();
|
||||
Task RollbackTranAsync();
|
||||
void ChangeDatabase(dynamic configId);
|
||||
void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression);
|
||||
SqlSugarTransaction UseTran();
|
||||
|
||||
@@ -903,6 +903,13 @@ namespace SqlSugar
|
||||
_IsAllTran = true;
|
||||
AllClientEach(it => it.Ado.BeginTran());
|
||||
}
|
||||
|
||||
public async Task BeginTranAsync()
|
||||
{
|
||||
_IsAllTran = true;
|
||||
await AllClientEachAsync(async it => await it.Ado.BeginTranAsync());
|
||||
}
|
||||
|
||||
public void CommitTran()
|
||||
{
|
||||
this.Context.Ado.CommitTran();
|
||||
@@ -921,6 +928,25 @@ namespace SqlSugar
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
|
||||
public async Task CommitTranAsync()
|
||||
{
|
||||
await this.Context.Ado.CommitTranAsync();
|
||||
await AllClientEachAsync(async it =>
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
await it.Ado.CommitTranAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
SugarRetry.Execute(() => it.Ado.CommitTran(), new TimeSpan(0, 0, 5), 3);
|
||||
}
|
||||
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null)
|
||||
{
|
||||
var result = new DbResult<bool>();
|
||||
@@ -951,10 +977,10 @@ namespace SqlSugar
|
||||
var result = new DbResult<bool>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
await this.BeginTranAsync();
|
||||
if (action != null)
|
||||
await action();
|
||||
this.CommitTran();
|
||||
await this.CommitTranAsync();
|
||||
result.Data = result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -962,7 +988,7 @@ namespace SqlSugar
|
||||
result.ErrorException = ex;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
await this.RollbackTranAsync();
|
||||
if (errorCallBack != null)
|
||||
{
|
||||
errorCallBack(ex);
|
||||
@@ -1041,6 +1067,24 @@ namespace SqlSugar
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
public async Task RollbackTranAsync()
|
||||
{
|
||||
await this.Context.Ado.RollbackTranAsync();
|
||||
await AllClientEachAsync(async it =>
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
await it.Ado.RollbackTranAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
SugarRetry.Execute(() => it.Ado.RollbackTran(), new TimeSpan(0, 0, 5), 3);
|
||||
}
|
||||
|
||||
});
|
||||
_IsAllTran = false;
|
||||
}
|
||||
public void Close()
|
||||
{
|
||||
this.Context.Close();
|
||||
@@ -1354,6 +1398,22 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task AllClientEachAsync(Func<ISqlSugarClient,Task> action)
|
||||
{
|
||||
if (this._AllClients == null)
|
||||
{
|
||||
this._AllClients = new List<SugarTenant>();
|
||||
this._AllClients.Add(new SugarTenant() { ConnectionConfig = this.CurrentConnectionConfig, Context = this.Context });
|
||||
}
|
||||
if (_AllClients.HasValue())
|
||||
{
|
||||
foreach (var item in _AllClients.Where(it => it.Context.HasValue()))
|
||||
{
|
||||
await action(item.Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void InitTenant(SugarTenant Tenant)
|
||||
{
|
||||
if (Tenant.Context == null)
|
||||
|
||||
@@ -93,7 +93,10 @@ namespace SqlSugar
|
||||
{
|
||||
ScopedContext.BeginTran();
|
||||
}
|
||||
|
||||
public Task BeginTranAsync()
|
||||
{
|
||||
return ScopedContext.BeginTranAsync();
|
||||
}
|
||||
public void ChangeDatabase(dynamic configId)
|
||||
{
|
||||
ScopedContext.ChangeDatabase(configId);
|
||||
@@ -113,7 +116,10 @@ namespace SqlSugar
|
||||
{
|
||||
ScopedContext.CommitTran();
|
||||
}
|
||||
|
||||
public Task CommitTranAsync()
|
||||
{
|
||||
return ScopedContext.CommitTranAsync();
|
||||
}
|
||||
public IDeleteable<T> Deleteable<T>() where T : class, new()
|
||||
{
|
||||
return ScopedContext.Deleteable<T>();
|
||||
@@ -472,7 +478,10 @@ namespace SqlSugar
|
||||
{
|
||||
ScopedContext.RollbackTran();
|
||||
}
|
||||
|
||||
public Task RollbackTranAsync()
|
||||
{
|
||||
return ScopedContext.RollbackTranAsync();
|
||||
}
|
||||
|
||||
[Obsolete("use Storageable")]
|
||||
public ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new()
|
||||
|
||||
Reference in New Issue
Block a user