如何跨网络互斥?互斥、网络

2023-09-07 16:38:30 作者:别拿臾凉拥抱我死亡

我有一个在网络上运行的桌面应用程序,每个实例都连接到同一个数据库.

I have a desktop application that runs on a network and every instance connects to the same database.

那么,在这种情况下,我如何实现一个互斥锁,该互斥锁适用于连接到同一数据库的所有正在运行的实例?

So, in this situation, how can I implement a mutex that works across all running instances that are connected to the same database?

换句话说,我不希望这两个以上的实例同时运行相同的功能.如果一个已经在运行该函数,则其他实例不应访问它.

In other words, I don't wan't that two+ instances to run the same function at the same time. If one is already running the function, the other instances shouldn't have access to it.

PS:数据库事务不会解决,因为我不想互斥的功能不使用数据库.我提到数据库只是因为它可以用来在运行的实例之间交换信息.

PS: Database transaction won't solve, because the function I wan't to mutex doesn't use the database. I've mentioned the database just because it can be used to exchange information across the running instances.

PS2:该功能大约需要 30 分钟才能完成,所以如果第二个实例尝试运行相同的功能,我想显示一个很好的消息,它现在无法执行,因为计算机X"已经运行该函数.

PS2: The function takes about ~30 minutes to complete, so if a second instance tries to run the same function I would like to display a nice message that it can't be performed right now because computer 'X' is already running that function.

PS3:函数必须在客户端机器上处理,所以我不能使用存储过程.

PS3: The function has to be processed on the client machine, so I can't use stored procedures.

推荐答案

我认为您正在寻找数据库事务.事务会将您的更改与所有其他客户端隔离.

I think you're looking for a database transaction. A transaction will isolate your changes from all other clients.

更新:您提到该函数当前不写入数据库.如果你想对这个函数进行互斥,必须有一些中心位置来存储当前的互斥持有者.数据库可以为此工作——只需添加一个包含当前持有者计算机名的新表.在开始您的函数之前检查该表.

Update: You mentioned that the function doesn't currently write to the database. If you want to mutex this function, there will have to be some central location to store the current mutex holder. The database can work for this -- just add a new table that includes the computername of the current holder. Check that table before starting your function.

我认为您的问题可能令人困惑.互斥锁应该是关于保护资源的.如果你的函数没有访问数据库,那么你在保护什么共享资源?

I think your question may be confusion though. Mutexes should be about protecting resources. If your function is not accessing the database, then what shared resource are you protecting?