Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreeSqlCloud的change方法存在多线程并发问题 #10

Open
xuanyuan5005 opened this issue Dec 4, 2024 · 8 comments
Open

FreeSqlCloud的change方法存在多线程并发问题 #10

xuanyuan5005 opened this issue Dec 4, 2024 · 8 comments

Comments

@xuanyuan5005
Copy link

监控日志
其中energy是主库,energy+项目Id是项目独立库,任务1是一个线程。其他任务每个任务也是一个线程。

2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy2 //任务1输出调用change
project id = 2 //任务1输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy2 -> energy //其他任务输出调用change输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy2 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy2 -> energy3 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy3 -> energy4 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy4 -> energy6 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy6 -> energy //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy7 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy7 -> energy8 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy8 -> energy9 //其他任务输出
2024-12-04 09:00:00 【FreeSql.Cloud】数据库切换[Change] energy9 -> energy2 //其他任务输出
lastCals count = 1 //任务1输出(直接从固定表中查询行数),但是查询得到的数据并不是energy2库中表的,而是去查的energy8的数据。

如果把其他任务都注释掉,只留任务1运行,则数据正确,监控日志如下
2024-12-04 10:20:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy2 //任务1输出
project id = 2 //任务1输出
lastCals count = 687 //任务1输出(直接从固定表中查询行数)

@2881099
Copy link
Owner

2881099 commented Dec 4, 2024

试试在 change 之后打印 cloud.Ado.ConnectionString

@xuanyuan5005
Copy link
Author

任务1:threadId=28
任务2:threadId=31

项目:项目1
计算开始时间:2024-12-05 08:20:00
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy2
ThreadId=28 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy2
ThreadId=28 project id = 2
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy2 -> energy
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Initial Catalog=energy;Password=pwd123456;Charset=utf8;SslMode=none
ThreadId=30 Data Source=172.19.9.4;Port=3306;User ID=root;Initial Catalog=energy;Password=pwd123456;Charset=utf8;SslMode=none //为何出现这行?连接字符串在封装change的函数统一打印,按理应该切换和连接字符串一一对应
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy2
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy2
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy2 -> energy
ThreadId=32 Data Source=172.19.9.4;Port=3306;User ID=root;Initial Catalog=energy;Password=pwd123456;Charset=utf8;SslMode=none
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy -> energy3
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy3
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy3 -> energy4
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy4
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy4 -> energy6
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy6
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy6 -> energy7
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy7
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy7 -> energy8
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy8
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy8 -> energy9
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy9
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy9 -> energy10
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy10
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy10 -> energy11
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy11
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy11 -> energy12
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy12
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy12 -> energy13
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy13
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy13 -> energy14
ThreadId=31 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy14
ThreadId=28 lastCals count = 5 //结果错误,这里实际访问了energy10

项目:项目2
计算开始时间:2024-12-05 08:20:00
2024-12-05 00:20:00 【FreeSql.Cloud】数据库切换[Change] energy14 -> energy3
ThreadId=28 Data Source=172.19.9.4;Port=3306;User ID=root;Password=pwd123456;Charset=utf8;SslMode=none;Initial Catalog=energy3
ThreadId=28 project id = 3
ThreadId=28 lastCals count = 159

@2881099
Copy link
Owner

2881099 commented Dec 5, 2024

你是 asp.net core 吗?有没有用 async ,不要用 async void 这些,也不要不 await 的异步

@xuanyuan5005
Copy link
Author

运行入口在定时任务quarz中,quarz的Execute接口确实是async的

@2881099
Copy link
Owner

2881099 commented Dec 5, 2024

async void Method() {} //错误

async Task Method() {
不 await //也是错误
}

确保完整的按顺序的执行上下文

@xuanyuan5005
Copy link
Author

现在入口写法是这样的
//quarz接口定义
public async System.Threading.Tasks.Task Execute(IJobExecutionContext context)
{
await System.Threading.Tasks.Task.Run(() =>
{
ExecuteTask();
});
}

@xuanyuan5005
Copy link
Author

这个执行其中一个任务(相当于日志中threadId=28,任务1),还有其他任务实现Execute接口,写法也是这样

@2881099
Copy link
Owner

2881099 commented Dec 5, 2024

FreeSqlCloud 切换逻辑,看一下 AsyncLocal 的文章

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants