Skip to content
果糖网 edited this page Jul 21, 2024 · 5 revisions

1、Single db transaction

A single library transaction is a transaction that executes a db operation, both ISqlSugarClient and SqlSugarClient

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

If a db has only one library, then you can also use multi-tenant transactions to save code, because they work equally well with one library

db.BeginTran(); // The.ADO is removed
db.CommitTran(); // The.ADO is removed
db.RollbackTran(); // The.ADO is removed
// The ISqlSugarClient interface uses multi-tenant transactions see Documentation 2.2

2、multi-library transactions (cross-library)

Multi-database transactions are a unique feature of SqlSugar and are more stable than CAP (CAP also has a layer of queues), making it a pleasant use of multi-database transactions in a single program SqlSugarClient or SqlSugarSope inherits from two interfaces with the following code SqlSugarClient : ISqlSugarClient, ITenant Multi-tenant declaration

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}
});

To put it simply, the usage of multi-tenant transactions and single-library transactions is basically 100% the same, the only difference is that there are fewer. Ado

db.Ado.BeginTran// Single database
db.BeginTran // Multi-database transaction
db.AsTenant().BeginTran()// Multi-database transactions are typically used by the ISqlSugarClient interface

2.1 SqlSugarClient transaction

Because the ITenant inheritance can be used directly (old var mysql=db.GetConnection should be written outside the transaction)

// db.Ado.BeginTran is prohibited. Multi-tenant is 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(); // Data rollback
throw;
}

// Master db
// Injected SqlSugarClient or SqlSugarScope we call the primary db

// Child db
// What we call a child db comes out of the tenant method GetConnection and is used to operate the current database. There is no tenant transactional-related method

// The master db can manage multiple sub-DBs with transactions, or it can use tenant methods such as GetConnection



// At present, the underlying mechanism is to submit transactions in a unified manner after successful service execution. If all services fail, they are rolled back three times during the rollback
// From the point of view of the current user usage, it is quite stable with almost no failure feedback
// High security data: Use the difference log +Catch(AggregateException ex) to compensate the quality
// throw new AggregateException if the rollback fails

2.2 ISqlSugarClient transaction

Because there is no inheritance with ITenant, it needs to be converted

db.AsTenant().BeginTran(); // Lower version (db as ITenant).BeginTran()
db.AsTenant().CommitTran();
db.AsTenant().RollbackTran()