Add Async Tran

This commit is contained in:
610262374@qq.com
2018-09-29 16:21:08 +08:00
parent a27ad4df2b
commit ca96c7999f
3 changed files with 54 additions and 0 deletions

View File

@@ -194,6 +194,27 @@ namespace OrmTest.Demo
db.Ado.RollbackTran();
throw;
}
//async tran
var asyncResult = db.Ado.UseTranAsync(() =>
{
var beginCount = db.Queryable<Student>().ToList();
db.Ado.ExecuteCommand("delete student");
var endCount = db.Queryable<Student>().Count();
throw new Exception("error haha");
});
asyncResult.Wait();
var asyncCount = db.Queryable<Student>().Count();
//async
var asyncResult2 = db.Ado.UseTranAsync<List<Student>>(() =>
{
return db.Queryable<Student>().ToList();
});
asyncResult2.Wait();
}
private static void Group()
{

View File

@@ -194,6 +194,17 @@ namespace SqlSugar
}
return result;
}
public Task<DbResult<bool>> UseTranAsync(Action action)
{
Task<DbResult<bool>> result = new Task<DbResult<bool>>(() =>
{
return UseTran(action);
});
TaskStart(result);
return result;
}
public DbResult<T> UseTran<T>(Func<T> action)
{
var result = new DbResult<T>();
@@ -214,6 +225,17 @@ namespace SqlSugar
}
return result;
}
public Task<DbResult<T>> UseTranAsync<T>(Func<T> action)
{
Task<DbResult<T>> result = new Task<DbResult<T>>(() =>
{
return UseTran(action);
});
TaskStart(result);
return result;
}
public void UseStoredProcedure(Action action)
{
var oldCommandType = this.CommandType;
@@ -624,6 +646,14 @@ namespace SqlSugar
#endregion
#region Helper
private void TaskStart<Type>(Task<Type> result)
{
if (this.Context.CurrentConnectionConfig.IsShardSameThread)
{
Check.Exception(true, "IsShardSameThread=true can't be used async method");
}
result.Start();
}
private void ExecuteProcessingSQL(ref string sql, SugarParameter[] parameters)
{
var result = this.ProcessingEventStartingSQL(sql, parameters);

View File

@@ -86,6 +86,9 @@ namespace SqlSugar
DbResult<bool> UseTran(Action action);
DbResult<T> UseTran<T>(Func<T> action);
Task<DbResult<bool>> UseTranAsync(Action action);
Task<DbResult<T>> UseTranAsync<T>(Func<T> action);
void UseStoredProcedure(Action action);
T UseStoredProcedure<T>(Func<T> action);
IAdo UseStoredProcedure();