删除和创建一起执行,遇到并发概率性删不掉,写入重复数据 返回
private static void Main(string[] args)
{
var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
{
ConnectionString = "",
DbType = DbType.PostgreSQL,
IsAutoCloseConnection = true
});
//建表
//db.CodeFirst.InitTables<Test001>();
db.Insertable(new Test001() { id = 1, name = "init" }).ExecuteCommand();//用例代码
var tasks = new Task[2]; // 假设我们要并发执行10个任务
for (int i = 0; i < tasks.Length; i++)
{
int taskId = i;
tasks[i] = Task.Run(() => DoWork(db, i));
}
Console.WriteLine("用例跑完");
Console.ReadKey();
}
private static void DoWork(SqlSugarScope db, int id)
{
//插入测试数据
Console.WriteLine(id);
db.Deleteable<Test001>().Where(x => x.id == 1).ExecuteCommand();
db.Insertable(new Test001() { id = 1, name = $"{id}" }).ExecuteCommand();//用例代码
}
//建类
public class Test001
{
public int id { get; set; }
public string name { get; set; }
}
热忱回答(8)
-
fate sta VIP02周前
这种肯定不行
0 回复 -
fate sta VIP02周前
这种一般要用锁
0 回复 -
Mr、谢 VIP02周前
@fate sta:这种用什么锁呢?
0 回复 -
fate sta VIP02周前
private static void DoWork(SqlSugarScope db, int id) { var newDb=db.CopyNew();//保证并发下的线程安全 newDb.BeginTran(); //查询条件记录后锁表 var data=newDb.Queryable<Order>().TranLock(DbLockType.Wait).Where(it=>it.Id==1).ToList();//返回条数尽量最少尽量主键 //插入、更新等操作 newDb.CommitTran(); }
0 回复 -
Mr、谢 VIP02周前
@fate sta:还是一样不行啊
internal class Program
{
private static void Main(string[] args)
{
var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
{
ConnectionString = "",
DbType = DbType.PostgreSQL,
IsAutoCloseConnection = true
});
//建表
//db.CodeFirst.InitTables<Test001>();
db.Insertable(new Test001() { name = "1" }).ExecuteCommand();//用例代码
var tasks = new Task[8]; // 假设我们要并发执行10个任务
for (int i = 0; i < tasks.Length; i++)
{
int taskId = i;
tasks[i] = Task.Run(() => DoWork(db, i));
}
Console.WriteLine("用例跑完");
Console.ReadKey();
}
private static void DoWork(SqlSugarScope db, int id)
{
var newDb = db.CopyNew();//保证并发下的线程安全
newDb.BeginTran();
newDb.Queryable<Test001>().TranLock(DbLockType.Wait).Where(it => it.name == "1").ToList();
newDb.Deleteable<Test001>().Where(x => x.name == "1").ExecuteCommand();
newDb.Insertable(new Test001() { name = $"{1}" }).ExecuteCommand();//用例代码
newDb.CommitTran();
//插入测试数据
Console.WriteLine(id);
}
//建类
public class Test001
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int id { get; set; }
public string name { get; set; }
}
}
0 回复 -
fate sta VIP02周前
using (db.AsyncLock())
{
newDb.Deleteable<Test001>().Where(x => x.name == "1").ExecuteCommand();
newDb.Insertable(new Test001() { name = $"{1}" }).ExecuteCommand();//用例代码
}
改成这样吧,之前的方式确实不好锁因为 ,只能已存在锁
0 回复 -
Mr、谢 VIP02周前
@fate sta:用这个是不是不能用工作单元了 事务开在锁外面也不行
0 回复 -
Mr、谢 VIP01周前
@fate sta:解决了
0 回复