How To Mutex Across a Network?

后端 未结 6 1394
独厮守ぢ
独厮守ぢ 2021-01-17 18:05

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 a

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 19:01

    In the past I have done the following:

    1. Create a table that basically has two fields, function_name and is_running
    2. I don't know what RDBMS you are using, but most have a way to lock individual records for update. Here is some pseduocode based on Oracle:

      BEGIN TRANS

      SELECT FOR UPDATE is_running FROM function_table WHERE function_name='foo';

      -- Check here to see if it is running, if not, you can set running to 'true'

      UPDATE function_table set is_running='Y' where function_name='foo';

      COMMIT TRANS

    Now I don't have the Oracle PSQL docs with me, but you get the idea. The 'FOR UPDATE' clause locks there record after the read until the commit, so other processes will block on that SELECT statement until the current process commits.

提交回复
热议问题