SqlSugar学习笔记六——事务
实体类
///<summary>
///
///</summary>
[SugarTable("Student")]
public partial class Student
{
public Student(){
}
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[SugarColumn(IsPrimaryKey=true,IsIdentity=true)]
public int Id {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int? SchoolId {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string Name {get;set;}
}
事务
/// <summary>
/// 租户事务
/// </summary>
public static class TenantTransactions
{
/// <summary>
/// 多租户事务
/// </summary>
public static void Show()
{
List<ConnectionConfig> connectionconfiglist = new List<ConnectionConfig>()
{
new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer,ConnectionString="Data Source=DESKTOP-VF73RDU;Initial Catalog=CustomerDB;Persist Security Info=True;User ID=sa;Password=sa123",IsAutoCloseConnection=true},
new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer,ConnectionString="Data Source=DESKTOP-VF73RDU;Initial Catalog=CustomerDB_New;Persist Security Info=True;User ID=sa;Password=sa123",IsAutoCloseConnection=true}
};
using (SqlSugarClient db = new SqlSugarClient(connectionconfiglist))
{
SqlSugarProvider provoder1 = db.GetConnection("1");
SqlSugarProvider provoder2 = db.GetConnection("2");
try
{
db.BeginTran(); //开启事务
provoder1.Insertable(new Student() { Name = "Richard老师--租户20220525", SchoolId = 1 }).ExecuteCommand();
provoder2.Insertable(new Student() { Name = "Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525Richard老师--租户20220525", SchoolId = 1 }).ExecuteCommand();
db.CommitTran();
}
catch (Exception)
{
db.RollbackTran();
}
finally
{
GC.Collect();
}
}
}
/// <summary>
/// 事务
/// </summary>
public static void TenantShow()
{
List<ConnectionConfig> connectionconfiglist = new List<ConnectionConfig>()
{
new ConnectionConfig(){DbType=DbType.SqlServer,ConnectionString="Data Source=DESKTOP-VF73RDU;Initial Catalog=CustomerDB;Persist Security Info=True;User ID=sa;Password=sa123",IsAutoCloseConnection=true}
};
using (SqlSugarClient db = new SqlSugarClient(connectionconfiglist))
{
try
{
db.AsTenant().BeginTran();
db.Insertable(new Student() { Name = "Richard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
db.Insertable(new Student() { Name = "Richard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
db.AsTenant().CommitTran();
}
catch (Exception)
{
db.AsTenant().RollbackTran();
}
finally
{
GC.Collect();
}
}
}
/// <summary>
/// 发生异常自动回滚
/// </summary>
public static void UseTranShow()
{
try
{
List<ConnectionConfig> connectionconfiglist = new List<ConnectionConfig>()
{
new ConnectionConfig(){DbType=DbType.SqlServer,ConnectionString="Data Source=DESKTOP-VF73RDU;Initial Catalog=CustomerDB;Persist Security Info=True;User ID=sa;Password=sa123",IsAutoCloseConnection=true}
};
using (SqlSugarClient db = new SqlSugarClient(connectionconfiglist))
{
using (SqlSugarTransaction tran = db.UseTran())
{
db.Insertable(new Student() { Name = "Richard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
db.Insertable(new Student() { Name = "Richard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
tran.CommitTran();
}
}
}
catch (Exception)
{
}
}
/// <summary>
/// 事务语法糖写法
/// </summary>
public static void UseTranShowNew()
{
List<ConnectionConfig> connectionconfiglist = new List<ConnectionConfig>()
{
new ConnectionConfig(){DbType=DbType.SqlServer,ConnectionString="Data Source=DESKTOP-VF73RDU;Initial Catalog=CustomerDB;Persist Security Info=True;User ID=sa;Password=sa123",IsAutoCloseConnection=true}
};
using (SqlSugarClient db = new SqlSugarClient(connectionconfiglist))
{
DbResult<bool> result = db.UseTran(() =>
{
db.Insertable(new Student() { Name = "Richard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
db.Insertable(new Student() { Name = "Richard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 NewRichard老师--租户 New", SchoolId = 1 }).ExecuteCommand();
return true;// 返回值等行res.Data
});
if (result.Data == false) //返回值为false
{
//result.Data 业务的返回值
//如果是上面的逻辑 result.Data==true肯定业务成功并且事务成功
//if(result.IsSuccess==false)//事务执行了回滚
//if(result.IsSuccess==true)//事务提交完成
}
}
}
}
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/16464175.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。