mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-21 19:09:57 +08:00
Support Shard Same Thread
This commit is contained in:
87
Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs
Normal file
87
Src/Asp.Net/SqlServerTest/Demos/B_SharedConnection.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
public class SharedConnection : DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
StudentDal studentDal = new StudentDal();
|
||||
SchoolDal schoolDal = new SchoolDal();
|
||||
|
||||
try
|
||||
{
|
||||
studentDal.BeginTran();
|
||||
|
||||
Console.WriteLine("school Count:"+ schoolDal.GetSchoolCount());//0
|
||||
|
||||
studentDal.AddStudent(new Student() { Name = "StudentTest" });
|
||||
schoolDal.AddSchool(new School() { Name = "SchoolTest" });//1
|
||||
|
||||
Console.WriteLine("school Count:" + schoolDal.GetSchoolCount());
|
||||
|
||||
throw new Exception("error");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
studentDal.RollbackTran();
|
||||
Console.WriteLine("school Count:" + schoolDal.GetSchoolCount());//0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public class StudentDal : BaseDao
|
||||
{
|
||||
public void AddStudent(Student sudent)
|
||||
{
|
||||
db.Insertable(sudent).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
public class SchoolDal : BaseDao
|
||||
{
|
||||
public void AddSchool(School school)
|
||||
{
|
||||
db.Insertable(school).ExecuteCommand();
|
||||
}
|
||||
public int GetSchoolCount()
|
||||
{
|
||||
return db.Queryable<School>().Count();
|
||||
}
|
||||
}
|
||||
|
||||
public class BaseDao
|
||||
{
|
||||
|
||||
public SqlSugar.SqlSugarClient db { get { return GetInstance(); } }
|
||||
public void BeginTran()
|
||||
{
|
||||
db.Ado.BeginTran();
|
||||
}
|
||||
public void CommitTran()
|
||||
{
|
||||
db.Ado.CommitTran();
|
||||
}
|
||||
public void RollbackTran()
|
||||
{
|
||||
db.Ado.RollbackTran();
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(
|
||||
new ConnectionConfig() {
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = false,
|
||||
IsShardSameThread= true /*Shard Same Thread*/
|
||||
});
|
||||
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user