• sqlsugar 事务执行


    1、单库事务

    单库事务是针一个db操作执行的事务,无论是 ISqlSugarClient和  SqlSugarClient 用法都一样

        try
        {
            db.Ado.BeginTran();
            db.Insertable(new Order() { .....}).ExecuteCommand();
            db.Ado.CommitTran();
        }
        catch (Exception ex)
        {
            db.Ado.RollbackTran();
            throw ex;
        }

    如果一个db就一个库,那么你也可以用多租户事务节约代码,因为2者在一个库的情况下作用一样

    db.BeginTran();//去掉了.ado
    db.CommitTran();//去掉了.ado
    db.RollbackTran();//去掉了.ado
    //ISqlSugarClient 接口使用多租户事务 看文档2.2

    2、多库事务(可跨库)

    多数据库事务是SqlSugar独有的功能,稳定比CAP更强(CAP还有一层队列),在单个程序中可以很愉快的使用多库事务

    SqlSugarClient或者SqlSugarSope 继承于2个接口 ,代码如下事物

    SqlSugarClient : ISqlSugarClient, ITenant

    多租户声明

    SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>(){
    new ConnectionConfig(){ ConfigId="0", DbType=DbType.SqlServer,ConnectionString=..,IsAutoCloseConnection=true},
    new ConnectionConfig(){ ConfigId="1", DbType=DbType.MySql,ConnectionString=..,IsAutoCloseConnection=true}
    });

    简单的说多租户事务和单库事务用法基本100%一致,唯一区别就是少了.Ado

     db.Ado.BeginTran//单库
     db.BeginTran //多库
     
     
     

    2.1 SqlSugarClient事务

      因为继承  ITenant 了可以直接使用   (老版本var mysql=db.GetConnection要写在事务外面)

     //禁止使用 db.Ado.BeginTran,多租户是db.BeginTran
     try
     {
         db.BeginTran();
          
         db.GetConnection("1").Insertable(new Order() { }).ExecuteCommand();
         db.GetConnection("0").Insertable(new Order() { }).ExecuteCommand();
          
         db.CommitTran();
     }
     catch (Exception)
     {
         db.RollbackTran();//数据回滚
         throw;
     }

    2.2 ISqlSugarClient事务

    因为和ITenant没有继承关需要转换一下 

    db.AsTenant().BeginTran();//低版本 (db as ITenant).BeginTran() 
    db.AsTenant().CommitTran();
    db.AsTenant().RollbackTran();
  • 相关阅读:
    蓝凌OA 后台URL跳转(鸡肋0day)
    蓝凌后台注入分析
    蓝凌ssrf+xmldecoder
    shiro550反序列化复现
    BCEL ClassLoader加载字节码
    TemplatesImple链加载字节码
    ysoserial Commons Collections3反序列化研究
    Xstream远程代码执行(CVE-2020-26217)复现分析
    Java安全之命令执行(二)
    Java安全之命令执行(一)
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/16818336.html
Copyright © 2020-2023  润新知